@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @param array $relations |
| 29 | 29 | * @param string $sortBy |
| 30 | - * @param boolean $desc |
|
| 30 | + * @param integer $desc |
|
| 31 | 31 | * @param array $columns |
| 32 | 32 | * @return collection |
| 33 | 33 | */ |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * @param integer $perPage |
| 44 | 44 | * @param array $relations |
| 45 | 45 | * @param string $sortBy |
| 46 | - * @param boolean $desc |
|
| 46 | + * @param integer $desc |
|
| 47 | 47 | * @param array $columns |
| 48 | 48 | * @return collection |
| 49 | 49 | */ |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @param integer $perPage |
| 62 | 62 | * @param array $relations |
| 63 | 63 | * @param string $sortBy |
| 64 | - * @param boolean $desc |
|
| 64 | + * @param integer $desc |
|
| 65 | 65 | * @param array $columns |
| 66 | 66 | * @return collection |
| 67 | 67 | */ |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * Save the given model to the storage. |
| 77 | 77 | * |
| 78 | 78 | * @param array $data |
| 79 | - * @return mixed |
|
| 79 | + * @return boolean |
|
| 80 | 80 | */ |
| 81 | 81 | public function save(array $data) |
| 82 | 82 | { |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | * @param array $conditions array of conditions |
| 133 | 133 | * @param array $relations |
| 134 | 134 | * @param string $sortBy |
| 135 | - * @param boolean $desc |
|
| 135 | + * @param integer $desc |
|
| 136 | 136 | * @param array $columns |
| 137 | 137 | * @return collection |
| 138 | 138 | */ |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | * @param array $conditions array of conditions |
| 165 | 165 | * @param integer $perPage |
| 166 | 166 | * @param string $sortBy |
| 167 | - * @param boolean $desc |
|
| 167 | + * @param integer $desc |
|
| 168 | 168 | * @param array $columns |
| 169 | 169 | * @return collection |
| 170 | 170 | */ |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __construct($model) |
| 23 | 23 | { |
| 24 | - $this->model = $model; |
|
| 24 | + $this->model = $model; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $model = false; |
| 99 | 99 | $relations = []; |
| 100 | 100 | |
| 101 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 101 | + \DB::transaction(function() use (&$model, &$relations, $data) { |
|
| 102 | 102 | |
| 103 | 103 | $model = $this->prepareModel($data); |
| 104 | 104 | $relations = $this->prepareRelations($data, $model); |
@@ -122,8 +122,8 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function delete($value, $attribute = 'id') |
| 124 | 124 | { |
| 125 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 126 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 125 | + \DB::transaction(function() use ($value, $attribute) { |
|
| 126 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
| 127 | 127 | $model->delete(); |
| 128 | 128 | }); |
| 129 | 129 | }); |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | { |
| 214 | 214 | $model = $this->model->onlyTrashed()->find($id); |
| 215 | 215 | |
| 216 | - if (! $model) { |
|
| 216 | + if ( ! $model) { |
|
| 217 | 217 | \Errors::notFound(class_basename($this->model).' with id : '.$id); |
| 218 | 218 | } |
| 219 | 219 | |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | * @var array |
| 238 | 238 | */ |
| 239 | 239 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 240 | - if (! $model) { |
|
| 240 | + if ( ! $model) { |
|
| 241 | 241 | \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
| 242 | 242 | } |
| 243 | 243 | |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | * If the relation has no value then marke the relation data |
| 295 | 295 | * related to the model to be deleted. |
| 296 | 296 | */ |
| 297 | - if (! $value || ! count($value)) { |
|
| 297 | + if ( ! $value || ! count($value)) { |
|
| 298 | 298 | $relations[$relation] = 'delete'; |
| 299 | 299 | } |
| 300 | 300 | } |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | * Check if the relation is a collection. |
| 313 | 313 | */ |
| 314 | 314 | if (class_basename($model->$relation) == 'Collection') { |
| 315 | - if (! is_array($val)) { |
|
| 315 | + if ( ! is_array($val)) { |
|
| 316 | 316 | $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
| 317 | 317 | } else { |
| 318 | 318 | /** |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | /** |
| 326 | 326 | * If model doesn't exists. |
| 327 | 327 | */ |
| 328 | - if (! $relationModel) { |
|
| 328 | + if ( ! $relationModel) { |
|
| 329 | 329 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
| 330 | 330 | } |
| 331 | 331 | |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | /** |
| 364 | 364 | * If model doesn't exists. |
| 365 | 365 | */ |
| 366 | - if (! $relationModel) { |
|
| 366 | + if ( ! $relationModel) { |
|
| 367 | 367 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
| 368 | 368 | } |
| 369 | 369 | |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
| 574 | 574 | $path = explode('->', $value); |
| 575 | 575 | $field = array_shift($path); |
| 576 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 576 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
| 577 | 577 | return '"'.$part.'"'; |
| 578 | 578 | })->implode('.')); |
| 579 | 579 | |
@@ -8,577 +8,577 @@ |
||
| 8 | 8 | |
| 9 | 9 | abstract class BaseRepository implements BaseRepositoryInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var object |
|
| 13 | - */ |
|
| 14 | - public $model; |
|
| 11 | + /** |
|
| 12 | + * @var object |
|
| 13 | + */ |
|
| 14 | + public $model; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Init new object. |
|
| 18 | - * |
|
| 19 | - * @var mixed model |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function __construct($model) |
|
| 23 | - { |
|
| 24 | - $this->model = $model; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Fetch all records with relations from the storage. |
|
| 29 | - * |
|
| 30 | - * @param array $relations |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @param array $columns |
|
| 34 | - * @return collection |
|
| 35 | - */ |
|
| 36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | - { |
|
| 38 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | - } |
|
| 16 | + /** |
|
| 17 | + * Init new object. |
|
| 18 | + * |
|
| 19 | + * @var mixed model |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function __construct($model) |
|
| 23 | + { |
|
| 24 | + $this->model = $model; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Fetch all records with relations from the storage. |
|
| 29 | + * |
|
| 30 | + * @param array $relations |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @param array $columns |
|
| 34 | + * @return collection |
|
| 35 | + */ |
|
| 36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 37 | + { |
|
| 38 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Fetch all records with relations from storage in pages. |
|
| 44 | - * |
|
| 45 | - * @param integer $perPage |
|
| 46 | - * @param array $relations |
|
| 47 | - * @param string $sortBy |
|
| 48 | - * @param boolean $desc |
|
| 49 | - * @param array $columns |
|
| 50 | - * @return collection |
|
| 51 | - */ |
|
| 52 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 53 | - { |
|
| 54 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 55 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Fetch all records with relations based on |
|
| 60 | - * the given condition from storage in pages. |
|
| 61 | - * |
|
| 62 | - * @param array $conditions array of conditions |
|
| 63 | - * @param integer $perPage |
|
| 64 | - * @param array $relations |
|
| 65 | - * @param string $sortBy |
|
| 66 | - * @param boolean $desc |
|
| 67 | - * @param array $columns |
|
| 68 | - * @return collection |
|
| 69 | - */ |
|
| 70 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 71 | - { |
|
| 72 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 73 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 74 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Count all records based on the given condition from storage. |
|
| 79 | - * |
|
| 80 | - * @param array $conditions array of conditions |
|
| 81 | - * @return collection |
|
| 82 | - */ |
|
| 83 | - public function count($conditions) |
|
| 84 | - { |
|
| 85 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 86 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 87 | - } |
|
| 42 | + /** |
|
| 43 | + * Fetch all records with relations from storage in pages. |
|
| 44 | + * |
|
| 45 | + * @param integer $perPage |
|
| 46 | + * @param array $relations |
|
| 47 | + * @param string $sortBy |
|
| 48 | + * @param boolean $desc |
|
| 49 | + * @param array $columns |
|
| 50 | + * @return collection |
|
| 51 | + */ |
|
| 52 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 53 | + { |
|
| 54 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 55 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Fetch all records with relations based on |
|
| 60 | + * the given condition from storage in pages. |
|
| 61 | + * |
|
| 62 | + * @param array $conditions array of conditions |
|
| 63 | + * @param integer $perPage |
|
| 64 | + * @param array $relations |
|
| 65 | + * @param string $sortBy |
|
| 66 | + * @param boolean $desc |
|
| 67 | + * @param array $columns |
|
| 68 | + * @return collection |
|
| 69 | + */ |
|
| 70 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 71 | + { |
|
| 72 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 73 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 74 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Count all records based on the given condition from storage. |
|
| 79 | + * |
|
| 80 | + * @param array $conditions array of conditions |
|
| 81 | + * @return collection |
|
| 82 | + */ |
|
| 83 | + public function count($conditions) |
|
| 84 | + { |
|
| 85 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 86 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Save the given model to the storage. |
|
| 91 | - * |
|
| 92 | - * @param array $data |
|
| 93 | - * @return mixed |
|
| 94 | - */ |
|
| 95 | - public function save(array $data) |
|
| 96 | - { |
|
| 97 | - $local = \Session::get('locale'); |
|
| 98 | - \Session::put('locale', 'all'); |
|
| 99 | - $model = false; |
|
| 100 | - $relations = []; |
|
| 101 | - |
|
| 102 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 89 | + /** |
|
| 90 | + * Save the given model to the storage. |
|
| 91 | + * |
|
| 92 | + * @param array $data |
|
| 93 | + * @return mixed |
|
| 94 | + */ |
|
| 95 | + public function save(array $data) |
|
| 96 | + { |
|
| 97 | + $local = \Session::get('locale'); |
|
| 98 | + \Session::put('locale', 'all'); |
|
| 99 | + $model = false; |
|
| 100 | + $relations = []; |
|
| 101 | + |
|
| 102 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
| 103 | 103 | |
| 104 | - $model = $this->prepareModel($data); |
|
| 105 | - $relations = $this->prepareRelations($data, $model); |
|
| 106 | - $model = $this->saveModel($model, $relations); |
|
| 107 | - }); |
|
| 108 | - \Session::put('locale', $local); |
|
| 104 | + $model = $this->prepareModel($data); |
|
| 105 | + $relations = $this->prepareRelations($data, $model); |
|
| 106 | + $model = $this->saveModel($model, $relations); |
|
| 107 | + }); |
|
| 108 | + \Session::put('locale', $local); |
|
| 109 | 109 | |
| 110 | - if (count($relations)) { |
|
| 111 | - $model->load(...array_keys($relations)); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $model; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Delete record from the storage based on the given |
|
| 119 | - * condition. |
|
| 120 | - * |
|
| 121 | - * @param var $value condition value |
|
| 122 | - * @param string $attribute condition column name |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function delete($value, $attribute = 'id') |
|
| 126 | - { |
|
| 127 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 128 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 129 | - $model->delete(); |
|
| 130 | - }); |
|
| 131 | - }); |
|
| 132 | - } |
|
| 110 | + if (count($relations)) { |
|
| 111 | + $model->load(...array_keys($relations)); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $model; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Delete record from the storage based on the given |
|
| 119 | + * condition. |
|
| 120 | + * |
|
| 121 | + * @param var $value condition value |
|
| 122 | + * @param string $attribute condition column name |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function delete($value, $attribute = 'id') |
|
| 126 | + { |
|
| 127 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 128 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 129 | + $model->delete(); |
|
| 130 | + }); |
|
| 131 | + }); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - /** |
|
| 135 | - * Fetch records from the storage based on the given |
|
| 136 | - * id. |
|
| 137 | - * |
|
| 138 | - * @param integer $id |
|
| 139 | - * @param string[] $relations |
|
| 140 | - * @param array $columns |
|
| 141 | - * @return object |
|
| 142 | - */ |
|
| 143 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 144 | - { |
|
| 145 | - return $this->model->with($relations)->find($id, $columns); |
|
| 146 | - } |
|
| 134 | + /** |
|
| 135 | + * Fetch records from the storage based on the given |
|
| 136 | + * id. |
|
| 137 | + * |
|
| 138 | + * @param integer $id |
|
| 139 | + * @param string[] $relations |
|
| 140 | + * @param array $columns |
|
| 141 | + * @return object |
|
| 142 | + */ |
|
| 143 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 144 | + { |
|
| 145 | + return $this->model->with($relations)->find($id, $columns); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * Fetch records from the storage based on the given |
|
| 150 | - * condition. |
|
| 151 | - * |
|
| 152 | - * @param array $conditions array of conditions |
|
| 153 | - * @param array $relations |
|
| 154 | - * @param string $sortBy |
|
| 155 | - * @param boolean $desc |
|
| 156 | - * @param array $columns |
|
| 157 | - * @return collection |
|
| 158 | - */ |
|
| 159 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 160 | - { |
|
| 161 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 162 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 163 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Fetch the first record from the storage based on the given |
|
| 168 | - * condition. |
|
| 169 | - * |
|
| 170 | - * @param array $conditions array of conditions |
|
| 171 | - * @param array $relations |
|
| 172 | - * @param array $columns |
|
| 173 | - * @return object |
|
| 174 | - */ |
|
| 175 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 176 | - { |
|
| 177 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 178 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Return the deleted models in pages based on the given conditions. |
|
| 183 | - * |
|
| 184 | - * @param array $conditions array of conditions |
|
| 185 | - * @param integer $perPage |
|
| 186 | - * @param string $sortBy |
|
| 187 | - * @param boolean $desc |
|
| 188 | - * @param array $columns |
|
| 189 | - * @return collection |
|
| 190 | - */ |
|
| 191 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 192 | - { |
|
| 193 | - unset($conditions['page']); |
|
| 194 | - unset($conditions['perPage']); |
|
| 195 | - unset($conditions['sortBy']); |
|
| 196 | - unset($conditions['sort']); |
|
| 197 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 198 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 199 | - $model = $this->model->onlyTrashed(); |
|
| 200 | - |
|
| 201 | - if (count($conditions['conditionValues'])) { |
|
| 202 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Restore the deleted model. |
|
| 210 | - * |
|
| 211 | - * @param integer $id |
|
| 212 | - * @return void |
|
| 213 | - */ |
|
| 214 | - public function restore($id) |
|
| 215 | - { |
|
| 216 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 217 | - |
|
| 218 | - if (! $model) { |
|
| 219 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $model->restore(); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Fill the model with the given data. |
|
| 227 | - * |
|
| 228 | - * @param array $data |
|
| 229 | - * |
|
| 230 | - * @return object |
|
| 231 | - */ |
|
| 232 | - public function prepareModel($data) |
|
| 233 | - { |
|
| 234 | - $modelClass = $this->model; |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * If the id is present in the data then select the model for updating, |
|
| 238 | - * else create new model. |
|
| 239 | - * @var array |
|
| 240 | - */ |
|
| 241 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 242 | - if (! $model) { |
|
| 243 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Construct the model object with the given data, |
|
| 248 | - * and if there is a relation add it to relations array, |
|
| 249 | - * then save the model. |
|
| 250 | - */ |
|
| 251 | - foreach ($data as $key => $value) { |
|
| 252 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 253 | - /** |
|
| 254 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 255 | - */ |
|
| 256 | - $model->$key = $value; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return $model; |
|
| 261 | - } |
|
| 148 | + /** |
|
| 149 | + * Fetch records from the storage based on the given |
|
| 150 | + * condition. |
|
| 151 | + * |
|
| 152 | + * @param array $conditions array of conditions |
|
| 153 | + * @param array $relations |
|
| 154 | + * @param string $sortBy |
|
| 155 | + * @param boolean $desc |
|
| 156 | + * @param array $columns |
|
| 157 | + * @return collection |
|
| 158 | + */ |
|
| 159 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 160 | + { |
|
| 161 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 162 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 163 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Fetch the first record from the storage based on the given |
|
| 168 | + * condition. |
|
| 169 | + * |
|
| 170 | + * @param array $conditions array of conditions |
|
| 171 | + * @param array $relations |
|
| 172 | + * @param array $columns |
|
| 173 | + * @return object |
|
| 174 | + */ |
|
| 175 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 176 | + { |
|
| 177 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 178 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Return the deleted models in pages based on the given conditions. |
|
| 183 | + * |
|
| 184 | + * @param array $conditions array of conditions |
|
| 185 | + * @param integer $perPage |
|
| 186 | + * @param string $sortBy |
|
| 187 | + * @param boolean $desc |
|
| 188 | + * @param array $columns |
|
| 189 | + * @return collection |
|
| 190 | + */ |
|
| 191 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 192 | + { |
|
| 193 | + unset($conditions['page']); |
|
| 194 | + unset($conditions['perPage']); |
|
| 195 | + unset($conditions['sortBy']); |
|
| 196 | + unset($conditions['sort']); |
|
| 197 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 198 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 199 | + $model = $this->model->onlyTrashed(); |
|
| 200 | + |
|
| 201 | + if (count($conditions['conditionValues'])) { |
|
| 202 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Restore the deleted model. |
|
| 210 | + * |
|
| 211 | + * @param integer $id |
|
| 212 | + * @return void |
|
| 213 | + */ |
|
| 214 | + public function restore($id) |
|
| 215 | + { |
|
| 216 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 217 | + |
|
| 218 | + if (! $model) { |
|
| 219 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $model->restore(); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Fill the model with the given data. |
|
| 227 | + * |
|
| 228 | + * @param array $data |
|
| 229 | + * |
|
| 230 | + * @return object |
|
| 231 | + */ |
|
| 232 | + public function prepareModel($data) |
|
| 233 | + { |
|
| 234 | + $modelClass = $this->model; |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * If the id is present in the data then select the model for updating, |
|
| 238 | + * else create new model. |
|
| 239 | + * @var array |
|
| 240 | + */ |
|
| 241 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 242 | + if (! $model) { |
|
| 243 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Construct the model object with the given data, |
|
| 248 | + * and if there is a relation add it to relations array, |
|
| 249 | + * then save the model. |
|
| 250 | + */ |
|
| 251 | + foreach ($data as $key => $value) { |
|
| 252 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 253 | + /** |
|
| 254 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 255 | + */ |
|
| 256 | + $model->$key = $value; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return $model; |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - /** |
|
| 264 | - * Prepare related models based on the given data for the given model. |
|
| 265 | - * |
|
| 266 | - * @param array $data |
|
| 267 | - * @param object $model |
|
| 268 | - * |
|
| 269 | - * @return array |
|
| 270 | - */ |
|
| 271 | - public function prepareRelations($data, $model) |
|
| 272 | - { |
|
| 273 | - /** |
|
| 274 | - * Init the relation array |
|
| 275 | - * |
|
| 276 | - * @var array |
|
| 277 | - */ |
|
| 278 | - $relations = []; |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Construct the model object with the given data, |
|
| 282 | - * and if there is a relation add it to relations array, |
|
| 283 | - * then save the model. |
|
| 284 | - */ |
|
| 285 | - foreach ($data as $key => $value) { |
|
| 286 | - /** |
|
| 287 | - * If the attribute is a relation. |
|
| 288 | - */ |
|
| 289 | - $relation = \Str::camel($key); |
|
| 290 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 291 | - /** |
|
| 292 | - * Check if the relation is a collection. |
|
| 293 | - */ |
|
| 294 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 295 | - /** |
|
| 296 | - * If the relation has no value then marke the relation data |
|
| 297 | - * related to the model to be deleted. |
|
| 298 | - */ |
|
| 299 | - if (! $value || ! count($value)) { |
|
| 300 | - $relations[$relation] = 'delete'; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - if (is_array($value)) { |
|
| 304 | - /** |
|
| 305 | - * Loop through the relation data. |
|
| 306 | - */ |
|
| 307 | - foreach ($value as $attr => $val) { |
|
| 308 | - /** |
|
| 309 | - * Get the relation model. |
|
| 310 | - */ |
|
| 311 | - $relationBaseModel = \Core::$relation()->model; |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Check if the relation is a collection. |
|
| 315 | - */ |
|
| 316 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 317 | - if (! is_array($val)) { |
|
| 318 | - $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
| 319 | - } else { |
|
| 320 | - /** |
|
| 321 | - * If the id is present in the data then select the relation model for updating, |
|
| 322 | - * else create new model. |
|
| 323 | - */ |
|
| 324 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * If model doesn't exists. |
|
| 329 | - */ |
|
| 330 | - if (! $relationModel) { |
|
| 331 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - if (is_array($val)) { |
|
| 335 | - /** |
|
| 336 | - * Loop through the relation attributes. |
|
| 337 | - */ |
|
| 338 | - foreach ($val as $attr => $val) { |
|
| 339 | - /** |
|
| 340 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 341 | - */ |
|
| 342 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 343 | - $relationModel->$attr = $val; |
|
| 344 | - } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 345 | - $extra[$attr] = $val; |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - if (isset($extra)) { |
|
| 351 | - $relationModel->extra = $extra; |
|
| 352 | - } |
|
| 353 | - $relations[$relation][] = $relationModel; |
|
| 354 | - } else { |
|
| 355 | - /** |
|
| 356 | - * Prevent the sub relations. |
|
| 357 | - */ |
|
| 358 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 359 | - /** |
|
| 360 | - * If the id is present in the data then select the relation model for updating, |
|
| 361 | - * else create new model. |
|
| 362 | - */ |
|
| 363 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * If model doesn't exists. |
|
| 367 | - */ |
|
| 368 | - if (! $relationModel) { |
|
| 369 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 373 | - /** |
|
| 374 | - * Prevent attributes not in the fillable. |
|
| 375 | - */ |
|
| 376 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 377 | - $relationModel->$relationAttribute = $relationValue; |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - $relations[$relation] = $relationModel; |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return $relations; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Save the model with related models. |
|
| 394 | - * |
|
| 395 | - * @param object $model |
|
| 396 | - * @param array $relations |
|
| 397 | - * |
|
| 398 | - * @return object |
|
| 399 | - */ |
|
| 400 | - public function saveModel($model, $relations) |
|
| 401 | - { |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Loop through the relations array. |
|
| 405 | - */ |
|
| 406 | - foreach ($relations as $key => $value) { |
|
| 407 | - /** |
|
| 408 | - * If the relation is marked for delete then delete it. |
|
| 409 | - */ |
|
| 410 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 411 | - $model->$key()->delete(); |
|
| 412 | - } elseif (gettype($value) == 'array') { |
|
| 413 | - /** |
|
| 414 | - * Save the model. |
|
| 415 | - */ |
|
| 416 | - $model->save(); |
|
| 417 | - $ids = []; |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Loop through the relations. |
|
| 421 | - */ |
|
| 422 | - foreach ($value as $val) { |
|
| 423 | - switch (class_basename($model->$key())) { |
|
| 424 | - /** |
|
| 425 | - * If the relation is one to many then update it's foreign key with |
|
| 426 | - * the model id and save it then add its id to ids array to delete all |
|
| 427 | - * relations who's id isn't in the ids array. |
|
| 428 | - */ |
|
| 429 | - case 'HasMany': |
|
| 430 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 431 | - $val->$foreignKeyName = $model->id; |
|
| 432 | - $val->save(); |
|
| 433 | - $ids[] = $val->id; |
|
| 434 | - break; |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * If the relation is many to many then add it's id to the ids array to |
|
| 438 | - * attache these ids to the model. |
|
| 439 | - */ |
|
| 440 | - case 'BelongsToMany': |
|
| 441 | - $extra = $val->extra; |
|
| 442 | - unset($val->extra); |
|
| 443 | - $val->save(); |
|
| 444 | - $ids[$val->id] = $extra ?? []; |
|
| 445 | - break; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - switch (class_basename($model->$key())) { |
|
| 449 | - /** |
|
| 450 | - * If the relation is one to many then delete all |
|
| 451 | - * relations who's id isn't in the ids array. |
|
| 452 | - */ |
|
| 453 | - case 'HasMany': |
|
| 454 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 455 | - break; |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * If the relation is many to many then |
|
| 459 | - * detach the previous data and attach |
|
| 460 | - * the ids array to the model. |
|
| 461 | - */ |
|
| 462 | - case 'BelongsToMany': |
|
| 463 | - $model->$key()->detach(); |
|
| 464 | - $model->$key()->attach($ids); |
|
| 465 | - break; |
|
| 466 | - } |
|
| 467 | - } else { |
|
| 468 | - switch (class_basename($model->$key())) { |
|
| 469 | - /** |
|
| 470 | - * If the relation is one to one. |
|
| 471 | - */ |
|
| 472 | - case 'HasOne': |
|
| 473 | - /** |
|
| 474 | - * Save the model. |
|
| 475 | - */ |
|
| 476 | - $model->save(); |
|
| 477 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 478 | - $value->$foreignKeyName = $model->id; |
|
| 479 | - $value->save(); |
|
| 480 | - break; |
|
| 481 | - case 'BelongsTo': |
|
| 482 | - /** |
|
| 483 | - * Save the model. |
|
| 484 | - */ |
|
| 485 | - $value->save(); |
|
| 486 | - $model->$key()->associate($value); |
|
| 487 | - break; |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Save the model. |
|
| 494 | - */ |
|
| 495 | - $model->save(); |
|
| 496 | - |
|
| 497 | - return $model; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Build the conditions recursively for the retrieving methods. |
|
| 502 | - * @param array $conditions |
|
| 503 | - * @return array |
|
| 504 | - */ |
|
| 505 | - protected function constructConditions($conditions, $model) |
|
| 506 | - { |
|
| 507 | - $conditionString = ''; |
|
| 508 | - $conditionValues = []; |
|
| 509 | - foreach ($conditions as $key => $value) { |
|
| 510 | - if (Str::contains($key, '->')) { |
|
| 511 | - $key = $this->wrapJsonSelector($key); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - if ($key == 'and') { |
|
| 515 | - $conditions = $this->constructConditions($value, $model); |
|
| 516 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 517 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 518 | - } elseif ($key == 'or') { |
|
| 519 | - $conditions = $this->constructConditions($value, $model); |
|
| 520 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 521 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 522 | - } else { |
|
| 523 | - if (is_array($value)) { |
|
| 524 | - $operator = $value['op']; |
|
| 525 | - if (strtolower($operator) == 'between') { |
|
| 526 | - $value1 = $value['val1']; |
|
| 527 | - $value2 = $value['val2']; |
|
| 528 | - } else { |
|
| 529 | - $value = Arr::get($value, 'val', ''); |
|
| 530 | - } |
|
| 531 | - } else { |
|
| 532 | - $operator = '='; |
|
| 533 | - } |
|
| 263 | + /** |
|
| 264 | + * Prepare related models based on the given data for the given model. |
|
| 265 | + * |
|
| 266 | + * @param array $data |
|
| 267 | + * @param object $model |
|
| 268 | + * |
|
| 269 | + * @return array |
|
| 270 | + */ |
|
| 271 | + public function prepareRelations($data, $model) |
|
| 272 | + { |
|
| 273 | + /** |
|
| 274 | + * Init the relation array |
|
| 275 | + * |
|
| 276 | + * @var array |
|
| 277 | + */ |
|
| 278 | + $relations = []; |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Construct the model object with the given data, |
|
| 282 | + * and if there is a relation add it to relations array, |
|
| 283 | + * then save the model. |
|
| 284 | + */ |
|
| 285 | + foreach ($data as $key => $value) { |
|
| 286 | + /** |
|
| 287 | + * If the attribute is a relation. |
|
| 288 | + */ |
|
| 289 | + $relation = \Str::camel($key); |
|
| 290 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 291 | + /** |
|
| 292 | + * Check if the relation is a collection. |
|
| 293 | + */ |
|
| 294 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 295 | + /** |
|
| 296 | + * If the relation has no value then marke the relation data |
|
| 297 | + * related to the model to be deleted. |
|
| 298 | + */ |
|
| 299 | + if (! $value || ! count($value)) { |
|
| 300 | + $relations[$relation] = 'delete'; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + if (is_array($value)) { |
|
| 304 | + /** |
|
| 305 | + * Loop through the relation data. |
|
| 306 | + */ |
|
| 307 | + foreach ($value as $attr => $val) { |
|
| 308 | + /** |
|
| 309 | + * Get the relation model. |
|
| 310 | + */ |
|
| 311 | + $relationBaseModel = \Core::$relation()->model; |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Check if the relation is a collection. |
|
| 315 | + */ |
|
| 316 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 317 | + if (! is_array($val)) { |
|
| 318 | + $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
| 319 | + } else { |
|
| 320 | + /** |
|
| 321 | + * If the id is present in the data then select the relation model for updating, |
|
| 322 | + * else create new model. |
|
| 323 | + */ |
|
| 324 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * If model doesn't exists. |
|
| 329 | + */ |
|
| 330 | + if (! $relationModel) { |
|
| 331 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + if (is_array($val)) { |
|
| 335 | + /** |
|
| 336 | + * Loop through the relation attributes. |
|
| 337 | + */ |
|
| 338 | + foreach ($val as $attr => $val) { |
|
| 339 | + /** |
|
| 340 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 341 | + */ |
|
| 342 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 343 | + $relationModel->$attr = $val; |
|
| 344 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
| 345 | + $extra[$attr] = $val; |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + if (isset($extra)) { |
|
| 351 | + $relationModel->extra = $extra; |
|
| 352 | + } |
|
| 353 | + $relations[$relation][] = $relationModel; |
|
| 354 | + } else { |
|
| 355 | + /** |
|
| 356 | + * Prevent the sub relations. |
|
| 357 | + */ |
|
| 358 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 359 | + /** |
|
| 360 | + * If the id is present in the data then select the relation model for updating, |
|
| 361 | + * else create new model. |
|
| 362 | + */ |
|
| 363 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * If model doesn't exists. |
|
| 367 | + */ |
|
| 368 | + if (! $relationModel) { |
|
| 369 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 373 | + /** |
|
| 374 | + * Prevent attributes not in the fillable. |
|
| 375 | + */ |
|
| 376 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 377 | + $relationModel->$relationAttribute = $relationValue; |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + $relations[$relation] = $relationModel; |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return $relations; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Save the model with related models. |
|
| 394 | + * |
|
| 395 | + * @param object $model |
|
| 396 | + * @param array $relations |
|
| 397 | + * |
|
| 398 | + * @return object |
|
| 399 | + */ |
|
| 400 | + public function saveModel($model, $relations) |
|
| 401 | + { |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Loop through the relations array. |
|
| 405 | + */ |
|
| 406 | + foreach ($relations as $key => $value) { |
|
| 407 | + /** |
|
| 408 | + * If the relation is marked for delete then delete it. |
|
| 409 | + */ |
|
| 410 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 411 | + $model->$key()->delete(); |
|
| 412 | + } elseif (gettype($value) == 'array') { |
|
| 413 | + /** |
|
| 414 | + * Save the model. |
|
| 415 | + */ |
|
| 416 | + $model->save(); |
|
| 417 | + $ids = []; |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Loop through the relations. |
|
| 421 | + */ |
|
| 422 | + foreach ($value as $val) { |
|
| 423 | + switch (class_basename($model->$key())) { |
|
| 424 | + /** |
|
| 425 | + * If the relation is one to many then update it's foreign key with |
|
| 426 | + * the model id and save it then add its id to ids array to delete all |
|
| 427 | + * relations who's id isn't in the ids array. |
|
| 428 | + */ |
|
| 429 | + case 'HasMany': |
|
| 430 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 431 | + $val->$foreignKeyName = $model->id; |
|
| 432 | + $val->save(); |
|
| 433 | + $ids[] = $val->id; |
|
| 434 | + break; |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * If the relation is many to many then add it's id to the ids array to |
|
| 438 | + * attache these ids to the model. |
|
| 439 | + */ |
|
| 440 | + case 'BelongsToMany': |
|
| 441 | + $extra = $val->extra; |
|
| 442 | + unset($val->extra); |
|
| 443 | + $val->save(); |
|
| 444 | + $ids[$val->id] = $extra ?? []; |
|
| 445 | + break; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + switch (class_basename($model->$key())) { |
|
| 449 | + /** |
|
| 450 | + * If the relation is one to many then delete all |
|
| 451 | + * relations who's id isn't in the ids array. |
|
| 452 | + */ |
|
| 453 | + case 'HasMany': |
|
| 454 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 455 | + break; |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * If the relation is many to many then |
|
| 459 | + * detach the previous data and attach |
|
| 460 | + * the ids array to the model. |
|
| 461 | + */ |
|
| 462 | + case 'BelongsToMany': |
|
| 463 | + $model->$key()->detach(); |
|
| 464 | + $model->$key()->attach($ids); |
|
| 465 | + break; |
|
| 466 | + } |
|
| 467 | + } else { |
|
| 468 | + switch (class_basename($model->$key())) { |
|
| 469 | + /** |
|
| 470 | + * If the relation is one to one. |
|
| 471 | + */ |
|
| 472 | + case 'HasOne': |
|
| 473 | + /** |
|
| 474 | + * Save the model. |
|
| 475 | + */ |
|
| 476 | + $model->save(); |
|
| 477 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 478 | + $value->$foreignKeyName = $model->id; |
|
| 479 | + $value->save(); |
|
| 480 | + break; |
|
| 481 | + case 'BelongsTo': |
|
| 482 | + /** |
|
| 483 | + * Save the model. |
|
| 484 | + */ |
|
| 485 | + $value->save(); |
|
| 486 | + $model->$key()->associate($value); |
|
| 487 | + break; |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Save the model. |
|
| 494 | + */ |
|
| 495 | + $model->save(); |
|
| 496 | + |
|
| 497 | + return $model; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Build the conditions recursively for the retrieving methods. |
|
| 502 | + * @param array $conditions |
|
| 503 | + * @return array |
|
| 504 | + */ |
|
| 505 | + protected function constructConditions($conditions, $model) |
|
| 506 | + { |
|
| 507 | + $conditionString = ''; |
|
| 508 | + $conditionValues = []; |
|
| 509 | + foreach ($conditions as $key => $value) { |
|
| 510 | + if (Str::contains($key, '->')) { |
|
| 511 | + $key = $this->wrapJsonSelector($key); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + if ($key == 'and') { |
|
| 515 | + $conditions = $this->constructConditions($value, $model); |
|
| 516 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 517 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 518 | + } elseif ($key == 'or') { |
|
| 519 | + $conditions = $this->constructConditions($value, $model); |
|
| 520 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 521 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 522 | + } else { |
|
| 523 | + if (is_array($value)) { |
|
| 524 | + $operator = $value['op']; |
|
| 525 | + if (strtolower($operator) == 'between') { |
|
| 526 | + $value1 = $value['val1']; |
|
| 527 | + $value2 = $value['val2']; |
|
| 528 | + } else { |
|
| 529 | + $value = Arr::get($value, 'val', ''); |
|
| 530 | + } |
|
| 531 | + } else { |
|
| 532 | + $operator = '='; |
|
| 533 | + } |
|
| 534 | 534 | |
| 535 | - if (strtolower($operator) == 'between') { |
|
| 536 | - $conditionString .= $key.' >= ? and '; |
|
| 537 | - $conditionValues[] = $value1; |
|
| 538 | - |
|
| 539 | - $conditionString .= $key.' <= ? {op} '; |
|
| 540 | - $conditionValues[] = $value2; |
|
| 541 | - } elseif (strtolower($operator) == 'in') { |
|
| 542 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 543 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 544 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 545 | - } elseif (strtolower($operator) == 'null') { |
|
| 546 | - $conditionString .= $key.' is null {op} '; |
|
| 547 | - } elseif (strtolower($operator) == 'not null') { |
|
| 548 | - $conditionString .= $key.' is not null {op} '; |
|
| 549 | - } elseif (strtolower($operator) == 'has') { |
|
| 550 | - $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
| 551 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
| 552 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 553 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
| 554 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
| 555 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 556 | - } else { |
|
| 557 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 558 | - $conditionValues[] = $value; |
|
| 559 | - } |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 563 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Wrap the given JSON selector. |
|
| 568 | - * |
|
| 569 | - * @param string $value |
|
| 570 | - * @return string |
|
| 571 | - */ |
|
| 572 | - protected function wrapJsonSelector($value) |
|
| 573 | - { |
|
| 574 | - $removeLast = strpos($value, ')'); |
|
| 575 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 576 | - $path = explode('->', $value); |
|
| 577 | - $field = array_shift($path); |
|
| 578 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 579 | - return '"'.$part.'"'; |
|
| 580 | - })->implode('.')); |
|
| 535 | + if (strtolower($operator) == 'between') { |
|
| 536 | + $conditionString .= $key.' >= ? and '; |
|
| 537 | + $conditionValues[] = $value1; |
|
| 538 | + |
|
| 539 | + $conditionString .= $key.' <= ? {op} '; |
|
| 540 | + $conditionValues[] = $value2; |
|
| 541 | + } elseif (strtolower($operator) == 'in') { |
|
| 542 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 543 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 544 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 545 | + } elseif (strtolower($operator) == 'null') { |
|
| 546 | + $conditionString .= $key.' is null {op} '; |
|
| 547 | + } elseif (strtolower($operator) == 'not null') { |
|
| 548 | + $conditionString .= $key.' is not null {op} '; |
|
| 549 | + } elseif (strtolower($operator) == 'has') { |
|
| 550 | + $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
| 551 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
| 552 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 553 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
| 554 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
| 555 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 556 | + } else { |
|
| 557 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 558 | + $conditionValues[] = $value; |
|
| 559 | + } |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 563 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Wrap the given JSON selector. |
|
| 568 | + * |
|
| 569 | + * @param string $value |
|
| 570 | + * @return string |
|
| 571 | + */ |
|
| 572 | + protected function wrapJsonSelector($value) |
|
| 573 | + { |
|
| 574 | + $removeLast = strpos($value, ')'); |
|
| 575 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 576 | + $path = explode('->', $value); |
|
| 577 | + $field = array_shift($path); |
|
| 578 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 579 | + return '"'.$part.'"'; |
|
| 580 | + })->implode('.')); |
|
| 581 | 581 | |
| 582 | - return $removeLast === false ? $result : $result.')'; |
|
| 583 | - } |
|
| 582 | + return $removeLast === false ? $result : $result.')'; |
|
| 583 | + } |
|
| 584 | 584 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @param array $relations |
| 54 | 54 | * @param string $sortBy |
| 55 | - * @param boolean $desc |
|
| 55 | + * @param integer $desc |
|
| 56 | 56 | * @param array $columns |
| 57 | 57 | * @return collection |
| 58 | 58 | */ |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * @param integer $perPage |
| 68 | 68 | * @param array $relations |
| 69 | 69 | * @param string $sortBy |
| 70 | - * @param boolean $desc |
|
| 70 | + * @param integer $desc |
|
| 71 | 71 | * @param array $columns |
| 72 | 72 | * @return collection |
| 73 | 73 | */ |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * @param integer $perPage |
| 85 | 85 | * @param array $relations |
| 86 | 86 | * @param string $sortBy |
| 87 | - * @param boolean $desc |
|
| 87 | + * @param integer $desc |
|
| 88 | 88 | * @param array $columns |
| 89 | 89 | * @return collection |
| 90 | 90 | */ |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @param array $conditions array of conditions |
| 139 | 139 | * @param array $relations |
| 140 | 140 | * @param string $sortBy |
| 141 | - * @param boolean $desc |
|
| 141 | + * @param integer $desc |
|
| 142 | 142 | * @param array $columns |
| 143 | 143 | * @return collection |
| 144 | 144 | */ |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @param array $conditions array of conditions |
| 168 | 168 | * @param integer $perPage |
| 169 | 169 | * @param string $sortBy |
| 170 | - * @param boolean $desc |
|
| 170 | + * @param integer $desc |
|
| 171 | 171 | * @param array $columns |
| 172 | 172 | * @return collection |
| 173 | 173 | */ |
@@ -6,223 +6,223 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseService implements BaseServiceInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var object |
|
| 11 | - */ |
|
| 12 | - public $repo; |
|
| 9 | + /** |
|
| 10 | + * @var object |
|
| 11 | + */ |
|
| 12 | + public $repo; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Init new object. |
|
| 16 | - * |
|
| 17 | - * @param mixed $repo |
|
| 18 | - * @return void |
|
| 19 | - */ |
|
| 20 | - public function __construct($repo) |
|
| 21 | - { |
|
| 22 | - $this->repo = $repo; |
|
| 23 | - } |
|
| 14 | + /** |
|
| 15 | + * Init new object. |
|
| 16 | + * |
|
| 17 | + * @param mixed $repo |
|
| 18 | + * @return void |
|
| 19 | + */ |
|
| 20 | + public function __construct($repo) |
|
| 21 | + { |
|
| 22 | + $this->repo = $repo; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Fetch records with relations based on the given params. |
|
| 27 | - * |
|
| 28 | - * @param array $relations |
|
| 29 | - * @param array $conditions |
|
| 30 | - * @param integer $perPage |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @param boolean $trashed |
|
| 34 | - * @return collection |
|
| 35 | - */ |
|
| 36 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 37 | - { |
|
| 38 | - $translatable = $this->repo->model->translatable ?? []; |
|
| 39 | - $filters = $this->constructFilters($conditions); |
|
| 40 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 25 | + /** |
|
| 26 | + * Fetch records with relations based on the given params. |
|
| 27 | + * |
|
| 28 | + * @param array $relations |
|
| 29 | + * @param array $conditions |
|
| 30 | + * @param integer $perPage |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @param boolean $trashed |
|
| 34 | + * @return collection |
|
| 35 | + */ |
|
| 36 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
| 37 | + { |
|
| 38 | + $translatable = $this->repo->model->translatable ?? []; |
|
| 39 | + $filters = $this->constructFilters($conditions); |
|
| 40 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 41 | 41 | |
| 42 | - if ($trashed) { |
|
| 43 | - return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 44 | - } |
|
| 42 | + if ($trashed) { |
|
| 43 | + return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - if (count($filters)) { |
|
| 47 | - return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 48 | - } |
|
| 46 | + if (count($filters)) { |
|
| 47 | + return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 51 | - } |
|
| 50 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Fetch all records with relations from the storage. |
|
| 55 | - * |
|
| 56 | - * @param array $relations |
|
| 57 | - * @param string $sortBy |
|
| 58 | - * @param boolean $desc |
|
| 59 | - * @param array $columns |
|
| 60 | - * @return collection |
|
| 61 | - */ |
|
| 62 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 63 | - { |
|
| 64 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 65 | - } |
|
| 53 | + /** |
|
| 54 | + * Fetch all records with relations from the storage. |
|
| 55 | + * |
|
| 56 | + * @param array $relations |
|
| 57 | + * @param string $sortBy |
|
| 58 | + * @param boolean $desc |
|
| 59 | + * @param array $columns |
|
| 60 | + * @return collection |
|
| 61 | + */ |
|
| 62 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 63 | + { |
|
| 64 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Fetch all records with relations from storage in pages. |
|
| 69 | - * |
|
| 70 | - * @param integer $perPage |
|
| 71 | - * @param array $relations |
|
| 72 | - * @param string $sortBy |
|
| 73 | - * @param boolean $desc |
|
| 74 | - * @param array $columns |
|
| 75 | - * @return collection |
|
| 76 | - */ |
|
| 77 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 78 | - { |
|
| 79 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 80 | - } |
|
| 67 | + /** |
|
| 68 | + * Fetch all records with relations from storage in pages. |
|
| 69 | + * |
|
| 70 | + * @param integer $perPage |
|
| 71 | + * @param array $relations |
|
| 72 | + * @param string $sortBy |
|
| 73 | + * @param boolean $desc |
|
| 74 | + * @param array $columns |
|
| 75 | + * @return collection |
|
| 76 | + */ |
|
| 77 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 78 | + { |
|
| 79 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Fetch all records with relations based on |
|
| 84 | - * the given condition from storage in pages. |
|
| 85 | - * |
|
| 86 | - * @param array $conditions array of conditions |
|
| 87 | - * @param integer $perPage |
|
| 88 | - * @param array $relations |
|
| 89 | - * @param string $sortBy |
|
| 90 | - * @param boolean $desc |
|
| 91 | - * @param array $columns |
|
| 92 | - * @return collection |
|
| 93 | - */ |
|
| 94 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 95 | - { |
|
| 96 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 97 | - } |
|
| 82 | + /** |
|
| 83 | + * Fetch all records with relations based on |
|
| 84 | + * the given condition from storage in pages. |
|
| 85 | + * |
|
| 86 | + * @param array $conditions array of conditions |
|
| 87 | + * @param integer $perPage |
|
| 88 | + * @param array $relations |
|
| 89 | + * @param string $sortBy |
|
| 90 | + * @param boolean $desc |
|
| 91 | + * @param array $columns |
|
| 92 | + * @return collection |
|
| 93 | + */ |
|
| 94 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 95 | + { |
|
| 96 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Save the given model to the storage. |
|
| 101 | - * |
|
| 102 | - * @param array $data |
|
| 103 | - * @return mixed |
|
| 104 | - */ |
|
| 105 | - public function save(array $data) |
|
| 106 | - { |
|
| 107 | - return $this->repo->save($data); |
|
| 108 | - } |
|
| 99 | + /** |
|
| 100 | + * Save the given model to the storage. |
|
| 101 | + * |
|
| 102 | + * @param array $data |
|
| 103 | + * @return mixed |
|
| 104 | + */ |
|
| 105 | + public function save(array $data) |
|
| 106 | + { |
|
| 107 | + return $this->repo->save($data); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Delete record from the storage based on the given |
|
| 112 | - * condition. |
|
| 113 | - * |
|
| 114 | - * @param var $value condition value |
|
| 115 | - * @param string $attribute condition column name |
|
| 116 | - * @return void |
|
| 117 | - */ |
|
| 118 | - public function delete($value, $attribute = 'id') |
|
| 119 | - { |
|
| 120 | - return $this->repo->delete($value, $attribute); |
|
| 121 | - } |
|
| 110 | + /** |
|
| 111 | + * Delete record from the storage based on the given |
|
| 112 | + * condition. |
|
| 113 | + * |
|
| 114 | + * @param var $value condition value |
|
| 115 | + * @param string $attribute condition column name |
|
| 116 | + * @return void |
|
| 117 | + */ |
|
| 118 | + public function delete($value, $attribute = 'id') |
|
| 119 | + { |
|
| 120 | + return $this->repo->delete($value, $attribute); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Fetch records from the storage based on the given |
|
| 125 | - * id. |
|
| 126 | - * |
|
| 127 | - * @param integer $id |
|
| 128 | - * @param array $relations |
|
| 129 | - * @param array $columns |
|
| 130 | - * @return object |
|
| 131 | - */ |
|
| 132 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 133 | - { |
|
| 134 | - return $this->repo->find($id, $relations, $columns); |
|
| 135 | - } |
|
| 123 | + /** |
|
| 124 | + * Fetch records from the storage based on the given |
|
| 125 | + * id. |
|
| 126 | + * |
|
| 127 | + * @param integer $id |
|
| 128 | + * @param array $relations |
|
| 129 | + * @param array $columns |
|
| 130 | + * @return object |
|
| 131 | + */ |
|
| 132 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 133 | + { |
|
| 134 | + return $this->repo->find($id, $relations, $columns); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Fetch records from the storage based on the given |
|
| 139 | - * condition. |
|
| 140 | - * |
|
| 141 | - * @param array $conditions array of conditions |
|
| 142 | - * @param array $relations |
|
| 143 | - * @param string $sortBy |
|
| 144 | - * @param boolean $desc |
|
| 145 | - * @param array $columns |
|
| 146 | - * @return collection |
|
| 147 | - */ |
|
| 148 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 149 | - { |
|
| 150 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 151 | - } |
|
| 137 | + /** |
|
| 138 | + * Fetch records from the storage based on the given |
|
| 139 | + * condition. |
|
| 140 | + * |
|
| 141 | + * @param array $conditions array of conditions |
|
| 142 | + * @param array $relations |
|
| 143 | + * @param string $sortBy |
|
| 144 | + * @param boolean $desc |
|
| 145 | + * @param array $columns |
|
| 146 | + * @return collection |
|
| 147 | + */ |
|
| 148 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 149 | + { |
|
| 150 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Fetch the first record from the storage based on the given |
|
| 155 | - * condition. |
|
| 156 | - * |
|
| 157 | - * @param array $conditions array of conditions |
|
| 158 | - * @param array $relations |
|
| 159 | - * @param array $columns |
|
| 160 | - * @return object |
|
| 161 | - */ |
|
| 162 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 163 | - { |
|
| 164 | - return $this->repo->first($conditions, $relations, $columns); |
|
| 165 | - } |
|
| 153 | + /** |
|
| 154 | + * Fetch the first record from the storage based on the given |
|
| 155 | + * condition. |
|
| 156 | + * |
|
| 157 | + * @param array $conditions array of conditions |
|
| 158 | + * @param array $relations |
|
| 159 | + * @param array $columns |
|
| 160 | + * @return object |
|
| 161 | + */ |
|
| 162 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 163 | + { |
|
| 164 | + return $this->repo->first($conditions, $relations, $columns); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Return the deleted models in pages based on the given conditions. |
|
| 169 | - * |
|
| 170 | - * @param array $conditions array of conditions |
|
| 171 | - * @param integer $perPage |
|
| 172 | - * @param string $sortBy |
|
| 173 | - * @param boolean $desc |
|
| 174 | - * @param array $columns |
|
| 175 | - * @return collection |
|
| 176 | - */ |
|
| 177 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 178 | - { |
|
| 179 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 180 | - } |
|
| 167 | + /** |
|
| 168 | + * Return the deleted models in pages based on the given conditions. |
|
| 169 | + * |
|
| 170 | + * @param array $conditions array of conditions |
|
| 171 | + * @param integer $perPage |
|
| 172 | + * @param string $sortBy |
|
| 173 | + * @param boolean $desc |
|
| 174 | + * @param array $columns |
|
| 175 | + * @return collection |
|
| 176 | + */ |
|
| 177 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 178 | + { |
|
| 179 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Restore the deleted model. |
|
| 184 | - * |
|
| 185 | - * @param integer $id |
|
| 186 | - * @return void |
|
| 187 | - */ |
|
| 188 | - public function restore($id) |
|
| 189 | - { |
|
| 190 | - return $this->repo->restore($id); |
|
| 191 | - } |
|
| 182 | + /** |
|
| 183 | + * Restore the deleted model. |
|
| 184 | + * |
|
| 185 | + * @param integer $id |
|
| 186 | + * @return void |
|
| 187 | + */ |
|
| 188 | + public function restore($id) |
|
| 189 | + { |
|
| 190 | + return $this->repo->restore($id); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * Prepare filters for repo. |
|
| 195 | - * |
|
| 196 | - * @param array $conditions |
|
| 197 | - * @return array |
|
| 198 | - */ |
|
| 199 | - public function constructFilters($conditions) |
|
| 200 | - { |
|
| 201 | - $filters = []; |
|
| 202 | - $translatable = $this->repo->model->translatable ?? []; |
|
| 203 | - foreach ($conditions as $key => $value) { |
|
| 204 | - if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
| 205 | - $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 206 | - if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
| 207 | - $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 208 | - } elseif (! is_array($value) && strpos($key, '_id')) { |
|
| 209 | - $filters[$key] = [ |
|
| 210 | - 'op' => 'in', |
|
| 211 | - 'val' => explode(',', $value) |
|
| 212 | - ]; |
|
| 213 | - } elseif (is_array($value)) { |
|
| 214 | - $filters[$key] = $value; |
|
| 215 | - } elseif($value) { |
|
| 216 | - $key = 'LOWER(' . $key . ')'; |
|
| 217 | - $value = strtolower($value); |
|
| 218 | - $filters[$key] = [ |
|
| 219 | - 'op' => 'like', |
|
| 220 | - 'val' => '%' . $value . '%' |
|
| 221 | - ]; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 193 | + /** |
|
| 194 | + * Prepare filters for repo. |
|
| 195 | + * |
|
| 196 | + * @param array $conditions |
|
| 197 | + * @return array |
|
| 198 | + */ |
|
| 199 | + public function constructFilters($conditions) |
|
| 200 | + { |
|
| 201 | + $filters = []; |
|
| 202 | + $translatable = $this->repo->model->translatable ?? []; |
|
| 203 | + foreach ($conditions as $key => $value) { |
|
| 204 | + if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
| 205 | + $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 206 | + if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
| 207 | + $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 208 | + } elseif (! is_array($value) && strpos($key, '_id')) { |
|
| 209 | + $filters[$key] = [ |
|
| 210 | + 'op' => 'in', |
|
| 211 | + 'val' => explode(',', $value) |
|
| 212 | + ]; |
|
| 213 | + } elseif (is_array($value)) { |
|
| 214 | + $filters[$key] = $value; |
|
| 215 | + } elseif($value) { |
|
| 216 | + $key = 'LOWER(' . $key . ')'; |
|
| 217 | + $value = strtolower($value); |
|
| 218 | + $filters[$key] = [ |
|
| 219 | + 'op' => 'like', |
|
| 220 | + 'val' => '%' . $value . '%' |
|
| 221 | + ]; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - return $filters; |
|
| 227 | - } |
|
| 226 | + return $filters; |
|
| 227 | + } |
|
| 228 | 228 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | { |
| 38 | 38 | $translatable = $this->repo->model->translatable ?? []; |
| 39 | 39 | $filters = $this->constructFilters($conditions); |
| 40 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
| 40 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy.'->'.\Session::get('locale') : $sortBy; |
|
| 41 | 41 | |
| 42 | 42 | if ($trashed) { |
| 43 | 43 | return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
@@ -202,22 +202,22 @@ discard block |
||
| 202 | 202 | $translatable = $this->repo->model->translatable ?? []; |
| 203 | 203 | foreach ($conditions as $key => $value) { |
| 204 | 204 | if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
| 205 | - $key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 205 | + $key = in_array($key, $translatable) ? $key.'->'.(\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
| 206 | 206 | if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
| 207 | 207 | $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
| 208 | - } elseif (! is_array($value) && strpos($key, '_id')) { |
|
| 208 | + } elseif ( ! is_array($value) && strpos($key, '_id')) { |
|
| 209 | 209 | $filters[$key] = [ |
| 210 | 210 | 'op' => 'in', |
| 211 | 211 | 'val' => explode(',', $value) |
| 212 | 212 | ]; |
| 213 | 213 | } elseif (is_array($value)) { |
| 214 | 214 | $filters[$key] = $value; |
| 215 | - } elseif($value) { |
|
| 216 | - $key = 'LOWER(' . $key . ')'; |
|
| 215 | + } elseif ($value) { |
|
| 216 | + $key = 'LOWER('.$key.')'; |
|
| 217 | 217 | $value = strtolower($value); |
| 218 | 218 | $filters[$key] = [ |
| 219 | 219 | 'op' => 'like', |
| 220 | - 'val' => '%' . $value . '%' |
|
| 220 | + 'val' => '%'.$value.'%' |
|
| 221 | 221 | ]; |
| 222 | 222 | } |
| 223 | 223 | } |
@@ -2,93 +2,93 @@ |
||
| 2 | 2 | |
| 3 | 3 | interface BaseRepositoryInterface |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Fetch all records with relations from the storage. |
|
| 7 | - * |
|
| 8 | - * @param array $relations |
|
| 9 | - * @param array $sortBy |
|
| 10 | - * @param array $desc |
|
| 11 | - * @param array $columns |
|
| 12 | - * @return collection |
|
| 13 | - */ |
|
| 14 | - public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 5 | + /** |
|
| 6 | + * Fetch all records with relations from the storage. |
|
| 7 | + * |
|
| 8 | + * @param array $relations |
|
| 9 | + * @param array $sortBy |
|
| 10 | + * @param array $desc |
|
| 11 | + * @param array $columns |
|
| 12 | + * @return collection |
|
| 13 | + */ |
|
| 14 | + public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Fetch all records with relations from storage in pages. |
|
| 18 | - * |
|
| 19 | - * @param integer $perPage |
|
| 20 | - * @param array $relations |
|
| 21 | - * @param array $sortBy |
|
| 22 | - * @param array $desc |
|
| 23 | - * @param array $columns |
|
| 24 | - * @return collection |
|
| 25 | - */ |
|
| 26 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 16 | + /** |
|
| 17 | + * Fetch all records with relations from storage in pages. |
|
| 18 | + * |
|
| 19 | + * @param integer $perPage |
|
| 20 | + * @param array $relations |
|
| 21 | + * @param array $sortBy |
|
| 22 | + * @param array $desc |
|
| 23 | + * @param array $columns |
|
| 24 | + * @return collection |
|
| 25 | + */ |
|
| 26 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Fetch all records with relations based on |
|
| 30 | - * the given condition from storage in pages. |
|
| 31 | - * |
|
| 32 | - * @param array $conditions array of conditions |
|
| 33 | - * @param integer $perPage |
|
| 34 | - * @param array $relations |
|
| 35 | - * @param array $sortBy |
|
| 36 | - * @param array $desc |
|
| 37 | - * @param array $columns |
|
| 38 | - * @return collection |
|
| 39 | - */ |
|
| 40 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 28 | + /** |
|
| 29 | + * Fetch all records with relations based on |
|
| 30 | + * the given condition from storage in pages. |
|
| 31 | + * |
|
| 32 | + * @param array $conditions array of conditions |
|
| 33 | + * @param integer $perPage |
|
| 34 | + * @param array $relations |
|
| 35 | + * @param array $sortBy |
|
| 36 | + * @param array $desc |
|
| 37 | + * @param array $columns |
|
| 38 | + * @return collection |
|
| 39 | + */ |
|
| 40 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Save the given model/models to the storage. |
|
| 44 | - * |
|
| 45 | - * @param array $data |
|
| 46 | - * @return mixed |
|
| 47 | - */ |
|
| 48 | - public function save(array $data); |
|
| 42 | + /** |
|
| 43 | + * Save the given model/models to the storage. |
|
| 44 | + * |
|
| 45 | + * @param array $data |
|
| 46 | + * @return mixed |
|
| 47 | + */ |
|
| 48 | + public function save(array $data); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Delete record from the storage based on the given |
|
| 52 | - * condition. |
|
| 53 | - * |
|
| 54 | - * @param var $value condition value |
|
| 55 | - * @param string $attribute condition column name |
|
| 56 | - * @return integer affected rows |
|
| 57 | - */ |
|
| 58 | - public function delete($value, $attribute = 'id'); |
|
| 50 | + /** |
|
| 51 | + * Delete record from the storage based on the given |
|
| 52 | + * condition. |
|
| 53 | + * |
|
| 54 | + * @param var $value condition value |
|
| 55 | + * @param string $attribute condition column name |
|
| 56 | + * @return integer affected rows |
|
| 57 | + */ |
|
| 58 | + public function delete($value, $attribute = 'id'); |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Fetch records from the storage based on the given |
|
| 62 | - * id. |
|
| 63 | - * |
|
| 64 | - * @param integer $id |
|
| 65 | - * @param array $relations |
|
| 66 | - * @param array $columns |
|
| 67 | - * @return object |
|
| 68 | - */ |
|
| 69 | - public function find($id, $relations = [], $columns = ['*']); |
|
| 60 | + /** |
|
| 61 | + * Fetch records from the storage based on the given |
|
| 62 | + * id. |
|
| 63 | + * |
|
| 64 | + * @param integer $id |
|
| 65 | + * @param array $relations |
|
| 66 | + * @param array $columns |
|
| 67 | + * @return object |
|
| 68 | + */ |
|
| 69 | + public function find($id, $relations = [], $columns = ['*']); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Fetch records from the storage based on the given |
|
| 73 | - * condition. |
|
| 74 | - * |
|
| 75 | - * @param array $conditions array of conditions |
|
| 76 | - * @param array $relations |
|
| 77 | - * @param array $sortBy |
|
| 78 | - * @param array $desc |
|
| 79 | - * @param array $columns |
|
| 80 | - * @return collection |
|
| 81 | - */ |
|
| 82 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 71 | + /** |
|
| 72 | + * Fetch records from the storage based on the given |
|
| 73 | + * condition. |
|
| 74 | + * |
|
| 75 | + * @param array $conditions array of conditions |
|
| 76 | + * @param array $relations |
|
| 77 | + * @param array $sortBy |
|
| 78 | + * @param array $desc |
|
| 79 | + * @param array $columns |
|
| 80 | + * @return collection |
|
| 81 | + */ |
|
| 82 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Fetch the first record fro the storage based on the given |
|
| 86 | - * condition. |
|
| 87 | - * |
|
| 88 | - * @param array $conditions array of conditions |
|
| 89 | - * @param array $relations |
|
| 90 | - * @param array $columns |
|
| 91 | - * @return object |
|
| 92 | - */ |
|
| 93 | - public function first($conditions, $relations = [], $columns = ['*']); |
|
| 84 | + /** |
|
| 85 | + * Fetch the first record fro the storage based on the given |
|
| 86 | + * condition. |
|
| 87 | + * |
|
| 88 | + * @param array $conditions array of conditions |
|
| 89 | + * @param array $relations |
|
| 90 | + * @param array $columns |
|
| 91 | + * @return object |
|
| 92 | + */ |
|
| 93 | + public function first($conditions, $relations = [], $columns = ['*']); |
|
| 94 | 94 | } |
@@ -2,93 +2,93 @@ |
||
| 2 | 2 | |
| 3 | 3 | interface BaseServiceInterface |
| 4 | 4 | { |
| 5 | - /** |
|
| 6 | - * Fetch all records with relations from the storage. |
|
| 7 | - * |
|
| 8 | - * @param array $relations |
|
| 9 | - * @param array $sortBy |
|
| 10 | - * @param array $desc |
|
| 11 | - * @param array $columns |
|
| 12 | - * @return collection |
|
| 13 | - */ |
|
| 14 | - public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 5 | + /** |
|
| 6 | + * Fetch all records with relations from the storage. |
|
| 7 | + * |
|
| 8 | + * @param array $relations |
|
| 9 | + * @param array $sortBy |
|
| 10 | + * @param array $desc |
|
| 11 | + * @param array $columns |
|
| 12 | + * @return collection |
|
| 13 | + */ |
|
| 14 | + public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Fetch all records with relations from storage in pages. |
|
| 18 | - * |
|
| 19 | - * @param integer $perPage |
|
| 20 | - * @param array $relations |
|
| 21 | - * @param array $sortBy |
|
| 22 | - * @param array $desc |
|
| 23 | - * @param array $columns |
|
| 24 | - * @return collection |
|
| 25 | - */ |
|
| 26 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 16 | + /** |
|
| 17 | + * Fetch all records with relations from storage in pages. |
|
| 18 | + * |
|
| 19 | + * @param integer $perPage |
|
| 20 | + * @param array $relations |
|
| 21 | + * @param array $sortBy |
|
| 22 | + * @param array $desc |
|
| 23 | + * @param array $columns |
|
| 24 | + * @return collection |
|
| 25 | + */ |
|
| 26 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Fetch all records with relations based on |
|
| 30 | - * the given condition from storage in pages. |
|
| 31 | - * |
|
| 32 | - * @param array $conditions array of conditions |
|
| 33 | - * @param integer $perPage |
|
| 34 | - * @param array $relations |
|
| 35 | - * @param array $sortBy |
|
| 36 | - * @param array $desc |
|
| 37 | - * @param array $columns |
|
| 38 | - * @return collection |
|
| 39 | - */ |
|
| 40 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 28 | + /** |
|
| 29 | + * Fetch all records with relations based on |
|
| 30 | + * the given condition from storage in pages. |
|
| 31 | + * |
|
| 32 | + * @param array $conditions array of conditions |
|
| 33 | + * @param integer $perPage |
|
| 34 | + * @param array $relations |
|
| 35 | + * @param array $sortBy |
|
| 36 | + * @param array $desc |
|
| 37 | + * @param array $columns |
|
| 38 | + * @return collection |
|
| 39 | + */ |
|
| 40 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Save the given model/models to the storage. |
|
| 44 | - * |
|
| 45 | - * @param array $data |
|
| 46 | - * @return mixed |
|
| 47 | - */ |
|
| 48 | - public function save(array $data); |
|
| 42 | + /** |
|
| 43 | + * Save the given model/models to the storage. |
|
| 44 | + * |
|
| 45 | + * @param array $data |
|
| 46 | + * @return mixed |
|
| 47 | + */ |
|
| 48 | + public function save(array $data); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Delete record from the storage based on the given |
|
| 52 | - * condition. |
|
| 53 | - * |
|
| 54 | - * @param var $value condition value |
|
| 55 | - * @param string $attribute condition column name |
|
| 56 | - * @return integer affected rows |
|
| 57 | - */ |
|
| 58 | - public function delete($value, $attribute = 'id'); |
|
| 50 | + /** |
|
| 51 | + * Delete record from the storage based on the given |
|
| 52 | + * condition. |
|
| 53 | + * |
|
| 54 | + * @param var $value condition value |
|
| 55 | + * @param string $attribute condition column name |
|
| 56 | + * @return integer affected rows |
|
| 57 | + */ |
|
| 58 | + public function delete($value, $attribute = 'id'); |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Fetch records from the storage based on the given |
|
| 62 | - * id. |
|
| 63 | - * |
|
| 64 | - * @param integer $id |
|
| 65 | - * @param array $relations |
|
| 66 | - * @param array $columns |
|
| 67 | - * @return object |
|
| 68 | - */ |
|
| 69 | - public function find($id, $relations = [], $columns = ['*']); |
|
| 60 | + /** |
|
| 61 | + * Fetch records from the storage based on the given |
|
| 62 | + * id. |
|
| 63 | + * |
|
| 64 | + * @param integer $id |
|
| 65 | + * @param array $relations |
|
| 66 | + * @param array $columns |
|
| 67 | + * @return object |
|
| 68 | + */ |
|
| 69 | + public function find($id, $relations = [], $columns = ['*']); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Fetch records from the storage based on the given |
|
| 73 | - * condition. |
|
| 74 | - * |
|
| 75 | - * @param array $conditions array of conditions |
|
| 76 | - * @param array $relations |
|
| 77 | - * @param array $sortBy |
|
| 78 | - * @param array $desc |
|
| 79 | - * @param array $columns |
|
| 80 | - * @return collection |
|
| 81 | - */ |
|
| 82 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 71 | + /** |
|
| 72 | + * Fetch records from the storage based on the given |
|
| 73 | + * condition. |
|
| 74 | + * |
|
| 75 | + * @param array $conditions array of conditions |
|
| 76 | + * @param array $relations |
|
| 77 | + * @param array $sortBy |
|
| 78 | + * @param array $desc |
|
| 79 | + * @param array $columns |
|
| 80 | + * @return collection |
|
| 81 | + */ |
|
| 82 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = ['*']); |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Fetch the first record fro the storage based on the given |
|
| 86 | - * condition. |
|
| 87 | - * |
|
| 88 | - * @param array $conditions array of conditions |
|
| 89 | - * @param array $relations |
|
| 90 | - * @param array $columns |
|
| 91 | - * @return object |
|
| 92 | - */ |
|
| 93 | - public function first($conditions, $relations = [], $columns = ['*']); |
|
| 84 | + /** |
|
| 85 | + * Fetch the first record fro the storage based on the given |
|
| 86 | + * condition. |
|
| 87 | + * |
|
| 88 | + * @param array $conditions array of conditions |
|
| 89 | + * @param array $relations |
|
| 90 | + * @param array $columns |
|
| 91 | + * @return object |
|
| 92 | + */ |
|
| 93 | + public function first($conditions, $relations = [], $columns = ['*']); |
|
| 94 | 94 | } |
@@ -3,7 +3,6 @@ |
||
| 3 | 3 | namespace App\Modules\Users\Repositories; |
| 4 | 4 | |
| 5 | 5 | use App\Modules\Core\BaseClasses\BaseRepository; |
| 6 | -use Illuminate\Support\Arr; |
|
| 7 | 6 | use App\Modules\Users\AclUser; |
| 8 | 7 | |
| 9 | 8 | class UserRepository extends BaseRepository |
@@ -8,52 +8,52 @@ |
||
| 8 | 8 | |
| 9 | 9 | class UserRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param AclUser $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(AclUser $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param AclUser $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(AclUser $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Detach all roles from the given user. |
|
| 24 | - * |
|
| 25 | - * @param mixed $user |
|
| 26 | - * @return object |
|
| 27 | - */ |
|
| 28 | - public function detachRoles($user) |
|
| 29 | - { |
|
| 30 | - $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 31 | - $user->roles()->detach(); |
|
| 32 | - } |
|
| 22 | + /** |
|
| 23 | + * Detach all roles from the given user. |
|
| 24 | + * |
|
| 25 | + * @param mixed $user |
|
| 26 | + * @return object |
|
| 27 | + */ |
|
| 28 | + public function detachRoles($user) |
|
| 29 | + { |
|
| 30 | + $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 31 | + $user->roles()->detach(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Attach role ids to the given user. |
|
| 36 | - * |
|
| 37 | - * @param mixed $user |
|
| 38 | - * @param array $roleIds |
|
| 39 | - * @return object |
|
| 40 | - */ |
|
| 41 | - public function attachRoles($user, $roleIds) |
|
| 42 | - { |
|
| 43 | - $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 44 | - $user->roles()->attach($roleIds); |
|
| 45 | - } |
|
| 34 | + /** |
|
| 35 | + * Attach role ids to the given user. |
|
| 36 | + * |
|
| 37 | + * @param mixed $user |
|
| 38 | + * @param array $roleIds |
|
| 39 | + * @return object |
|
| 40 | + */ |
|
| 41 | + public function attachRoles($user, $roleIds) |
|
| 42 | + { |
|
| 43 | + $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 44 | + $user->roles()->attach($roleIds); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Count the given user the given roles. |
|
| 49 | - * |
|
| 50 | - * @param mixed $user |
|
| 51 | - * @param string[] $roles |
|
| 52 | - * @return boolean |
|
| 53 | - */ |
|
| 54 | - public function countRoles($user, $roles) |
|
| 55 | - { |
|
| 56 | - $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 57 | - return $user->roles()->whereIn('name', $roles)->count(); |
|
| 58 | - } |
|
| 47 | + /** |
|
| 48 | + * Count the given user the given roles. |
|
| 49 | + * |
|
| 50 | + * @param mixed $user |
|
| 51 | + * @param string[] $roles |
|
| 52 | + * @return boolean |
|
| 53 | + */ |
|
| 54 | + public function countRoles($user, $roles) |
|
| 55 | + { |
|
| 56 | + $user = ! filter_var($user, FILTER_VALIDATE_INT) ? $user : $this->find($user); |
|
| 57 | + return $user->roles()->whereIn('name', $roles)->count(); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -2,8 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Here goes your error messages. |
|
| 7 | - */ |
|
| 5 | + /** |
|
| 6 | + * Here goes your error messages. |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | 9 | ]; |
@@ -4,8 +4,8 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Errors extends Facade |
| 6 | 6 | { |
| 7 | - protected static function getFacadeAccessor() |
|
| 8 | - { |
|
| 9 | - return 'Errors'; |
|
| 10 | - } |
|
| 7 | + protected static function getFacadeAccessor() |
|
| 8 | + { |
|
| 9 | + return 'Errors'; |
|
| 10 | + } |
|
| 11 | 11 | } |
@@ -19,8 +19,8 @@ |
||
| 19 | 19 | public function __call($name, $arguments) |
| 20 | 20 | { |
| 21 | 21 | foreach (\Module::all() as $module) { |
| 22 | - $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
| 23 | - $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
| 22 | + $nameSpace = 'App\\Modules\\'.$module['basename']; |
|
| 23 | + $class = $nameSpace.'\\Errors\\'.$module['basename'].'Errors'; |
|
| 24 | 24 | |
| 25 | 25 | if (class_exists($class)) { |
| 26 | 26 | $class = \App::make($class); |
@@ -6,28 +6,28 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Errors implements BaseFactoryInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Construct the config class name based on |
|
| 11 | - * the method name called, search in the |
|
| 12 | - * given namespaces for the class and |
|
| 13 | - * return an instance. |
|
| 14 | - * |
|
| 15 | - * @param string $name the called method name |
|
| 16 | - * @param array $arguments the method arguments |
|
| 17 | - * @return object |
|
| 18 | - */ |
|
| 19 | - public function __call($name, $arguments) |
|
| 20 | - { |
|
| 21 | - foreach (\Module::all() as $module) { |
|
| 22 | - $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
| 23 | - $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
| 9 | + /** |
|
| 10 | + * Construct the config class name based on |
|
| 11 | + * the method name called, search in the |
|
| 12 | + * given namespaces for the class and |
|
| 13 | + * return an instance. |
|
| 14 | + * |
|
| 15 | + * @param string $name the called method name |
|
| 16 | + * @param array $arguments the method arguments |
|
| 17 | + * @return object |
|
| 18 | + */ |
|
| 19 | + public function __call($name, $arguments) |
|
| 20 | + { |
|
| 21 | + foreach (\Module::all() as $module) { |
|
| 22 | + $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
| 23 | + $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
| 24 | 24 | |
| 25 | - if (class_exists($class)) { |
|
| 26 | - $class = \App::make($class); |
|
| 27 | - if (method_exists($class, $name)) { |
|
| 28 | - return call_user_func_array([$class, $name], $arguments); |
|
| 29 | - } |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - } |
|
| 25 | + if (class_exists($class)) { |
|
| 26 | + $class = \App::make($class); |
|
| 27 | + if (method_exists($class, $name)) { |
|
| 28 | + return call_user_func_array([$class, $name], $arguments); |
|
| 29 | + } |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -2,21 +2,21 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Here goes your error messages. |
|
| 7 | - */ |
|
| 8 | - 'unAuthorized' => 'Please login before any action', |
|
| 9 | - 'invalidRefreshToken' => 'Invalid refresh token', |
|
| 10 | - 'noPermissions' => 'No permissions', |
|
| 11 | - 'loginFailed' => 'Wrong mail or password', |
|
| 12 | - 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
| 13 | - 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
| 14 | - 'userIsBlocked' => 'You have been blocked', |
|
| 15 | - 'invalidResetToken' => 'Reset password token is invalid', |
|
| 16 | - 'invalidResetPassword' => 'Reset password is invalid', |
|
| 17 | - 'invalidOldPassword' => 'Old password is invalid', |
|
| 18 | - 'invalidConfirmationCode' => 'Confirmation link expired or already used', |
|
| 19 | - 'emailNotConfirmed' => 'Your email isn\'t confirmed', |
|
| 20 | - 'emailAlreadyConfirmed' => 'Your email is already confirmed' |
|
| 5 | + /** |
|
| 6 | + * Here goes your error messages. |
|
| 7 | + */ |
|
| 8 | + 'unAuthorized' => 'Please login before any action', |
|
| 9 | + 'invalidRefreshToken' => 'Invalid refresh token', |
|
| 10 | + 'noPermissions' => 'No permissions', |
|
| 11 | + 'loginFailed' => 'Wrong mail or password', |
|
| 12 | + 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
| 13 | + 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
| 14 | + 'userIsBlocked' => 'You have been blocked', |
|
| 15 | + 'invalidResetToken' => 'Reset password token is invalid', |
|
| 16 | + 'invalidResetPassword' => 'Reset password is invalid', |
|
| 17 | + 'invalidOldPassword' => 'Old password is invalid', |
|
| 18 | + 'invalidConfirmationCode' => 'Confirmation link expired or already used', |
|
| 19 | + 'emailNotConfirmed' => 'Your email isn\'t confirmed', |
|
| 20 | + 'emailAlreadyConfirmed' => 'Your email is already confirmed' |
|
| 21 | 21 | |
| 22 | 22 | ]; |