1 | <?php |
||
23 | trait ApplicationUriDomainTrait { |
||
24 | |||
25 | /** |
||
26 | */ |
||
27 | protected $pool; |
||
28 | |||
29 | /** |
||
30 | * Creates a new ApplicationUri with the provided data |
||
31 | * |
||
32 | * @param mixed $data |
||
33 | * @return PayloadInterface |
||
34 | */ |
||
35 | public function create($data) { |
||
36 | // hydrate |
||
37 | $serializer = ApplicationUri::getSerializer(); |
||
38 | $model = $serializer->hydrate(new ApplicationUri(), $data); |
||
39 | $this->hydrateRelationships($model, $data); |
||
|
|||
40 | |||
41 | // dispatch pre save hooks |
||
42 | $this->dispatch(ApplicationUriEvent::PRE_CREATE, $model, $data); |
||
43 | $this->dispatch(ApplicationUriEvent::PRE_SAVE, $model, $data); |
||
44 | |||
45 | // validate |
||
46 | $validator = $this->getValidator(); |
||
47 | if ($validator !== null && !$validator->validate($model)) { |
||
48 | return new NotValid([ |
||
49 | 'errors' => $validator->getValidationFailures() |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | // save and dispatch post save hooks |
||
54 | $model->save(); |
||
55 | $this->dispatch(ApplicationUriEvent::POST_CREATE, $model, $data); |
||
56 | $this->dispatch(ApplicationUriEvent::POST_SAVE, $model, $data); |
||
57 | |||
58 | return new Created(['model' => $model]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Deletes a ApplicationUri with the given id |
||
63 | * |
||
64 | * @param mixed $id |
||
65 | * @return PayloadInterface |
||
66 | */ |
||
67 | public function delete($id) { |
||
68 | // find |
||
69 | $model = $this->get($id); |
||
70 | |||
71 | if ($model === null) { |
||
72 | return new NotFound(['message' => 'ApplicationUri not found.']); |
||
73 | } |
||
74 | |||
75 | // delete |
||
76 | $this->dispatch(ApplicationUriEvent::PRE_DELETE, $model); |
||
77 | $model->delete(); |
||
78 | |||
79 | if ($model->isDeleted()) { |
||
80 | $this->dispatch(ApplicationUriEvent::POST_DELETE, $model); |
||
81 | return new Deleted(['model' => $model]); |
||
82 | } |
||
83 | |||
84 | return new NotDeleted(['message' => 'Could not delete ApplicationUri']); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns a paginated result |
||
89 | * |
||
90 | * @param Parameters $params |
||
91 | * @return PayloadInterface |
||
92 | */ |
||
93 | public function paginate(Parameters $params) { |
||
120 | |||
121 | /** |
||
122 | * Returns one ApplicationUri with the given id |
||
123 | * |
||
124 | * @param mixed $id |
||
125 | * @return PayloadInterface |
||
126 | */ |
||
127 | public function read($id) { |
||
138 | |||
139 | /** |
||
140 | * Sets the Application id |
||
141 | * |
||
142 | * @param mixed $id |
||
143 | * @param mixed $relatedId |
||
144 | * @return PayloadInterface |
||
145 | */ |
||
146 | public function setApplicationId($id, $relatedId) { |
||
147 | // find |
||
148 | $model = $this->get($id); |
||
149 | |||
150 | if ($model === null) { |
||
151 | return new NotFound(['message' => 'ApplicationUri not found.']); |
||
152 | } |
||
153 | |||
154 | // update |
||
155 | if ($this->doSetApplicationId($model, $relatedId)) { |
||
156 | $this->dispatch(ApplicationUriEvent::PRE_APPLICATION_UPDATE, $model); |
||
157 | $this->dispatch(ApplicationUriEvent::PRE_SAVE, $model); |
||
158 | $model->save(); |
||
159 | $this->dispatch(ApplicationUriEvent::POST_APPLICATION_UPDATE, $model); |
||
160 | $this->dispatch(ApplicationUriEvent::POST_SAVE, $model); |
||
161 | |||
162 | return Updated(['model' => $model]); |
||
163 | } |
||
164 | |||
165 | return NotUpdated(['model' => $model]); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Sets the Localization id |
||
170 | * |
||
171 | * @param mixed $id |
||
172 | * @param mixed $relatedId |
||
173 | * @return PayloadInterface |
||
174 | */ |
||
175 | public function setLocalizationId($id, $relatedId) { |
||
176 | // find |
||
177 | $model = $this->get($id); |
||
178 | |||
179 | if ($model === null) { |
||
180 | return new NotFound(['message' => 'ApplicationUri not found.']); |
||
181 | } |
||
182 | |||
183 | // update |
||
184 | if ($this->doSetLocalizationId($model, $relatedId)) { |
||
185 | $this->dispatch(ApplicationUriEvent::PRE_LOCALIZATION_UPDATE, $model); |
||
186 | $this->dispatch(ApplicationUriEvent::PRE_SAVE, $model); |
||
187 | $model->save(); |
||
188 | $this->dispatch(ApplicationUriEvent::POST_LOCALIZATION_UPDATE, $model); |
||
189 | $this->dispatch(ApplicationUriEvent::POST_SAVE, $model); |
||
190 | |||
191 | return Updated(['model' => $model]); |
||
192 | } |
||
193 | |||
194 | return NotUpdated(['model' => $model]); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Updates a ApplicationUri with the given idand the provided data |
||
199 | * |
||
200 | * @param mixed $id |
||
201 | * @param mixed $data |
||
202 | * @return PayloadInterface |
||
203 | */ |
||
204 | public function update($id, $data) { |
||
205 | // find |
||
206 | $model = $this->get($id); |
||
207 | |||
208 | if ($model === null) { |
||
209 | return new NotFound(['message' => 'ApplicationUri not found.']); |
||
210 | } |
||
211 | |||
212 | // hydrate |
||
213 | $serializer = ApplicationUri::getSerializer(); |
||
214 | $model = $serializer->hydrate($model, $data); |
||
215 | $this->hydrateRelationships($model, $data); |
||
216 | |||
217 | // dispatch pre save hooks |
||
218 | $this->dispatch(ApplicationUriEvent::PRE_UPDATE, $model, $data); |
||
219 | $this->dispatch(ApplicationUriEvent::PRE_SAVE, $model, $data); |
||
220 | |||
221 | // validate |
||
222 | $validator = $this->getValidator(); |
||
223 | if ($validator !== null && !$validator->validate($model)) { |
||
224 | return new NotValid([ |
||
225 | 'errors' => $validator->getValidationFailures() |
||
226 | ]); |
||
227 | } |
||
228 | |||
229 | // save and dispath post save hooks |
||
230 | $rows = $model->save(); |
||
231 | $this->dispatch(ApplicationUriEvent::POST_UPDATE, $model, $data); |
||
232 | $this->dispatch(ApplicationUriEvent::POST_SAVE, $model, $data); |
||
233 | |||
234 | $payload = ['model' => $model]; |
||
235 | |||
236 | if ($rows === 0) { |
||
237 | return new NotUpdated($payload); |
||
238 | } |
||
239 | |||
240 | return new Updated($payload); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param mixed $query |
||
245 | * @param mixed $filter |
||
246 | * @return void |
||
247 | */ |
||
248 | protected function applyFilter($query, $filter) { |
||
268 | |||
269 | /** |
||
270 | * @param string $type |
||
271 | * @param ApplicationUri $model |
||
272 | * @param array $data |
||
273 | */ |
||
274 | protected function dispatch($type, ApplicationUri $model, array $data = []) { |
||
275 | $methods = [ |
||
276 | ApplicationUriEvent::PRE_CREATE => 'preCreate', |
||
277 | ApplicationUriEvent::POST_CREATE => 'postCreate', |
||
278 | ApplicationUriEvent::PRE_UPDATE => 'preUpdate', |
||
279 | ApplicationUriEvent::POST_UPDATE => 'postUpdate', |
||
280 | ApplicationUriEvent::PRE_DELETE => 'preDelete', |
||
281 | ApplicationUriEvent::POST_DELETE => 'postDelete', |
||
282 | ApplicationUriEvent::PRE_SAVE => 'preSave', |
||
283 | ApplicationUriEvent::POST_SAVE => 'postSave' |
||
284 | ]; |
||
285 | |||
286 | if (isset($methods[$type])) { |
||
287 | $method = $methods[$type]; |
||
288 | if (method_exists($this, $method)) { |
||
289 | $this->$method($model, $data); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | $dispatcher = $this->getServiceContainer()->getDispatcher(); |
||
294 | $dispatcher->dispatch($type, new ApplicationUriEvent($model)); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Internal mechanism to set the Application id |
||
299 | * |
||
300 | * @param ApplicationUri $model |
||
301 | * @param mixed $relatedId |
||
302 | */ |
||
303 | protected function doSetApplicationId(ApplicationUri $model, $relatedId) { |
||
312 | |||
313 | /** |
||
314 | * Internal mechanism to set the Localization id |
||
315 | * |
||
316 | * @param ApplicationUri $model |
||
317 | * @param mixed $relatedId |
||
318 | */ |
||
319 | protected function doSetLocalizationId(ApplicationUri $model, $relatedId) { |
||
328 | |||
329 | /** |
||
330 | * Returns one ApplicationUri with the given id from cache |
||
331 | * |
||
332 | * @param mixed $id |
||
333 | * @return ApplicationUri|null |
||
334 | */ |
||
335 | protected function get($id) { |
||
347 | |||
348 | /** |
||
349 | * Returns the service container |
||
350 | * |
||
351 | * @return ServiceContainer |
||
352 | */ |
||
353 | abstract protected function getServiceContainer(); |
||
354 | } |
||
355 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.