|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Repositories; |
| 4 | + |
| 5 | +use Illuminate\Container\Container as Application; |
| 6 | +use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 7 | +use Illuminate\Database\Eloquent\Builder; |
| 8 | +use Illuminate\Database\Eloquent\Collection; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | + |
| 11 | +abstract class BaseRepository |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Model |
| 15 | + */ |
| 16 | + protected Model $model; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var Application |
| 20 | + */ |
| 21 | + protected Application $app; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param Application $app |
| 25 | + * |
| 26 | + * @throws \Exception |
| 27 | + */ |
| 28 | + public function __construct(Application $app) |
| 29 | + { |
| 30 | + $this->app = $app; |
| 31 | + $this->makeModel(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Get searchable fields array |
| 36 | + * |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + abstract public function getFieldsSearchable(): array; |
| 40 | + |
| 41 | + /** |
| 42 | + * Configure the Model |
| 43 | + * |
| 44 | + * @return string |
| 45 | + */ |
| 46 | + abstract public function model(): string; |
| 47 | + |
| 48 | + /** |
| 49 | + * Make Model instance |
| 50 | + * |
| 51 | + * @return Model |
| 52 | + * @throws \Exception |
| 53 | + * |
| 54 | + */ |
| 55 | + public function makeModel(): Model |
| 56 | + { |
| 57 | + $model = $this->app->make($this->model()); |
| 58 | + |
| 59 | + if (!$model instanceof Model) { |
| 60 | + throw new \Exception("Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"); |
| 61 | + } |
| 62 | + |
| 63 | + return $this->model = $model; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Paginate records for scaffold. |
| 68 | + * |
| 69 | + * @param int $perPage |
| 70 | + * @param array $columns |
| 71 | + * @return LengthAwarePaginator |
| 72 | + */ |
| 73 | + public function paginate(int $perPage, array $columns = ['*']): LengthAwarePaginator |
| 74 | + { |
| 75 | + $query = $this->allQuery(); |
| 76 | + |
| 77 | + return $query->paginate($perPage, $columns); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Build a query for retrieving all records. |
| 82 | + * |
| 83 | + * @param array $search |
| 84 | + * @param int|null $skip |
| 85 | + * @param int|null $limit |
| 86 | + * @return Builder |
| 87 | + */ |
| 88 | + public function allQuery(array $search = [], int $skip = null, int $limit = null): Builder |
| 89 | + { |
| 90 | + $query = $this->model->newQuery(); |
| 91 | + |
| 92 | + if (count($search)) { |
| 93 | + foreach ($search as $key => $value) { |
| 94 | + if (in_array($key, $this->getFieldsSearchable())) { |
| 95 | + $query->where($key, $value); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (!is_null($skip)) { |
| 101 | + $query->skip($skip); |
| 102 | + } |
| 103 | + |
| 104 | + if (!is_null($limit)) { |
| 105 | + $query->limit($limit); |
| 106 | + } |
| 107 | + |
| 108 | + return $query; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Retrieve all records with given filter criteria |
| 113 | + * |
| 114 | + * @param array $search |
| 115 | + * @param int|null $skip |
| 116 | + * @param int|null $limit |
| 117 | + * @param array $columns |
| 118 | + * |
| 119 | + * @return LengthAwarePaginator|Builder[]|Collection |
| 120 | + */ |
| 121 | + public function all(array $search = [], int $skip = null, int $limit = null, array $columns = ['*']): Collection|LengthAwarePaginator|array |
| 122 | + { |
| 123 | + $query = $this->allQuery($search, $skip, $limit); |
| 124 | + |
| 125 | + return $query->get($columns); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Create model record |
| 130 | + * |
| 131 | + * @param array $input |
| 132 | + * |
| 133 | + * @return Model |
| 134 | + */ |
| 135 | + public function create(array $input): Model |
| 136 | + { |
| 137 | + $model = $this->model->newInstance($input); |
| 138 | + |
| 139 | + $model->save(); |
| 140 | + |
| 141 | + return $model; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Find model record for given id |
| 146 | + * |
| 147 | + * @param int $id |
| 148 | + * @param array $columns |
| 149 | + * |
| 150 | + * @return Builder|Builder[]|Collection|Model|null |
| 151 | + */ |
| 152 | + public function find(int $id, array $columns = ['*']): Model|Collection|Builder|array|null |
| 153 | + { |
| 154 | + $query = $this->model->newQuery(); |
| 155 | + |
| 156 | + return $query->find($id, $columns); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Update model record for given id |
| 161 | + * |
| 162 | + * @param array $input |
| 163 | + * @param int $id |
| 164 | + * |
| 165 | + * @return Builder|Builder[]|Collection|Model |
| 166 | + */ |
| 167 | + public function update(array $input, int $id): Model|Collection|Builder|array |
| 168 | + { |
| 169 | + $query = $this->model->newQuery(); |
| 170 | + |
| 171 | + $model = $query->findOrFail($id); |
| 172 | + |
| 173 | + $model->fill($input); |
| 174 | + |
| 175 | + $model->save(); |
| 176 | + |
| 177 | + return $model; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @param int $id |
| 182 | + * |
| 183 | + * @return bool|mixed|null |
| 184 | + * @throws \Exception |
| 185 | + * |
| 186 | + */ |
| 187 | + public function delete(int $id): mixed |
| 188 | + { |
| 189 | + $query = $this->model->newQuery(); |
| 190 | + |
| 191 | + $model = $query->findOrFail($id); |
| 192 | + |
| 193 | + return $model->delete(); |
| 194 | + } |
| 195 | +} |
0 commit comments