1 | <?php |
||
15 | trait HashIdTrait |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * endpoint to be skipped from decoding their ID's (example for external ID's) |
||
20 | * @var array |
||
21 | */ |
||
22 | private $skippedEndpoints = [ |
||
23 | // 'orders/{id}/external', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * All ID's passed with all endpoints will be decoded before entering the Application |
||
28 | */ |
||
29 | public function runEndpointsHashedIdsDecoder() |
||
49 | |||
50 | /** |
||
51 | * Will be used by the Eloquent Models (since it's used as trait there). |
||
52 | * |
||
53 | * @param null $key |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function getHashedKey($key = null) |
||
66 | |||
67 | /** |
||
68 | * without decoding the encoded ID's you won't be able to use |
||
69 | * validation features like `exists:table,id` |
||
70 | * |
||
71 | * @param array $requestData |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | protected function decodeHashedIdsBeforeApplyingValidationRules(Array $requestData) |
||
90 | |||
91 | /** |
||
92 | * Expected Keys formats: |
||
93 | * |
||
94 | * Type 1: |
||
95 | * A |
||
96 | * Type 2: |
||
97 | * A.*.B |
||
98 | * A.*.B.*.C |
||
99 | * Type 3: |
||
100 | * A.* |
||
101 | * A.*.B.* |
||
102 | * |
||
103 | * @param $requestData |
||
104 | * @param $key |
||
105 | * |
||
106 | * @return mixed |
||
107 | */ |
||
108 | private function locateAndDecodeIds($requestData, $key) |
||
124 | |||
125 | /** |
||
126 | * @param $requestData |
||
127 | * @param $key |
||
128 | */ |
||
129 | private function decodeType1Key(&$requestData, $key) |
||
136 | |||
137 | /** |
||
138 | * @param $requestData |
||
139 | * @param $key |
||
140 | */ |
||
141 | private function decodeType2Key(&$requestData, $key) |
||
155 | |||
156 | /** |
||
157 | * @param $requestData |
||
158 | * @param $key |
||
159 | */ |
||
160 | private function decodeType3Key(&$requestData, $key) |
||
181 | |||
182 | /** |
||
183 | * @param $subject |
||
184 | * @param $findKey |
||
185 | * @param $callback |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | public function findKeyAndReturnValue(&$subject, $findKey, $callback) |
||
207 | |||
208 | /** |
||
209 | * @param $search |
||
210 | * @param $subject |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | private function removeLastOccurrenceFromString($subject, $search) |
||
226 | |||
227 | /** |
||
228 | * @param $needle |
||
229 | * @param $haystack |
||
230 | * |
||
231 | * @return int |
||
232 | */ |
||
233 | private function stringEndsWithChars($needle, $haystack) |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param array $ids |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function decodeArray(array $ids) |
||
253 | |||
254 | /** |
||
255 | * @param $id |
||
256 | * |
||
257 | * @return mixed |
||
258 | */ |
||
259 | public function decode($id) |
||
267 | |||
268 | /** |
||
269 | * @param $id |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public function encode($id) |
||
277 | |||
278 | /** |
||
279 | * @param $id |
||
280 | * |
||
281 | * @return mixed |
||
282 | */ |
||
283 | private function decoder($id) |
||
287 | |||
288 | /** |
||
289 | * @param $id |
||
290 | * |
||
291 | * @return mixed |
||
292 | */ |
||
293 | public function encoder($id) |
||
297 | |||
298 | } |
||
299 |
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.