1 | <?php |
||
23 | trait ExtensionDomainTrait { |
||
24 | |||
25 | /** |
||
26 | */ |
||
27 | protected $pool; |
||
28 | |||
29 | /** |
||
30 | * Creates a new Extension with the provided data |
||
31 | * |
||
32 | * @param mixed $data |
||
33 | * @return PayloadInterface |
||
34 | */ |
||
35 | public function create($data) { |
||
36 | // hydrate |
||
37 | $serializer = Extension::getSerializer(); |
||
38 | $model = $serializer->hydrate(new Extension(), $data); |
||
39 | $this->hydrateRelationships($model, $data); |
||
|
|||
40 | |||
41 | // dispatch pre save hooks |
||
42 | $this->dispatch(ExtensionEvent::PRE_CREATE, $model, $data); |
||
43 | $this->dispatch(ExtensionEvent::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(ExtensionEvent::POST_CREATE, $model, $data); |
||
56 | $this->dispatch(ExtensionEvent::POST_SAVE, $model, $data); |
||
57 | |||
58 | return new Created(['model' => $model]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Deletes a Extension 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' => 'Extension not found.']); |
||
73 | } |
||
74 | |||
75 | // delete |
||
76 | $this->dispatch(ExtensionEvent::PRE_DELETE, $model); |
||
77 | $model->delete(); |
||
78 | |||
79 | if ($model->isDeleted()) { |
||
80 | $this->dispatch(ExtensionEvent::POST_DELETE, $model); |
||
81 | return new Deleted(['model' => $model]); |
||
82 | } |
||
83 | |||
84 | return new NotDeleted(['message' => 'Could not delete Extension']); |
||
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 Extension with the given id |
||
123 | * |
||
124 | * @param mixed $id |
||
125 | * @return PayloadInterface |
||
126 | */ |
||
127 | public function read($id) { |
||
138 | |||
139 | /** |
||
140 | * Updates a Extension with the given idand the provided data |
||
141 | * |
||
142 | * @param mixed $id |
||
143 | * @param mixed $data |
||
144 | * @return PayloadInterface |
||
145 | */ |
||
146 | public function update($id, $data) { |
||
184 | |||
185 | /** |
||
186 | * @param mixed $query |
||
187 | * @param mixed $filter |
||
188 | * @return void |
||
189 | */ |
||
190 | protected function applyFilter($query, $filter) { |
||
210 | |||
211 | /** |
||
212 | * @param string $type |
||
213 | * @param Extension $model |
||
214 | * @param array $data |
||
215 | */ |
||
216 | protected function dispatch($type, Extension $model, array $data = []) { |
||
238 | |||
239 | /** |
||
240 | * Returns one Extension with the given id from cache |
||
241 | * |
||
242 | * @param mixed $id |
||
243 | * @return Extension|null |
||
244 | */ |
||
245 | protected function get($id) { |
||
257 | |||
258 | /** |
||
259 | * Returns the service container |
||
260 | * |
||
261 | * @return ServiceContainer |
||
262 | */ |
||
263 | abstract protected function getServiceContainer(); |
||
264 | } |
||
265 |
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.