@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | * |
41 | 41 | * @param array $relations |
42 | 42 | * @param string $sortBy |
43 | - * @param boolean $desc |
|
43 | + * @param integer $desc |
|
44 | 44 | * @param array $columns |
45 | 45 | * @return collection |
46 | 46 | */ |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @param integer $perPage |
61 | 61 | * @param array $relations |
62 | 62 | * @param string $sortBy |
63 | - * @param boolean $desc |
|
63 | + * @param integer $desc |
|
64 | 64 | * @param array $columns |
65 | 65 | * @return collection |
66 | 66 | */ |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * @param integer $perPage |
80 | 80 | * @param array $relations |
81 | 81 | * @param string $sortBy |
82 | - * @param boolean $desc |
|
82 | + * @param integer $desc |
|
83 | 83 | * @param array $columns |
84 | 84 | * @return collection |
85 | 85 | */ |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | * @param integer $perPage |
101 | 101 | * @param array $relations |
102 | 102 | * @param string $sortBy |
103 | - * @param boolean $desc |
|
103 | + * @param integer $desc |
|
104 | 104 | * @param array $columns |
105 | 105 | * @return collection |
106 | 106 | */ |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | * @param array $conditions array of conditions |
180 | 180 | * @param array $relations |
181 | 181 | * @param string $sortBy |
182 | - * @param boolean $desc |
|
182 | + * @param integer $desc |
|
183 | 183 | * @param array $columns |
184 | 184 | * @return collection |
185 | 185 | */ |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | * @param array $conditions array of conditions |
215 | 215 | * @param integer $perPage |
216 | 216 | * @param string $sortBy |
217 | - * @param boolean $desc |
|
217 | + * @param integer $desc |
|
218 | 218 | * @param array $columns |
219 | 219 | * @return collection |
220 | 220 | */ |
@@ -4,251 +4,251 @@ |
||
4 | 4 | |
5 | 5 | class CachingDecorator implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The repo implementation. |
|
9 | - * |
|
10 | - * @var repo |
|
11 | - */ |
|
12 | - protected $repo; |
|
7 | + /** |
|
8 | + * The repo implementation. |
|
9 | + * |
|
10 | + * @var repo |
|
11 | + */ |
|
12 | + protected $repo; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The cache implementation. |
|
16 | - * |
|
17 | - * @var cache |
|
18 | - */ |
|
19 | - protected $cache; |
|
14 | + /** |
|
15 | + * The cache implementation. |
|
16 | + * |
|
17 | + * @var cache |
|
18 | + */ |
|
19 | + protected $cache; |
|
20 | 20 | |
21 | - /** |
|
22 | - * The model implementation. |
|
23 | - * |
|
24 | - * @var model |
|
25 | - */ |
|
26 | - public $model; |
|
21 | + /** |
|
22 | + * The model implementation. |
|
23 | + * |
|
24 | + * @var model |
|
25 | + */ |
|
26 | + public $model; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Create new CachingDecorator instance. |
|
30 | - */ |
|
31 | - public function __construct($repo, $cache) |
|
32 | - { |
|
33 | - $this->repo = $repo; |
|
34 | - $this->cache = $cache; |
|
35 | - $this->model = get_class($this->repo->model); |
|
36 | - } |
|
28 | + /** |
|
29 | + * Create new CachingDecorator instance. |
|
30 | + */ |
|
31 | + public function __construct($repo, $cache) |
|
32 | + { |
|
33 | + $this->repo = $repo; |
|
34 | + $this->cache = $cache; |
|
35 | + $this->model = get_class($this->repo->model); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Fetch all records with relations from the storage. |
|
40 | - * |
|
41 | - * @param array $relations |
|
42 | - * @param string $sortBy |
|
43 | - * @param boolean $desc |
|
44 | - * @param array $columns |
|
45 | - * @return collection |
|
46 | - */ |
|
47 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
48 | - { |
|
49 | - $cacheKey = 'all'; |
|
50 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($relations, $sortBy, $desc, $columns) { |
|
51 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
52 | - }); |
|
53 | - } |
|
38 | + /** |
|
39 | + * Fetch all records with relations from the storage. |
|
40 | + * |
|
41 | + * @param array $relations |
|
42 | + * @param string $sortBy |
|
43 | + * @param boolean $desc |
|
44 | + * @param array $columns |
|
45 | + * @return collection |
|
46 | + */ |
|
47 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
48 | + { |
|
49 | + $cacheKey = 'all'; |
|
50 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($relations, $sortBy, $desc, $columns) { |
|
51 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
52 | + }); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Fetch all records with relations from storage in pages |
|
57 | - * that matche the given query. |
|
58 | - * |
|
59 | - * @param string $query |
|
60 | - * @param integer $perPage |
|
61 | - * @param array $relations |
|
62 | - * @param string $sortBy |
|
63 | - * @param boolean $desc |
|
64 | - * @param array $columns |
|
65 | - * @return collection |
|
66 | - */ |
|
67 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
68 | - { |
|
69 | - $page = \Request::get('page') ?? '1'; |
|
70 | - $cacheKey = 'search.' . $perPage . '.' . $query . '.' . $page; |
|
71 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($query, $perPage, $relations, $sortBy, $desc, $columns) { |
|
72 | - return $this->repo->search($query, $perPage, $relations, $sortBy, $desc, $columns); |
|
73 | - }); |
|
74 | - } |
|
55 | + /** |
|
56 | + * Fetch all records with relations from storage in pages |
|
57 | + * that matche the given query. |
|
58 | + * |
|
59 | + * @param string $query |
|
60 | + * @param integer $perPage |
|
61 | + * @param array $relations |
|
62 | + * @param string $sortBy |
|
63 | + * @param boolean $desc |
|
64 | + * @param array $columns |
|
65 | + * @return collection |
|
66 | + */ |
|
67 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
68 | + { |
|
69 | + $page = \Request::get('page') ?? '1'; |
|
70 | + $cacheKey = 'search.' . $perPage . '.' . $query . '.' . $page; |
|
71 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($query, $perPage, $relations, $sortBy, $desc, $columns) { |
|
72 | + return $this->repo->search($query, $perPage, $relations, $sortBy, $desc, $columns); |
|
73 | + }); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Fetch all records with relations from storage in pages. |
|
78 | - * |
|
79 | - * @param integer $perPage |
|
80 | - * @param array $relations |
|
81 | - * @param string $sortBy |
|
82 | - * @param boolean $desc |
|
83 | - * @param array $columns |
|
84 | - * @return collection |
|
85 | - */ |
|
86 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
87 | - { |
|
88 | - $page = \Request::get('page') ?? '1'; |
|
89 | - $cacheKey = 'paginate.' . $perPage . '.' . $page; |
|
90 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($perPage, $relations, $sortBy, $desc, $columns) { |
|
91 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
92 | - }); |
|
93 | - } |
|
76 | + /** |
|
77 | + * Fetch all records with relations from storage in pages. |
|
78 | + * |
|
79 | + * @param integer $perPage |
|
80 | + * @param array $relations |
|
81 | + * @param string $sortBy |
|
82 | + * @param boolean $desc |
|
83 | + * @param array $columns |
|
84 | + * @return collection |
|
85 | + */ |
|
86 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
87 | + { |
|
88 | + $page = \Request::get('page') ?? '1'; |
|
89 | + $cacheKey = 'paginate.' . $perPage . '.' . $page; |
|
90 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($perPage, $relations, $sortBy, $desc, $columns) { |
|
91 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
92 | + }); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Fetch all records with relations based on |
|
97 | - * the given condition from storage in pages. |
|
98 | - * |
|
99 | - * @param array $conditions array of conditions |
|
100 | - * @param integer $perPage |
|
101 | - * @param array $relations |
|
102 | - * @param string $sortBy |
|
103 | - * @param boolean $desc |
|
104 | - * @param array $columns |
|
105 | - * @return collection |
|
106 | - */ |
|
107 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
108 | - { |
|
109 | - $page = \Request::get('page') ?? '1'; |
|
110 | - $cacheKey = 'paginateBy.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
111 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $relations, $sortBy, $desc, $columns) { |
|
112 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
113 | - }); |
|
114 | - } |
|
95 | + /** |
|
96 | + * Fetch all records with relations based on |
|
97 | + * the given condition from storage in pages. |
|
98 | + * |
|
99 | + * @param array $conditions array of conditions |
|
100 | + * @param integer $perPage |
|
101 | + * @param array $relations |
|
102 | + * @param string $sortBy |
|
103 | + * @param boolean $desc |
|
104 | + * @param array $columns |
|
105 | + * @return collection |
|
106 | + */ |
|
107 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
108 | + { |
|
109 | + $page = \Request::get('page') ?? '1'; |
|
110 | + $cacheKey = 'paginateBy.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
111 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $relations, $sortBy, $desc, $columns) { |
|
112 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
113 | + }); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Save the given model to the storage. |
|
118 | - * |
|
119 | - * @param array $data |
|
120 | - * @param boolean $saveLog |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - public function save(array $data, $saveLog = true) |
|
124 | - { |
|
125 | - $this->cache->tags([$this->model])->flush(); |
|
126 | - return $this->repo->save($data, $saveLog); |
|
127 | - } |
|
116 | + /** |
|
117 | + * Save the given model to the storage. |
|
118 | + * |
|
119 | + * @param array $data |
|
120 | + * @param boolean $saveLog |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + public function save(array $data, $saveLog = true) |
|
124 | + { |
|
125 | + $this->cache->tags([$this->model])->flush(); |
|
126 | + return $this->repo->save($data, $saveLog); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * Update record in the storage based on the given |
|
131 | - * condition. |
|
132 | - * |
|
133 | - * @param var $value condition value |
|
134 | - * @param array $data |
|
135 | - * @param string $attribute condition column name |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
139 | - { |
|
140 | - $this->cache->tags([$this->model])->flush(); |
|
141 | - return $this->repo->update($value, $data, $attribute, $saveLog); |
|
142 | - } |
|
129 | + /** |
|
130 | + * Update record in the storage based on the given |
|
131 | + * condition. |
|
132 | + * |
|
133 | + * @param var $value condition value |
|
134 | + * @param array $data |
|
135 | + * @param string $attribute condition column name |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
139 | + { |
|
140 | + $this->cache->tags([$this->model])->flush(); |
|
141 | + return $this->repo->update($value, $data, $attribute, $saveLog); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * Delete record from the storage based on the given |
|
146 | - * condition. |
|
147 | - * |
|
148 | - * @param var $value condition value |
|
149 | - * @param string $attribute condition column name |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
153 | - { |
|
154 | - $this->cache->tags([$this->model])->flush(); |
|
155 | - return $this->repo->delete($value, $attribute, $saveLog); |
|
156 | - } |
|
144 | + /** |
|
145 | + * Delete record from the storage based on the given |
|
146 | + * condition. |
|
147 | + * |
|
148 | + * @param var $value condition value |
|
149 | + * @param string $attribute condition column name |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
153 | + { |
|
154 | + $this->cache->tags([$this->model])->flush(); |
|
155 | + return $this->repo->delete($value, $attribute, $saveLog); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * Fetch records from the storage based on the given |
|
160 | - * id. |
|
161 | - * |
|
162 | - * @param integer $id |
|
163 | - * @param array $relations |
|
164 | - * @param array $columns |
|
165 | - * @return object |
|
166 | - */ |
|
167 | - public function find($id, $relations = [], $columns = array('*')) |
|
168 | - { |
|
169 | - $cacheKey = 'find.' . $id; |
|
170 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($id, $relations, $columns) { |
|
171 | - return $this->repo->find($id, $relations, $columns); |
|
172 | - }); |
|
173 | - } |
|
158 | + /** |
|
159 | + * Fetch records from the storage based on the given |
|
160 | + * id. |
|
161 | + * |
|
162 | + * @param integer $id |
|
163 | + * @param array $relations |
|
164 | + * @param array $columns |
|
165 | + * @return object |
|
166 | + */ |
|
167 | + public function find($id, $relations = [], $columns = array('*')) |
|
168 | + { |
|
169 | + $cacheKey = 'find.' . $id; |
|
170 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($id, $relations, $columns) { |
|
171 | + return $this->repo->find($id, $relations, $columns); |
|
172 | + }); |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Fetch records from the storage based on the given |
|
177 | - * condition. |
|
178 | - * |
|
179 | - * @param array $conditions array of conditions |
|
180 | - * @param array $relations |
|
181 | - * @param string $sortBy |
|
182 | - * @param boolean $desc |
|
183 | - * @param array $columns |
|
184 | - * @return collection |
|
185 | - */ |
|
186 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
187 | - { |
|
188 | - $cacheKey = 'findBy.' . serialize($conditions); |
|
189 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $sortBy, $desc, $columns) { |
|
190 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
191 | - }); |
|
192 | - } |
|
175 | + /** |
|
176 | + * Fetch records from the storage based on the given |
|
177 | + * condition. |
|
178 | + * |
|
179 | + * @param array $conditions array of conditions |
|
180 | + * @param array $relations |
|
181 | + * @param string $sortBy |
|
182 | + * @param boolean $desc |
|
183 | + * @param array $columns |
|
184 | + * @return collection |
|
185 | + */ |
|
186 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
187 | + { |
|
188 | + $cacheKey = 'findBy.' . serialize($conditions); |
|
189 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $sortBy, $desc, $columns) { |
|
190 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
191 | + }); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Fetch the first record from the storage based on the given |
|
196 | - * condition. |
|
197 | - * |
|
198 | - * @param array $conditions array of conditions |
|
199 | - * @param array $relations |
|
200 | - * @param array $columns |
|
201 | - * @return object |
|
202 | - */ |
|
203 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
204 | - { |
|
205 | - $cacheKey = 'first.' . serialize($conditions); |
|
206 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $columns) { |
|
207 | - return $this->repo->first($conditions, $relations, $columns); |
|
208 | - }); |
|
209 | - } |
|
194 | + /** |
|
195 | + * Fetch the first record from the storage based on the given |
|
196 | + * condition. |
|
197 | + * |
|
198 | + * @param array $conditions array of conditions |
|
199 | + * @param array $relations |
|
200 | + * @param array $columns |
|
201 | + * @return object |
|
202 | + */ |
|
203 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
204 | + { |
|
205 | + $cacheKey = 'first.' . serialize($conditions); |
|
206 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $columns) { |
|
207 | + return $this->repo->first($conditions, $relations, $columns); |
|
208 | + }); |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Return the deleted models in pages based on the given conditions. |
|
213 | - * |
|
214 | - * @param array $conditions array of conditions |
|
215 | - * @param integer $perPage |
|
216 | - * @param string $sortBy |
|
217 | - * @param boolean $desc |
|
218 | - * @param array $columns |
|
219 | - * @return collection |
|
220 | - */ |
|
221 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
222 | - { |
|
223 | - $page = \Request::get('page') ?? '1'; |
|
224 | - $cacheKey = 'deleted.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
225 | - return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $sortBy, $desc, $columns) { |
|
226 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
227 | - }); |
|
228 | - } |
|
211 | + /** |
|
212 | + * Return the deleted models in pages based on the given conditions. |
|
213 | + * |
|
214 | + * @param array $conditions array of conditions |
|
215 | + * @param integer $perPage |
|
216 | + * @param string $sortBy |
|
217 | + * @param boolean $desc |
|
218 | + * @param array $columns |
|
219 | + * @return collection |
|
220 | + */ |
|
221 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
222 | + { |
|
223 | + $page = \Request::get('page') ?? '1'; |
|
224 | + $cacheKey = 'deleted.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
225 | + return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $sortBy, $desc, $columns) { |
|
226 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
227 | + }); |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Restore the deleted model. |
|
232 | - * |
|
233 | - * @param integer $id |
|
234 | - * @return void |
|
235 | - */ |
|
236 | - public function restore($id) |
|
237 | - { |
|
238 | - $this->cache->tags([$this->model])->flush(); |
|
239 | - return $this->repo->restore($id); |
|
240 | - } |
|
230 | + /** |
|
231 | + * Restore the deleted model. |
|
232 | + * |
|
233 | + * @param integer $id |
|
234 | + * @return void |
|
235 | + */ |
|
236 | + public function restore($id) |
|
237 | + { |
|
238 | + $this->cache->tags([$this->model])->flush(); |
|
239 | + return $this->repo->restore($id); |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * Handle calling methods that is not implemented in the |
|
244 | - * abstract repository. |
|
245 | - * |
|
246 | - * @param string $name the called method name |
|
247 | - * @param array $arguments the method arguments |
|
248 | - * @return object |
|
249 | - */ |
|
250 | - public function __call($name, $arguments) |
|
251 | - { |
|
252 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
253 | - } |
|
242 | + /** |
|
243 | + * Handle calling methods that is not implemented in the |
|
244 | + * abstract repository. |
|
245 | + * |
|
246 | + * @param string $name the called method name |
|
247 | + * @param array $arguments the method arguments |
|
248 | + * @return object |
|
249 | + */ |
|
250 | + public function __call($name, $arguments) |
|
251 | + { |
|
252 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
253 | + } |
|
254 | 254 | } |
255 | 255 | \ No newline at end of file |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
68 | 68 | { |
69 | 69 | $page = \Request::get('page') ?? '1'; |
70 | - $cacheKey = 'search.' . $perPage . '.' . $query . '.' . $page; |
|
70 | + $cacheKey = 'search.'.$perPage.'.'.$query.'.'.$page; |
|
71 | 71 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($query, $perPage, $relations, $sortBy, $desc, $columns) { |
72 | 72 | return $this->repo->search($query, $perPage, $relations, $sortBy, $desc, $columns); |
73 | 73 | }); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
87 | 87 | { |
88 | 88 | $page = \Request::get('page') ?? '1'; |
89 | - $cacheKey = 'paginate.' . $perPage . '.' . $page; |
|
89 | + $cacheKey = 'paginate.'.$perPage.'.'.$page; |
|
90 | 90 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($perPage, $relations, $sortBy, $desc, $columns) { |
91 | 91 | return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
92 | 92 | }); |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
108 | 108 | { |
109 | 109 | $page = \Request::get('page') ?? '1'; |
110 | - $cacheKey = 'paginateBy.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
110 | + $cacheKey = 'paginateBy.'.$perPage.'.'.$page.'.'.serialize($conditions); |
|
111 | 111 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $relations, $sortBy, $desc, $columns) { |
112 | 112 | return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
113 | 113 | }); |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | */ |
167 | 167 | public function find($id, $relations = [], $columns = array('*')) |
168 | 168 | { |
169 | - $cacheKey = 'find.' . $id; |
|
169 | + $cacheKey = 'find.'.$id; |
|
170 | 170 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($id, $relations, $columns) { |
171 | 171 | return $this->repo->find($id, $relations, $columns); |
172 | 172 | }); |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | */ |
186 | 186 | public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
187 | 187 | { |
188 | - $cacheKey = 'findBy.' . serialize($conditions); |
|
188 | + $cacheKey = 'findBy.'.serialize($conditions); |
|
189 | 189 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $sortBy, $desc, $columns) { |
190 | 190 | return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
191 | 191 | }); |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | */ |
203 | 203 | public function first($conditions, $relations = [], $columns = array('*')) |
204 | 204 | { |
205 | - $cacheKey = 'first.' . serialize($conditions); |
|
205 | + $cacheKey = 'first.'.serialize($conditions); |
|
206 | 206 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $relations, $columns) { |
207 | 207 | return $this->repo->first($conditions, $relations, $columns); |
208 | 208 | }); |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
222 | 222 | { |
223 | 223 | $page = \Request::get('page') ?? '1'; |
224 | - $cacheKey = 'deleted.' . $perPage . '.' . $page . '.' . serialize($conditions); |
|
224 | + $cacheKey = 'deleted.'.$perPage.'.'.$page.'.'.serialize($conditions); |
|
225 | 225 | return $this->cache->tags([$this->model])->rememberForever($cacheKey, function() use ($conditions, $perPage, $sortBy, $desc, $columns) { |
226 | 226 | return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
227 | 227 | }); |
@@ -4,655 +4,655 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var config |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var config |
|
18 | + */ |
|
19 | + protected $config; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Create new AbstractRepository instance. |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->config = \CoreConfig::getConfig(); |
|
27 | - $this->model = \App::make($this->getModel()); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Fetch all records with relations from the storage. |
|
32 | - * |
|
33 | - * @param array $relations |
|
34 | - * @param string $sortBy |
|
35 | - * @param boolean $desc |
|
36 | - * @param array $columns |
|
37 | - * @return collection |
|
38 | - */ |
|
39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | - { |
|
41 | - $sort = $desc ? 'desc' : 'asc'; |
|
42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Fetch all records with relations from storage in pages |
|
47 | - * that matche the given query. |
|
48 | - * |
|
49 | - * @param string $query |
|
50 | - * @param integer $perPage |
|
51 | - * @param array $relations |
|
52 | - * @param string $sortBy |
|
53 | - * @param boolean $desc |
|
54 | - * @param array $columns |
|
55 | - * @return collection |
|
56 | - */ |
|
57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | - { |
|
59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | - $conditionColumns = $this->model->searchable; |
|
61 | - $sort = $desc ? 'desc' : 'asc'; |
|
62 | - |
|
63 | - /** |
|
64 | - * Construct the select conditions for the model. |
|
65 | - */ |
|
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | - |
|
68 | - if (count($conditionColumns)) |
|
69 | - { |
|
70 | - /** |
|
71 | - * Use the first element in the model columns to construct the first condition. |
|
72 | - */ |
|
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Loop through the rest of the columns to construct or where conditions. |
|
78 | - */ |
|
79 | - foreach ($conditionColumns as $column) |
|
80 | - { |
|
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Loop through the model relations. |
|
86 | - */ |
|
87 | - foreach ($relations as $relation) |
|
88 | - { |
|
89 | - /** |
|
90 | - * Remove the sub relation if exists. |
|
91 | - */ |
|
92 | - $relation = explode('.', $relation)[0]; |
|
93 | - |
|
94 | - /** |
|
95 | - * Try to fetch the relation repository from the core. |
|
96 | - */ |
|
97 | - if (\Core::$relation()) |
|
98 | - { |
|
99 | - /** |
|
100 | - * Construct the relation condition. |
|
101 | - */ |
|
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
103 | - |
|
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
105 | - |
|
106 | - /** |
|
107 | - * Get columns of the relation. |
|
108 | - */ |
|
109 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
110 | - |
|
111 | - if (count($subConditionColumns)) |
|
112 | - { |
|
113 | - /** |
|
114 | - * Use the first element in the relation model columns to construct the first condition. |
|
115 | - */ |
|
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Loop through the rest of the columns to construct or where conditions. |
|
121 | - */ |
|
122 | - foreach ($subConditionColumns as $subConditionColumn) |
|
123 | - { |
|
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | - } |
|
126 | - }); |
|
127 | - |
|
128 | - }); |
|
129 | - } |
|
130 | - } |
|
131 | - }); |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | + { |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->searchable; |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | + |
|
63 | + /** |
|
64 | + * Construct the select conditions for the model. |
|
65 | + */ |
|
66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | + |
|
68 | + if (count($conditionColumns)) |
|
69 | + { |
|
70 | + /** |
|
71 | + * Use the first element in the model columns to construct the first condition. |
|
72 | + */ |
|
73 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Loop through the rest of the columns to construct or where conditions. |
|
78 | + */ |
|
79 | + foreach ($conditionColumns as $column) |
|
80 | + { |
|
81 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Loop through the model relations. |
|
86 | + */ |
|
87 | + foreach ($relations as $relation) |
|
88 | + { |
|
89 | + /** |
|
90 | + * Remove the sub relation if exists. |
|
91 | + */ |
|
92 | + $relation = explode('.', $relation)[0]; |
|
93 | + |
|
94 | + /** |
|
95 | + * Try to fetch the relation repository from the core. |
|
96 | + */ |
|
97 | + if (\Core::$relation()) |
|
98 | + { |
|
99 | + /** |
|
100 | + * Construct the relation condition. |
|
101 | + */ |
|
102 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
103 | + |
|
104 | + $subModel->where(function ($q) use ($query, $relation){ |
|
105 | + |
|
106 | + /** |
|
107 | + * Get columns of the relation. |
|
108 | + */ |
|
109 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
110 | + |
|
111 | + if (count($subConditionColumns)) |
|
112 | + { |
|
113 | + /** |
|
114 | + * Use the first element in the relation model columns to construct the first condition. |
|
115 | + */ |
|
116 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Loop through the rest of the columns to construct or where conditions. |
|
121 | + */ |
|
122 | + foreach ($subConditionColumns as $subConditionColumn) |
|
123 | + { |
|
124 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
125 | + } |
|
126 | + }); |
|
127 | + |
|
128 | + }); |
|
129 | + } |
|
130 | + } |
|
131 | + }); |
|
132 | 132 | |
133 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | - } |
|
133 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Fetch all records with relations from storage in pages. |
|
138 | - * |
|
139 | - * @param integer $perPage |
|
140 | - * @param array $relations |
|
141 | - * @param string $sortBy |
|
142 | - * @param boolean $desc |
|
143 | - * @param array $columns |
|
144 | - * @return collection |
|
145 | - */ |
|
146 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | - { |
|
148 | - $sort = $desc ? 'desc' : 'asc'; |
|
149 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Fetch all records with relations based on |
|
154 | - * the given condition from storage in pages. |
|
155 | - * |
|
156 | - * @param array $conditions array of conditions |
|
157 | - * @param integer $perPage |
|
158 | - * @param array $relations |
|
159 | - * @param string $sortBy |
|
160 | - * @param boolean $desc |
|
161 | - * @param array $columns |
|
162 | - * @return collection |
|
163 | - */ |
|
164 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | - { |
|
166 | - unset($conditions['page']); |
|
167 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
168 | - $sort = $desc ? 'desc' : 'asc'; |
|
169 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | - } |
|
136 | + /** |
|
137 | + * Fetch all records with relations from storage in pages. |
|
138 | + * |
|
139 | + * @param integer $perPage |
|
140 | + * @param array $relations |
|
141 | + * @param string $sortBy |
|
142 | + * @param boolean $desc |
|
143 | + * @param array $columns |
|
144 | + * @return collection |
|
145 | + */ |
|
146 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
147 | + { |
|
148 | + $sort = $desc ? 'desc' : 'asc'; |
|
149 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Fetch all records with relations based on |
|
154 | + * the given condition from storage in pages. |
|
155 | + * |
|
156 | + * @param array $conditions array of conditions |
|
157 | + * @param integer $perPage |
|
158 | + * @param array $relations |
|
159 | + * @param string $sortBy |
|
160 | + * @param boolean $desc |
|
161 | + * @param array $columns |
|
162 | + * @return collection |
|
163 | + */ |
|
164 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
165 | + { |
|
166 | + unset($conditions['page']); |
|
167 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
168 | + $sort = $desc ? 'desc' : 'asc'; |
|
169 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Save the given model to the storage. |
|
174 | - * |
|
175 | - * @param array $data |
|
176 | - * @param boolean $saveLog |
|
177 | - * @return void |
|
178 | - */ |
|
179 | - public function save(array $data, $saveLog = true) |
|
180 | - { |
|
181 | - $model = false; |
|
182 | - $modelClass = $this->model; |
|
183 | - $relations = []; |
|
184 | - |
|
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | - /** |
|
187 | - * If the id is present in the data then select the model for updating, |
|
188 | - * else create new model. |
|
189 | - * @var array |
|
190 | - */ |
|
191 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | - if ( ! $model) |
|
193 | - { |
|
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Construct the model object with the given data, |
|
199 | - * and if there is a relation add it to relations array, |
|
200 | - * then save the model. |
|
201 | - */ |
|
202 | - foreach ($data as $key => $value) |
|
203 | - { |
|
204 | - /** |
|
205 | - * If the attribute is a relation. |
|
206 | - */ |
|
207 | - $relation = camel_case($key); |
|
208 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
209 | - { |
|
210 | - /** |
|
211 | - * Check if the relation is a collection. |
|
212 | - */ |
|
213 | - if (class_basename($model->$relation) == 'Collection') |
|
214 | - { |
|
215 | - /** |
|
216 | - * If the relation has no value then marke the relation data |
|
217 | - * related to the model to be deleted. |
|
218 | - */ |
|
219 | - if ( ! $value || ! count($value)) |
|
220 | - { |
|
221 | - $relations[$relation] = 'delete'; |
|
222 | - } |
|
223 | - } |
|
224 | - if (is_array($value)) |
|
225 | - { |
|
226 | - /** |
|
227 | - * Loop through the relation data. |
|
228 | - */ |
|
229 | - foreach ($value as $attr => $val) |
|
230 | - { |
|
231 | - /** |
|
232 | - * Get the relation model. |
|
233 | - */ |
|
234 | - $relationBaseModel = \Core::$relation()->model; |
|
235 | - |
|
236 | - /** |
|
237 | - * Check if the relation is a collection. |
|
238 | - */ |
|
239 | - if (class_basename($model->$relation) == 'Collection') |
|
240 | - { |
|
241 | - /** |
|
242 | - * If the id is present in the data then select the relation model for updating, |
|
243 | - * else create new model. |
|
244 | - */ |
|
245 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
246 | - |
|
247 | - /** |
|
248 | - * If model doesn't exists. |
|
249 | - */ |
|
250 | - if ( ! $relationModel) |
|
251 | - { |
|
252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Loop through the relation attributes. |
|
257 | - */ |
|
258 | - foreach ($val as $attr => $val) |
|
259 | - { |
|
260 | - /** |
|
261 | - * Prevent the sub relations or attributes not in the fillable. |
|
262 | - */ |
|
263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | - { |
|
265 | - $relationModel->$attr = $val; |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - $relations[$relation][] = $relationModel; |
|
270 | - } |
|
271 | - /** |
|
272 | - * If not collection. |
|
273 | - */ |
|
274 | - else |
|
275 | - { |
|
276 | - /** |
|
277 | - * Prevent the sub relations. |
|
278 | - */ |
|
279 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | - { |
|
281 | - |
|
282 | - /** |
|
283 | - * If the id is present in the data then select the relation model for updating, |
|
284 | - * else create new model. |
|
285 | - */ |
|
286 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
287 | - |
|
288 | - /** |
|
289 | - * If model doesn't exists. |
|
290 | - */ |
|
291 | - if ( ! $relationModel) |
|
292 | - { |
|
293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
294 | - } |
|
295 | - |
|
296 | - foreach ($value as $relationAttribute => $relationValue) |
|
297 | - { |
|
298 | - /** |
|
299 | - * Prevent attributes not in the fillable. |
|
300 | - */ |
|
301 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
302 | - { |
|
303 | - $relationModel->$relationAttribute = $relationValue; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - $relations[$relation] = $relationModel; |
|
308 | - } |
|
309 | - } |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - /** |
|
314 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
315 | - */ |
|
316 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
317 | - { |
|
318 | - $model->$key = $value; |
|
319 | - } |
|
320 | - } |
|
321 | - /** |
|
322 | - * Save the model. |
|
323 | - */ |
|
324 | - $model->save(); |
|
325 | - |
|
326 | - /** |
|
327 | - * Loop through the relations array. |
|
328 | - */ |
|
329 | - foreach ($relations as $key => $value) |
|
330 | - { |
|
331 | - /** |
|
332 | - * If the relation is marked for delete then delete it. |
|
333 | - */ |
|
334 | - if ($value == 'delete' && $model->$key()->count()) |
|
335 | - { |
|
336 | - $model->$key()->delete(); |
|
337 | - } |
|
338 | - /** |
|
339 | - * If the relation is an array. |
|
340 | - */ |
|
341 | - else if (gettype($value) == 'array') |
|
342 | - { |
|
343 | - $ids = []; |
|
344 | - /** |
|
345 | - * Loop through the relations. |
|
346 | - */ |
|
347 | - foreach ($value as $val) |
|
348 | - { |
|
349 | - switch (class_basename($model->$key())) |
|
350 | - { |
|
351 | - /** |
|
352 | - * If the relation is one to many then update it's foreign key with |
|
353 | - * the model id and save it then add its id to ids array to delete all |
|
354 | - * relations who's id isn't in the ids array. |
|
355 | - */ |
|
356 | - case 'HasMany': |
|
357 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
358 | - $val->$foreignKeyName = $model->id; |
|
359 | - $val->save(); |
|
360 | - $ids[] = $val->id; |
|
361 | - break; |
|
362 | - |
|
363 | - /** |
|
364 | - * If the relation is many to many then add it's id to the ids array to |
|
365 | - * attache these ids to the model. |
|
366 | - */ |
|
367 | - case 'BelongsToMany': |
|
368 | - $val->save(); |
|
369 | - $ids[] = $val->id; |
|
370 | - break; |
|
371 | - } |
|
372 | - } |
|
373 | - switch (class_basename($model->$key())) |
|
374 | - { |
|
375 | - /** |
|
376 | - * If the relation is one to many then delete all |
|
377 | - * relations who's id isn't in the ids array. |
|
378 | - */ |
|
379 | - case 'HasMany': |
|
380 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
381 | - break; |
|
382 | - |
|
383 | - /** |
|
384 | - * If the relation is many to many then |
|
385 | - * detach the previous data and attach |
|
386 | - * the ids array to the model. |
|
387 | - */ |
|
388 | - case 'BelongsToMany': |
|
389 | - $model->$key()->detach(); |
|
390 | - $model->$key()->attach($ids); |
|
391 | - break; |
|
392 | - } |
|
393 | - } |
|
394 | - /** |
|
395 | - * If the relation isn't array. |
|
396 | - */ |
|
397 | - else |
|
398 | - { |
|
399 | - switch (class_basename($model->$key())) |
|
400 | - { |
|
401 | - /** |
|
402 | - * If the relation is one to many or one to one. |
|
403 | - */ |
|
404 | - case 'HasOne': |
|
405 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
406 | - $value->$foreignKeyName = $model->id; |
|
407 | - $value->save(); |
|
408 | - break; |
|
409 | - } |
|
410 | - } |
|
411 | - } |
|
412 | - |
|
413 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
414 | - }); |
|
415 | - } |
|
172 | + /** |
|
173 | + * Save the given model to the storage. |
|
174 | + * |
|
175 | + * @param array $data |
|
176 | + * @param boolean $saveLog |
|
177 | + * @return void |
|
178 | + */ |
|
179 | + public function save(array $data, $saveLog = true) |
|
180 | + { |
|
181 | + $model = false; |
|
182 | + $modelClass = $this->model; |
|
183 | + $relations = []; |
|
184 | + |
|
185 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | + /** |
|
187 | + * If the id is present in the data then select the model for updating, |
|
188 | + * else create new model. |
|
189 | + * @var array |
|
190 | + */ |
|
191 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
192 | + if ( ! $model) |
|
193 | + { |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Construct the model object with the given data, |
|
199 | + * and if there is a relation add it to relations array, |
|
200 | + * then save the model. |
|
201 | + */ |
|
202 | + foreach ($data as $key => $value) |
|
203 | + { |
|
204 | + /** |
|
205 | + * If the attribute is a relation. |
|
206 | + */ |
|
207 | + $relation = camel_case($key); |
|
208 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
209 | + { |
|
210 | + /** |
|
211 | + * Check if the relation is a collection. |
|
212 | + */ |
|
213 | + if (class_basename($model->$relation) == 'Collection') |
|
214 | + { |
|
215 | + /** |
|
216 | + * If the relation has no value then marke the relation data |
|
217 | + * related to the model to be deleted. |
|
218 | + */ |
|
219 | + if ( ! $value || ! count($value)) |
|
220 | + { |
|
221 | + $relations[$relation] = 'delete'; |
|
222 | + } |
|
223 | + } |
|
224 | + if (is_array($value)) |
|
225 | + { |
|
226 | + /** |
|
227 | + * Loop through the relation data. |
|
228 | + */ |
|
229 | + foreach ($value as $attr => $val) |
|
230 | + { |
|
231 | + /** |
|
232 | + * Get the relation model. |
|
233 | + */ |
|
234 | + $relationBaseModel = \Core::$relation()->model; |
|
235 | + |
|
236 | + /** |
|
237 | + * Check if the relation is a collection. |
|
238 | + */ |
|
239 | + if (class_basename($model->$relation) == 'Collection') |
|
240 | + { |
|
241 | + /** |
|
242 | + * If the id is present in the data then select the relation model for updating, |
|
243 | + * else create new model. |
|
244 | + */ |
|
245 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
246 | + |
|
247 | + /** |
|
248 | + * If model doesn't exists. |
|
249 | + */ |
|
250 | + if ( ! $relationModel) |
|
251 | + { |
|
252 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Loop through the relation attributes. |
|
257 | + */ |
|
258 | + foreach ($val as $attr => $val) |
|
259 | + { |
|
260 | + /** |
|
261 | + * Prevent the sub relations or attributes not in the fillable. |
|
262 | + */ |
|
263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | + { |
|
265 | + $relationModel->$attr = $val; |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + $relations[$relation][] = $relationModel; |
|
270 | + } |
|
271 | + /** |
|
272 | + * If not collection. |
|
273 | + */ |
|
274 | + else |
|
275 | + { |
|
276 | + /** |
|
277 | + * Prevent the sub relations. |
|
278 | + */ |
|
279 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
280 | + { |
|
281 | + |
|
282 | + /** |
|
283 | + * If the id is present in the data then select the relation model for updating, |
|
284 | + * else create new model. |
|
285 | + */ |
|
286 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
287 | + |
|
288 | + /** |
|
289 | + * If model doesn't exists. |
|
290 | + */ |
|
291 | + if ( ! $relationModel) |
|
292 | + { |
|
293 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
294 | + } |
|
295 | + |
|
296 | + foreach ($value as $relationAttribute => $relationValue) |
|
297 | + { |
|
298 | + /** |
|
299 | + * Prevent attributes not in the fillable. |
|
300 | + */ |
|
301 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
302 | + { |
|
303 | + $relationModel->$relationAttribute = $relationValue; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + $relations[$relation] = $relationModel; |
|
308 | + } |
|
309 | + } |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + /** |
|
314 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
315 | + */ |
|
316 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
317 | + { |
|
318 | + $model->$key = $value; |
|
319 | + } |
|
320 | + } |
|
321 | + /** |
|
322 | + * Save the model. |
|
323 | + */ |
|
324 | + $model->save(); |
|
325 | + |
|
326 | + /** |
|
327 | + * Loop through the relations array. |
|
328 | + */ |
|
329 | + foreach ($relations as $key => $value) |
|
330 | + { |
|
331 | + /** |
|
332 | + * If the relation is marked for delete then delete it. |
|
333 | + */ |
|
334 | + if ($value == 'delete' && $model->$key()->count()) |
|
335 | + { |
|
336 | + $model->$key()->delete(); |
|
337 | + } |
|
338 | + /** |
|
339 | + * If the relation is an array. |
|
340 | + */ |
|
341 | + else if (gettype($value) == 'array') |
|
342 | + { |
|
343 | + $ids = []; |
|
344 | + /** |
|
345 | + * Loop through the relations. |
|
346 | + */ |
|
347 | + foreach ($value as $val) |
|
348 | + { |
|
349 | + switch (class_basename($model->$key())) |
|
350 | + { |
|
351 | + /** |
|
352 | + * If the relation is one to many then update it's foreign key with |
|
353 | + * the model id and save it then add its id to ids array to delete all |
|
354 | + * relations who's id isn't in the ids array. |
|
355 | + */ |
|
356 | + case 'HasMany': |
|
357 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
358 | + $val->$foreignKeyName = $model->id; |
|
359 | + $val->save(); |
|
360 | + $ids[] = $val->id; |
|
361 | + break; |
|
362 | + |
|
363 | + /** |
|
364 | + * If the relation is many to many then add it's id to the ids array to |
|
365 | + * attache these ids to the model. |
|
366 | + */ |
|
367 | + case 'BelongsToMany': |
|
368 | + $val->save(); |
|
369 | + $ids[] = $val->id; |
|
370 | + break; |
|
371 | + } |
|
372 | + } |
|
373 | + switch (class_basename($model->$key())) |
|
374 | + { |
|
375 | + /** |
|
376 | + * If the relation is one to many then delete all |
|
377 | + * relations who's id isn't in the ids array. |
|
378 | + */ |
|
379 | + case 'HasMany': |
|
380 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
381 | + break; |
|
382 | + |
|
383 | + /** |
|
384 | + * If the relation is many to many then |
|
385 | + * detach the previous data and attach |
|
386 | + * the ids array to the model. |
|
387 | + */ |
|
388 | + case 'BelongsToMany': |
|
389 | + $model->$key()->detach(); |
|
390 | + $model->$key()->attach($ids); |
|
391 | + break; |
|
392 | + } |
|
393 | + } |
|
394 | + /** |
|
395 | + * If the relation isn't array. |
|
396 | + */ |
|
397 | + else |
|
398 | + { |
|
399 | + switch (class_basename($model->$key())) |
|
400 | + { |
|
401 | + /** |
|
402 | + * If the relation is one to many or one to one. |
|
403 | + */ |
|
404 | + case 'HasOne': |
|
405 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
406 | + $value->$foreignKeyName = $model->id; |
|
407 | + $value->save(); |
|
408 | + break; |
|
409 | + } |
|
410 | + } |
|
411 | + } |
|
412 | + |
|
413 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
414 | + }); |
|
415 | + } |
|
416 | 416 | |
417 | - /** |
|
418 | - * Update record in the storage based on the given |
|
419 | - * condition. |
|
420 | - * |
|
421 | - * @param var $value condition value |
|
422 | - * @param array $data |
|
423 | - * @param string $attribute condition column name |
|
424 | - * @return void |
|
425 | - */ |
|
426 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
427 | - { |
|
428 | - if ($attribute == 'id') |
|
429 | - { |
|
430 | - $model = $this->model->lockForUpdate()->find($value); |
|
431 | - $model ? $model->update($data) : 0; |
|
432 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
433 | - } |
|
434 | - else |
|
435 | - { |
|
436 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
437 | - $model->update($data); |
|
438 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
439 | - }); |
|
440 | - } |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Delete record from the storage based on the given |
|
445 | - * condition. |
|
446 | - * |
|
447 | - * @param var $value condition value |
|
448 | - * @param string $attribute condition column name |
|
449 | - * @return void |
|
450 | - */ |
|
451 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
452 | - { |
|
453 | - if ($attribute == 'id') |
|
454 | - { |
|
455 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
456 | - $model = $this->model->lockForUpdate()->find($value); |
|
457 | - if ( ! $model) |
|
458 | - { |
|
459 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
460 | - } |
|
417 | + /** |
|
418 | + * Update record in the storage based on the given |
|
419 | + * condition. |
|
420 | + * |
|
421 | + * @param var $value condition value |
|
422 | + * @param array $data |
|
423 | + * @param string $attribute condition column name |
|
424 | + * @return void |
|
425 | + */ |
|
426 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
427 | + { |
|
428 | + if ($attribute == 'id') |
|
429 | + { |
|
430 | + $model = $this->model->lockForUpdate()->find($value); |
|
431 | + $model ? $model->update($data) : 0; |
|
432 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
433 | + } |
|
434 | + else |
|
435 | + { |
|
436 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
437 | + $model->update($data); |
|
438 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
439 | + }); |
|
440 | + } |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Delete record from the storage based on the given |
|
445 | + * condition. |
|
446 | + * |
|
447 | + * @param var $value condition value |
|
448 | + * @param string $attribute condition column name |
|
449 | + * @return void |
|
450 | + */ |
|
451 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
452 | + { |
|
453 | + if ($attribute == 'id') |
|
454 | + { |
|
455 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
456 | + $model = $this->model->lockForUpdate()->find($value); |
|
457 | + if ( ! $model) |
|
458 | + { |
|
459 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
460 | + } |
|
461 | 461 | |
462 | - $model->delete(); |
|
463 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
464 | - }); |
|
465 | - } |
|
466 | - else |
|
467 | - { |
|
468 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
469 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
470 | - $model->delete(); |
|
471 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
472 | - }); |
|
473 | - }); |
|
474 | - } |
|
475 | - } |
|
462 | + $model->delete(); |
|
463 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
464 | + }); |
|
465 | + } |
|
466 | + else |
|
467 | + { |
|
468 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
469 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
470 | + $model->delete(); |
|
471 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
472 | + }); |
|
473 | + }); |
|
474 | + } |
|
475 | + } |
|
476 | 476 | |
477 | - /** |
|
478 | - * Fetch records from the storage based on the given |
|
479 | - * id. |
|
480 | - * |
|
481 | - * @param integer $id |
|
482 | - * @param array $relations |
|
483 | - * @param array $columns |
|
484 | - * @return object |
|
485 | - */ |
|
486 | - public function find($id, $relations = [], $columns = array('*')) |
|
487 | - { |
|
488 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
489 | - } |
|
477 | + /** |
|
478 | + * Fetch records from the storage based on the given |
|
479 | + * id. |
|
480 | + * |
|
481 | + * @param integer $id |
|
482 | + * @param array $relations |
|
483 | + * @param array $columns |
|
484 | + * @return object |
|
485 | + */ |
|
486 | + public function find($id, $relations = [], $columns = array('*')) |
|
487 | + { |
|
488 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
489 | + } |
|
490 | 490 | |
491 | - /** |
|
492 | - * Fetch records from the storage based on the given |
|
493 | - * condition. |
|
494 | - * |
|
495 | - * @param array $conditions array of conditions |
|
496 | - * @param array $relations |
|
497 | - * @param string $sortBy |
|
498 | - * @param boolean $desc |
|
499 | - * @param array $columns |
|
500 | - * @return collection |
|
501 | - */ |
|
502 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
503 | - { |
|
504 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
505 | - $sort = $desc ? 'desc' : 'asc'; |
|
506 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
507 | - } |
|
508 | - |
|
509 | - /** |
|
510 | - * Fetch the first record from the storage based on the given |
|
511 | - * condition. |
|
512 | - * |
|
513 | - * @param array $conditions array of conditions |
|
514 | - * @param array $relations |
|
515 | - * @param array $columns |
|
516 | - * @return object |
|
517 | - */ |
|
518 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
519 | - { |
|
520 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
521 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * Return the deleted models in pages based on the given conditions. |
|
526 | - * |
|
527 | - * @param array $conditions array of conditions |
|
528 | - * @param integer $perPage |
|
529 | - * @param string $sortBy |
|
530 | - * @param boolean $desc |
|
531 | - * @param array $columns |
|
532 | - * @return collection |
|
533 | - */ |
|
534 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
535 | - { |
|
536 | - unset($conditions['page']); |
|
537 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
538 | - $sort = $desc ? 'desc' : 'asc'; |
|
539 | - $model = $this->model->onlyTrashed(); |
|
540 | - |
|
541 | - if (count($conditions['conditionValues'])) |
|
542 | - { |
|
543 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
544 | - } |
|
545 | - |
|
546 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Restore the deleted model. |
|
551 | - * |
|
552 | - * @param integer $id |
|
553 | - * @return void |
|
554 | - */ |
|
555 | - public function restore($id) |
|
556 | - { |
|
557 | - $model = $this->model->onlyTrashed()->find($id); |
|
558 | - |
|
559 | - if ( ! $model) |
|
560 | - { |
|
561 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
562 | - } |
|
563 | - |
|
564 | - $model->restore(); |
|
565 | - } |
|
566 | - |
|
567 | - /** |
|
568 | - * Build the conditions recursively for the retrieving methods. |
|
569 | - * @param array $conditions |
|
570 | - * @return array |
|
571 | - */ |
|
572 | - protected function constructConditions($conditions, $model) |
|
573 | - { |
|
574 | - $conditionString = ''; |
|
575 | - $conditionValues = []; |
|
576 | - foreach ($conditions as $key => $value) |
|
577 | - { |
|
578 | - if ($key == 'and') |
|
579 | - { |
|
580 | - $conditions = $this->constructConditions($value, $model); |
|
581 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
582 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
583 | - } |
|
584 | - else if ($key == 'or') |
|
585 | - { |
|
586 | - $conditions = $this->constructConditions($value, $model); |
|
587 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
588 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
589 | - } |
|
590 | - else |
|
591 | - { |
|
592 | - if (is_array($value)) |
|
593 | - { |
|
594 | - $operator = $value['op']; |
|
595 | - if (strtolower($operator) == 'between') |
|
596 | - { |
|
597 | - $value1 = $value['val1']; |
|
598 | - $value2 = $value['val2']; |
|
599 | - } |
|
600 | - else |
|
601 | - { |
|
602 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
603 | - } |
|
604 | - } |
|
605 | - else |
|
606 | - { |
|
607 | - $operator = '='; |
|
608 | - } |
|
491 | + /** |
|
492 | + * Fetch records from the storage based on the given |
|
493 | + * condition. |
|
494 | + * |
|
495 | + * @param array $conditions array of conditions |
|
496 | + * @param array $relations |
|
497 | + * @param string $sortBy |
|
498 | + * @param boolean $desc |
|
499 | + * @param array $columns |
|
500 | + * @return collection |
|
501 | + */ |
|
502 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
503 | + { |
|
504 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
505 | + $sort = $desc ? 'desc' : 'asc'; |
|
506 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
507 | + } |
|
508 | + |
|
509 | + /** |
|
510 | + * Fetch the first record from the storage based on the given |
|
511 | + * condition. |
|
512 | + * |
|
513 | + * @param array $conditions array of conditions |
|
514 | + * @param array $relations |
|
515 | + * @param array $columns |
|
516 | + * @return object |
|
517 | + */ |
|
518 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
519 | + { |
|
520 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
521 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * Return the deleted models in pages based on the given conditions. |
|
526 | + * |
|
527 | + * @param array $conditions array of conditions |
|
528 | + * @param integer $perPage |
|
529 | + * @param string $sortBy |
|
530 | + * @param boolean $desc |
|
531 | + * @param array $columns |
|
532 | + * @return collection |
|
533 | + */ |
|
534 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
535 | + { |
|
536 | + unset($conditions['page']); |
|
537 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
538 | + $sort = $desc ? 'desc' : 'asc'; |
|
539 | + $model = $this->model->onlyTrashed(); |
|
540 | + |
|
541 | + if (count($conditions['conditionValues'])) |
|
542 | + { |
|
543 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
544 | + } |
|
545 | + |
|
546 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Restore the deleted model. |
|
551 | + * |
|
552 | + * @param integer $id |
|
553 | + * @return void |
|
554 | + */ |
|
555 | + public function restore($id) |
|
556 | + { |
|
557 | + $model = $this->model->onlyTrashed()->find($id); |
|
558 | + |
|
559 | + if ( ! $model) |
|
560 | + { |
|
561 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
562 | + } |
|
563 | + |
|
564 | + $model->restore(); |
|
565 | + } |
|
566 | + |
|
567 | + /** |
|
568 | + * Build the conditions recursively for the retrieving methods. |
|
569 | + * @param array $conditions |
|
570 | + * @return array |
|
571 | + */ |
|
572 | + protected function constructConditions($conditions, $model) |
|
573 | + { |
|
574 | + $conditionString = ''; |
|
575 | + $conditionValues = []; |
|
576 | + foreach ($conditions as $key => $value) |
|
577 | + { |
|
578 | + if ($key == 'and') |
|
579 | + { |
|
580 | + $conditions = $this->constructConditions($value, $model); |
|
581 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
582 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
583 | + } |
|
584 | + else if ($key == 'or') |
|
585 | + { |
|
586 | + $conditions = $this->constructConditions($value, $model); |
|
587 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
588 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
589 | + } |
|
590 | + else |
|
591 | + { |
|
592 | + if (is_array($value)) |
|
593 | + { |
|
594 | + $operator = $value['op']; |
|
595 | + if (strtolower($operator) == 'between') |
|
596 | + { |
|
597 | + $value1 = $value['val1']; |
|
598 | + $value2 = $value['val2']; |
|
599 | + } |
|
600 | + else |
|
601 | + { |
|
602 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
603 | + } |
|
604 | + } |
|
605 | + else |
|
606 | + { |
|
607 | + $operator = '='; |
|
608 | + } |
|
609 | 609 | |
610 | - if (strtolower($operator) == 'between') |
|
611 | - { |
|
612 | - $conditionString .= $key . ' >= ? and '; |
|
613 | - $conditionValues[] = $value1; |
|
614 | - |
|
615 | - $conditionString .= $key . ' <= ? {op} '; |
|
616 | - $conditionValues[] = $value2; |
|
617 | - } |
|
618 | - elseif (strtolower($operator) == 'in') |
|
619 | - { |
|
620 | - $conditionValues = array_merge($conditionValues, $value); |
|
621 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
622 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
623 | - } |
|
624 | - elseif (strtolower($operator) == 'null') |
|
625 | - { |
|
626 | - $conditionString .= $key . ' is null {op} '; |
|
627 | - } |
|
628 | - elseif (strtolower($operator) == 'not null') |
|
629 | - { |
|
630 | - $conditionString .= $key . ' is not null {op} '; |
|
631 | - } |
|
632 | - elseif (strtolower($operator) == 'has') |
|
633 | - { |
|
634 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
635 | - $conditions = $this->constructConditions($value, $model->first()->$key); |
|
636 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
637 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
638 | - } |
|
639 | - else |
|
640 | - { |
|
641 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
642 | - $conditionValues[] = $value; |
|
643 | - } |
|
644 | - } |
|
645 | - } |
|
646 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
647 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
648 | - } |
|
649 | - |
|
650 | - /** |
|
651 | - * Abstract method that return the necessary |
|
652 | - * information (full model namespace) |
|
653 | - * needed to preform the previous actions. |
|
654 | - * |
|
655 | - * @return string |
|
656 | - */ |
|
657 | - abstract protected function getModel(); |
|
610 | + if (strtolower($operator) == 'between') |
|
611 | + { |
|
612 | + $conditionString .= $key . ' >= ? and '; |
|
613 | + $conditionValues[] = $value1; |
|
614 | + |
|
615 | + $conditionString .= $key . ' <= ? {op} '; |
|
616 | + $conditionValues[] = $value2; |
|
617 | + } |
|
618 | + elseif (strtolower($operator) == 'in') |
|
619 | + { |
|
620 | + $conditionValues = array_merge($conditionValues, $value); |
|
621 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
622 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
623 | + } |
|
624 | + elseif (strtolower($operator) == 'null') |
|
625 | + { |
|
626 | + $conditionString .= $key . ' is null {op} '; |
|
627 | + } |
|
628 | + elseif (strtolower($operator) == 'not null') |
|
629 | + { |
|
630 | + $conditionString .= $key . ' is not null {op} '; |
|
631 | + } |
|
632 | + elseif (strtolower($operator) == 'has') |
|
633 | + { |
|
634 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
635 | + $conditions = $this->constructConditions($value, $model->first()->$key); |
|
636 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
637 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
638 | + } |
|
639 | + else |
|
640 | + { |
|
641 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
642 | + $conditionValues[] = $value; |
|
643 | + } |
|
644 | + } |
|
645 | + } |
|
646 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
647 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
648 | + } |
|
649 | + |
|
650 | + /** |
|
651 | + * Abstract method that return the necessary |
|
652 | + * information (full model namespace) |
|
653 | + * needed to preform the previous actions. |
|
654 | + * |
|
655 | + * @return string |
|
656 | + */ |
|
657 | + abstract protected function getModel(); |
|
658 | 658 | } |
659 | 659 | \ No newline at end of file |
@@ -63,14 +63,14 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * Construct the select conditions for the model. |
65 | 65 | */ |
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
66 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
67 | 67 | |
68 | 68 | if (count($conditionColumns)) |
69 | 69 | { |
70 | 70 | /** |
71 | 71 | * Use the first element in the model columns to construct the first condition. |
72 | 72 | */ |
73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
73 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | */ |
79 | 79 | foreach ($conditionColumns as $column) |
80 | 80 | { |
81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
81 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | /** |
100 | 100 | * Construct the relation condition. |
101 | 101 | */ |
102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
102 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
103 | 103 | |
104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
104 | + $subModel->where(function($q) use ($query, $relation){ |
|
105 | 105 | |
106 | 106 | /** |
107 | 107 | * Get columns of the relation. |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * Use the first element in the relation model columns to construct the first condition. |
115 | 115 | */ |
116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
116 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | foreach ($subConditionColumns as $subConditionColumn) |
123 | 123 | { |
124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
124 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
125 | 125 | } |
126 | 126 | }); |
127 | 127 | |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $modelClass = $this->model; |
183 | 183 | $relations = []; |
184 | 184 | |
185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
185 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
186 | 186 | /** |
187 | 187 | * If the id is present in the data then select the model for updating, |
188 | 188 | * else create new model. |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
192 | 192 | if ( ! $model) |
193 | 193 | { |
194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
194 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | */ |
250 | 250 | if ( ! $relationModel) |
251 | 251 | { |
252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
252 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /** |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | /** |
261 | 261 | * Prevent the sub relations or attributes not in the fillable. |
262 | 262 | */ |
263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
264 | 264 | { |
265 | 265 | $relationModel->$attr = $val; |
266 | 266 | } |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | */ |
291 | 291 | if ( ! $relationModel) |
292 | 292 | { |
293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
293 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | foreach ($value as $relationAttribute => $relationValue) |
@@ -433,7 +433,7 @@ discard block |
||
433 | 433 | } |
434 | 434 | else |
435 | 435 | { |
436 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
436 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
437 | 437 | $model->update($data); |
438 | 438 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
439 | 439 | }); |
@@ -452,11 +452,11 @@ discard block |
||
452 | 452 | { |
453 | 453 | if ($attribute == 'id') |
454 | 454 | { |
455 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
455 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
456 | 456 | $model = $this->model->lockForUpdate()->find($value); |
457 | 457 | if ( ! $model) |
458 | 458 | { |
459 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
459 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
460 | 460 | } |
461 | 461 | |
462 | 462 | $model->delete(); |
@@ -465,8 +465,8 @@ discard block |
||
465 | 465 | } |
466 | 466 | else |
467 | 467 | { |
468 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
469 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
|
468 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
469 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($saveLog){ |
|
470 | 470 | $model->delete(); |
471 | 471 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
472 | 472 | }); |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | { |
504 | 504 | $conditions = $this->constructConditions($conditions, $this->model); |
505 | 505 | $sort = $desc ? 'desc' : 'asc'; |
506 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
506 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
507 | 507 | } |
508 | 508 | |
509 | 509 | /** |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
544 | 544 | } |
545 | 545 | |
546 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
546 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
547 | 547 | } |
548 | 548 | |
549 | 549 | /** |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | |
559 | 559 | if ( ! $model) |
560 | 560 | { |
561 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
561 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
562 | 562 | } |
563 | 563 | |
564 | 564 | $model->restore(); |
@@ -578,13 +578,13 @@ discard block |
||
578 | 578 | if ($key == 'and') |
579 | 579 | { |
580 | 580 | $conditions = $this->constructConditions($value, $model); |
581 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
581 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
582 | 582 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
583 | 583 | } |
584 | 584 | else if ($key == 'or') |
585 | 585 | { |
586 | 586 | $conditions = $this->constructConditions($value, $model); |
587 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
587 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
588 | 588 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
589 | 589 | } |
590 | 590 | else |
@@ -609,41 +609,41 @@ discard block |
||
609 | 609 | |
610 | 610 | if (strtolower($operator) == 'between') |
611 | 611 | { |
612 | - $conditionString .= $key . ' >= ? and '; |
|
612 | + $conditionString .= $key.' >= ? and '; |
|
613 | 613 | $conditionValues[] = $value1; |
614 | 614 | |
615 | - $conditionString .= $key . ' <= ? {op} '; |
|
615 | + $conditionString .= $key.' <= ? {op} '; |
|
616 | 616 | $conditionValues[] = $value2; |
617 | 617 | } |
618 | 618 | elseif (strtolower($operator) == 'in') |
619 | 619 | { |
620 | 620 | $conditionValues = array_merge($conditionValues, $value); |
621 | 621 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
622 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
622 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
623 | 623 | } |
624 | 624 | elseif (strtolower($operator) == 'null') |
625 | 625 | { |
626 | - $conditionString .= $key . ' is null {op} '; |
|
626 | + $conditionString .= $key.' is null {op} '; |
|
627 | 627 | } |
628 | 628 | elseif (strtolower($operator) == 'not null') |
629 | 629 | { |
630 | - $conditionString .= $key . ' is not null {op} '; |
|
630 | + $conditionString .= $key.' is not null {op} '; |
|
631 | 631 | } |
632 | 632 | elseif (strtolower($operator) == 'has') |
633 | 633 | { |
634 | 634 | $sql = $model->withTrashed()->has($key)->toSql(); |
635 | 635 | $conditions = $this->constructConditions($value, $model->first()->$key); |
636 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
636 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
637 | 637 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
638 | 638 | } |
639 | 639 | else |
640 | 640 | { |
641 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
641 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
642 | 642 | $conditionValues[] = $value; |
643 | 643 | } |
644 | 644 | } |
645 | 645 | } |
646 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
646 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
647 | 647 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
648 | 648 | } |
649 | 649 |
@@ -430,8 +430,7 @@ discard block |
||
430 | 430 | $model = $this->model->lockForUpdate()->find($value); |
431 | 431 | $model ? $model->update($data) : 0; |
432 | 432 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
433 | - } |
|
434 | - else |
|
433 | + } else |
|
435 | 434 | { |
436 | 435 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
437 | 436 | $model->update($data); |
@@ -462,8 +461,7 @@ discard block |
||
462 | 461 | $model->delete(); |
463 | 462 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
464 | 463 | }); |
465 | - } |
|
466 | - else |
|
464 | + } else |
|
467 | 465 | { |
468 | 466 | \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
469 | 467 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($saveLog){ |
@@ -580,14 +578,12 @@ discard block |
||
580 | 578 | $conditions = $this->constructConditions($value, $model); |
581 | 579 | $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
582 | 580 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
583 | - } |
|
584 | - else if ($key == 'or') |
|
581 | + } else if ($key == 'or') |
|
585 | 582 | { |
586 | 583 | $conditions = $this->constructConditions($value, $model); |
587 | 584 | $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
588 | 585 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
589 | - } |
|
590 | - else |
|
586 | + } else |
|
591 | 587 | { |
592 | 588 | if (is_array($value)) |
593 | 589 | { |
@@ -596,13 +592,11 @@ discard block |
||
596 | 592 | { |
597 | 593 | $value1 = $value['val1']; |
598 | 594 | $value2 = $value['val2']; |
599 | - } |
|
600 | - else |
|
595 | + } else |
|
601 | 596 | { |
602 | 597 | $value = array_key_exists('val', $value) ? $value['val'] : ''; |
603 | 598 | } |
604 | - } |
|
605 | - else |
|
599 | + } else |
|
606 | 600 | { |
607 | 601 | $operator = '='; |
608 | 602 | } |
@@ -614,29 +608,24 @@ discard block |
||
614 | 608 | |
615 | 609 | $conditionString .= $key . ' <= ? {op} '; |
616 | 610 | $conditionValues[] = $value2; |
617 | - } |
|
618 | - elseif (strtolower($operator) == 'in') |
|
611 | + } elseif (strtolower($operator) == 'in') |
|
619 | 612 | { |
620 | 613 | $conditionValues = array_merge($conditionValues, $value); |
621 | 614 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
622 | 615 | $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
623 | - } |
|
624 | - elseif (strtolower($operator) == 'null') |
|
616 | + } elseif (strtolower($operator) == 'null') |
|
625 | 617 | { |
626 | 618 | $conditionString .= $key . ' is null {op} '; |
627 | - } |
|
628 | - elseif (strtolower($operator) == 'not null') |
|
619 | + } elseif (strtolower($operator) == 'not null') |
|
629 | 620 | { |
630 | 621 | $conditionString .= $key . ' is not null {op} '; |
631 | - } |
|
632 | - elseif (strtolower($operator) == 'has') |
|
622 | + } elseif (strtolower($operator) == 'has') |
|
633 | 623 | { |
634 | 624 | $sql = $model->withTrashed()->has($key)->toSql(); |
635 | 625 | $conditions = $this->constructConditions($value, $model->first()->$key); |
636 | 626 | $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
637 | 627 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
638 | - } |
|
639 | - else |
|
628 | + } else |
|
640 | 629 | { |
641 | 630 | $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
642 | 631 | $conditionValues[] = $value; |
@@ -4,39 +4,39 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepositoryContainer implements RepositoryContainerInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Construct the repository class name based on |
|
9 | - * the method name called, search in the |
|
10 | - * given namespaces for the class and |
|
11 | - * return an instance. |
|
12 | - * |
|
13 | - * @param string $name the called method name |
|
14 | - * @param array $arguments the method arguments |
|
15 | - * @return object |
|
16 | - */ |
|
17 | - public function __call($name, $arguments) |
|
18 | - { |
|
19 | - foreach ($this->getRepoNameSpace() as $repoNameSpace) |
|
20 | - { |
|
21 | - $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
22 | - if (class_exists($class)) |
|
23 | - { |
|
24 | - \App::singleton($class, function ($app) use ($class) { |
|
7 | + /** |
|
8 | + * Construct the repository class name based on |
|
9 | + * the method name called, search in the |
|
10 | + * given namespaces for the class and |
|
11 | + * return an instance. |
|
12 | + * |
|
13 | + * @param string $name the called method name |
|
14 | + * @param array $arguments the method arguments |
|
15 | + * @return object |
|
16 | + */ |
|
17 | + public function __call($name, $arguments) |
|
18 | + { |
|
19 | + foreach ($this->getRepoNameSpace() as $repoNameSpace) |
|
20 | + { |
|
21 | + $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
22 | + if (class_exists($class)) |
|
23 | + { |
|
24 | + \App::singleton($class, function ($app) use ($class) { |
|
25 | 25 | |
26 | - return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
|
27 | - }); |
|
26 | + return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
|
27 | + }); |
|
28 | 28 | |
29 | - return \App::make($class); |
|
30 | - } |
|
31 | - } |
|
32 | - } |
|
29 | + return \App::make($class); |
|
30 | + } |
|
31 | + } |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Abstract methods that return the necessary |
|
36 | - * information (repositories namespaces) |
|
37 | - * needed to preform the previous actions. |
|
38 | - * |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - abstract protected function getRepoNameSpace(); |
|
34 | + /** |
|
35 | + * Abstract methods that return the necessary |
|
36 | + * information (repositories namespaces) |
|
37 | + * needed to preform the previous actions. |
|
38 | + * |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + abstract protected function getRepoNameSpace(); |
|
42 | 42 | } |
43 | 43 | \ No newline at end of file |
@@ -18,10 +18,10 @@ |
||
18 | 18 | { |
19 | 19 | foreach ($this->getRepoNameSpace() as $repoNameSpace) |
20 | 20 | { |
21 | - $class = rtrim($repoNameSpace, '\\') . '\\' . ucfirst(str_singular($name)) . 'Repository'; |
|
21 | + $class = rtrim($repoNameSpace, '\\').'\\'.ucfirst(str_singular($name)).'Repository'; |
|
22 | 22 | if (class_exists($class)) |
23 | 23 | { |
24 | - \App::singleton($class, function ($app) use ($class) { |
|
24 | + \App::singleton($class, function($app) use ($class) { |
|
25 | 25 | |
26 | 26 | return new \App\Modules\V1\Core\Decorators\CachingDecorator(new $class, $app['cache.store']); |
27 | 27 | }); |