|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PPSpaces\Repositories; |
| 4 | + |
| 5 | +use PPSpaces\Contracts\EloquentRepository as RepositoryInterface; |
| 6 | + |
| 7 | +abstract class EloquentRepository implements RepositoryInterface { |
| 8 | + |
| 9 | + /** |
| 10 | + * The model instance. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $model; |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a new repository instance. |
| 18 | + * |
| 19 | + * @param \Illuminate\Database\Eloquent\Model $model |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function __construct($model) {} |
| 23 | + |
| 24 | + /** |
| 25 | + * Get all of the models from the database. |
| 26 | + * |
| 27 | + * @param array|mixed $columns |
| 28 | + * @return \Illuminate\Database\Eloquent\Collection|static[] |
| 29 | + */ |
| 30 | + public function all($columns = ['*']) { |
| 31 | + return $this->get($columns); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Dynamically retrieve attributes on the model. |
| 36 | + * |
| 37 | + * @param array $columns |
| 38 | + * @return \Illuminate\Database\Eloquent\Model |
| 39 | + */ |
| 40 | + public function get($columns = ['*']) { |
| 41 | + return $this->model->get($columns); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get a new query of one model by their IDs. |
| 46 | + * |
| 47 | + * @param array|int $ids |
| 48 | + * @return \Illuminate\Database\Eloquent\Model |
| 49 | + */ |
| 50 | + public function find($id) { |
| 51 | + return $this->model->findOrFail($id); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Perform a model create operation. |
| 56 | + * |
| 57 | + * @param \Illuminate\Database\Eloquent\Model $query |
| 58 | + * @return bool |
| 59 | + */ |
| 60 | + public function create(array $attributes) { |
| 61 | + return $this->model->create($attributes); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Update the model in the database. |
| 66 | + * |
| 67 | + * @param array $attributes |
| 68 | + * @param array $options |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + public function update(array $attributes = []) { |
| 72 | + return $this->model->update($attributes); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Delete the model from the database. |
| 77 | + * |
| 78 | + * @return bool|null |
| 79 | + * |
| 80 | + * @throws \Exception |
| 81 | + */ |
| 82 | + public function delete($id) { |
| 83 | + return $this->model->findOrFail($id)->delete(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the value of the model's route key. |
| 88 | + * |
| 89 | + * @return mixed |
| 90 | + */ |
| 91 | + public function getRouteKey() |
| 92 | + { |
| 93 | + return $this->model->getAttribute($this->getRouteKeyName()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the route key for the model. |
| 98 | + * |
| 99 | + * @return string |
| 100 | + */ |
| 101 | + public function getRouteKeyName() |
| 102 | + { |
| 103 | + return $this->model->getKeyName(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Retrieve the model for a bound value. |
| 108 | + * |
| 109 | + * @param mixed $value |
| 110 | + * @return \Illuminate\Database\Eloquent\Model|null |
| 111 | + */ |
| 112 | + public function resolveRouteBinding($value) |
| 113 | + { |
| 114 | + return $this->model->where($this->getRouteKeyName(), $value)->first(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Convert the object into something JSON serializable. |
| 119 | + * |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + public function jsonSerialize() |
| 123 | + { |
| 124 | + return $this->model->toArray(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Convert the model to its string representation. |
| 129 | + * |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + public function __toString() |
| 133 | + { |
| 134 | + return $this->model->toJson(); |
| 135 | + } |
| 136 | +} |
0 commit comments