|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yoelfme\PatternRepository; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Foundation\Application; |
| 8 | + |
| 9 | +/** |
| 10 | + * This is a class for implements a pattern repository in models Eloquent in Laravel |
| 11 | + * @package Yoelfme\PatternRepository |
| 12 | + */ |
| 13 | +abstract class Repository implements RepositoryInterface |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Instance of Illuminate\Foundation |
| 17 | + */ |
| 18 | + protected $app = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * Instance of Eloquent Model |
| 22 | + * @var Model |
| 23 | + */ |
| 24 | + protected $model = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * Variable with the list of relations of model |
| 28 | + * @var array |
| 29 | + */ |
| 30 | + protected $relations = array(); |
| 31 | + |
| 32 | + function __construct(Application $app) { |
| 33 | + $this->app = $app; |
| 34 | + $this->makeModel(); |
| 35 | + $this->relations = $this->model->_relations; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Function to return class with the namespace of Eloquent Model |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + abstract function model(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Function to create the instance of Eloquent Model |
| 46 | + * @return Model |
| 47 | + * @throws Exception |
| 48 | + */ |
| 49 | + public function makeModel() |
| 50 | + { |
| 51 | + $name_model = $this->model(); |
| 52 | + $model = $this->app->make($name_model); |
| 53 | + |
| 54 | + if(!$model instanceof Model) { |
| 55 | + throw new Exception("Class { $name_model } must be an instance of Illuminate\\Database\\Eloquent\\Model"); |
| 56 | + } |
| 57 | + |
| 58 | + return $this->model = $model; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return all rows of model, select all columns default |
| 63 | + * @param array $columns |
| 64 | + */ |
| 65 | + public function all($columns = array('*')) |
| 66 | + { |
| 67 | + return $this->model->get($columns); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Return the number of rows given |
| 72 | + * @param $limit |
| 73 | + */ |
| 74 | + public function take($limit) |
| 75 | + { |
| 76 | + return $this->model->get()->take($limit); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Return the number of rows given but filtered the field by value given |
| 81 | + * @param $limit |
| 82 | + * @param $field |
| 83 | + * @param $value |
| 84 | + * @return mixed |
| 85 | + */ |
| 86 | + public function takeBy($limit, $field, $value) |
| 87 | + { |
| 88 | + return $this->model->where($field, $value)->get()->take($limit); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return the number of rows given but filtered the field by value given with relations |
| 93 | + * @param $limit |
| 94 | + * @param $field |
| 95 | + * @param $value |
| 96 | + * @return mixed |
| 97 | + */ |
| 98 | + public function takeByWithRelations($limit, $field, $value) |
| 99 | + { |
| 100 | + return $this->model->with($this->relations)->where($field, $value)->get()->take($limit); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Return the number of random rows given but filtered the field by value given with relations only for MySQL Database |
| 105 | + * @param $limit |
| 106 | + * @param $field |
| 107 | + * @param $value |
| 108 | + * @return mixed |
| 109 | + */ |
| 110 | + public function takeRandomByWithRelations($limit, $field, $value) |
| 111 | + { |
| 112 | + return $this->model->with($this->relations)->where($field, $value) |
| 113 | + ->orderBy(DB::raw('RAND()'))->get()->take($limit); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Return all rows with relations |
| 118 | + * @param array $columns |
| 119 | + * @param array $where |
| 120 | + */ |
| 121 | + public function allWhitRelations($columns = array('*'), $where = array()) |
| 122 | + { |
| 123 | + return $this->model->with($this->relations)->get($columns); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Return all rows paginate by value of $perPage |
| 128 | + * @param int $perPage |
| 129 | + * @param array $columns |
| 130 | + * @return mixed |
| 131 | + */ |
| 132 | + public function paginate($perPage = 10, $columns = array('*')) |
| 133 | + { |
| 134 | + return $this->model->paginate($perPage, $columns); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Create a record of model |
| 139 | + * @param array $data |
| 140 | + * @return mixed |
| 141 | + */ |
| 142 | + public function create(array $data) |
| 143 | + { |
| 144 | + return $this->model->create($data); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Update a model pass the new data and id of record or entity |
| 149 | + * @param array $data |
| 150 | + * @param $entity |
| 151 | + * @return mixed |
| 152 | + */ |
| 153 | + public function update(array $data, $entity) |
| 154 | + { |
| 155 | + if(is_numeric($entity)) |
| 156 | + { |
| 157 | + $entity = $this->findOrFail($entity); |
| 158 | + } |
| 159 | + |
| 160 | + $entity->fill($data); |
| 161 | + $entity->save(); |
| 162 | + |
| 163 | + return $entity; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Delete a record by id of record or entity |
| 168 | + * @param $entity |
| 169 | + * @return null |
| 170 | + */ |
| 171 | + public function delete($entity) |
| 172 | + { |
| 173 | + try |
| 174 | + { |
| 175 | + if(is_numeric($entity)) |
| 176 | + { |
| 177 | + $this->model->destroy($entity); |
| 178 | + } |
| 179 | + else { |
| 180 | + $entity = $this->findOrFail($entity); |
| 181 | + $entity->delete(); |
| 182 | + } |
| 183 | + return true; |
| 184 | + } |
| 185 | + catch(\Exception $e) |
| 186 | + { |
| 187 | + return false; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @param string $field |
| 193 | + * @param string $value |
| 194 | + * @param array $columns |
| 195 | + * @return mixed |
| 196 | + */ |
| 197 | + public function where($field, $value, $columns = array('*')) |
| 198 | + { |
| 199 | + return $this->model->where($field, $value)->get($columns); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param string $field |
| 204 | + * @param string $value |
| 205 | + * @param array $columns |
| 206 | + * @return mixed |
| 207 | + */ |
| 208 | + public function whereWithRelations($field, $value, $columns = array('*')) |
| 209 | + { |
| 210 | + return $this->model->with($this->relations)->where($field, $value)->get($columns); |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + /** |
| 215 | + * @param $id |
| 216 | + * @param array $columns |
| 217 | + * @return mixed |
| 218 | + */ |
| 219 | + public function find($id, $columns = array('*')) |
| 220 | + { |
| 221 | + return $this->model->find($id, $columns); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * @param $id |
| 226 | + * @param array $columns |
| 227 | + * @return mixed |
| 228 | + */ |
| 229 | + public function findWithRelations($id, $columns = array('*')) |
| 230 | + { |
| 231 | + return $this->model->with($this->relations)->find($id, $columns); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @param $id |
| 236 | + * @param array $columns |
| 237 | + * @return mixed |
| 238 | + */ |
| 239 | + public function findOrFail($id, $columns = array('*')) |
| 240 | + { |
| 241 | + return $this->model->findOrFail($id, $columns); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @param $field |
| 246 | + * @param $value |
| 247 | + * @param array $columns |
| 248 | + * @return mixed |
| 249 | + */ |
| 250 | + public function findBy($field, $value, $columns = array('*')) |
| 251 | + { |
| 252 | + return $this->model->where($field, $value)->first($columns); |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * @param string $field |
| 257 | + * @param string $value |
| 258 | + * @param array $columns |
| 259 | + * @return mixed |
| 260 | + */ |
| 261 | + public function findByWithRelations($field, $value, $columns = array('*')) |
| 262 | + { |
| 263 | + return $this->model->with($this->relations)->where($field, $value)->first($columns); |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * @param string $display |
| 268 | + */ |
| 269 | + public function listsIdAnd($display) |
| 270 | + { |
| 271 | + return $this->model->lists($display, 'id'); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * @param string $value |
| 276 | + */ |
| 277 | + public function listsField($value) |
| 278 | + { |
| 279 | + return $this->model->lists($value); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * @param string $key |
| 284 | + * @param string $value |
| 285 | + */ |
| 286 | + public function listsFields($key, $value) |
| 287 | + { |
| 288 | + return $this->model->lists($value, $key); |
| 289 | + } |
| 290 | + |
| 291 | + |
| 292 | + /** |
| 293 | + * @param string $field |
| 294 | + * @param array $whereIn |
| 295 | + * @return mixed |
| 296 | + */ |
| 297 | + public function whereIn($field, $whereIn = array()) |
| 298 | + { |
| 299 | + return $this->model->whereIn($field, $whereIn)->get(); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * @param string $field |
| 304 | + * @param array $whereIn |
| 305 | + * @return mixed |
| 306 | + */ |
| 307 | + public function whereInWithRelations($field, $whereIn = array()) |
| 308 | + { |
| 309 | + return $this->model->with($this->relations)->whereIn($field, $whereIn)->get(); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * @param array $columns |
| 314 | + * @return mixed |
| 315 | + */ |
| 316 | + public function random($columns = array('*')) |
| 317 | + { |
| 318 | + return $this->model->get()->random(); |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * Return data ordered by field |
| 323 | + * @param string $field |
| 324 | + * @param string $order |
| 325 | + */ |
| 326 | + public function orderBy($field = 'created_at', $order = 'ASC') |
| 327 | + { |
| 328 | + return $this->model->with($this->relations)->orderBy($field, $order)->get(); |
| 329 | + } |
| 330 | +} |
0 commit comments