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 | * Will be used by the Eloquent Models (since it's used as trait there). |
||
28 | * |
||
29 | * @param null $key |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function getHashedKey($key = null) |
||
42 | |||
43 | /** |
||
44 | * without decoding the encoded ID's you won't be able to use |
||
45 | * validation features like `exists:table,id` |
||
46 | * |
||
47 | * @param array $requestData |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | protected function decodeHashedIdsBeforeValidation(Array $requestData) |
||
63 | |||
64 | /** |
||
65 | * Expected Keys formats: |
||
66 | * |
||
67 | * Type 1: |
||
68 | * A |
||
69 | * Type 2: |
||
70 | * A.*.B |
||
71 | * A.*.B.*.C |
||
72 | * Type 3: |
||
73 | * A.* |
||
74 | * A.*.B.* |
||
75 | * |
||
76 | * @param $requestData |
||
77 | * @param $key |
||
78 | * |
||
79 | * @return mixed |
||
80 | */ |
||
81 | private function locateAndDecodeIds($requestData, $key) |
||
96 | |||
97 | /** |
||
98 | * @param $requestData |
||
99 | * @param $key |
||
100 | */ |
||
101 | private function decodeType1Key(&$requestData, $key) |
||
108 | |||
109 | /** |
||
110 | * @param $requestData |
||
111 | * @param $key |
||
112 | */ |
||
113 | private function decodeType2Key(&$requestData, $key) |
||
126 | |||
127 | /** |
||
128 | * @param $requestData |
||
129 | * @param $key |
||
130 | */ |
||
131 | private function decodeType3Key(&$requestData, $key) |
||
151 | |||
152 | /** |
||
153 | * @param $subject |
||
154 | * @param $findKey |
||
155 | * @param $callback |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function findKeyAndReturnValue(&$subject, $findKey, $callback) |
||
177 | |||
178 | /** |
||
179 | * @param $search |
||
180 | * @param $subject |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | private function removeLastOccurrenceFromString($subject, $search) |
||
196 | |||
197 | /** |
||
198 | * @param $needle |
||
199 | * @param $haystack |
||
200 | * |
||
201 | * @return int |
||
202 | */ |
||
203 | private function stringEndsWithChars($needle, $haystack) |
||
207 | |||
208 | /** |
||
209 | * @param array $ids |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function decodeArray(array $ids) |
||
222 | |||
223 | /** |
||
224 | * @param $id |
||
225 | * @param null $parameter |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function decode($id, $parameter = null) |
||
237 | |||
238 | /** |
||
239 | * @param $id |
||
240 | * |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public function encode($id) |
||
247 | |||
248 | /** |
||
249 | * @param $id |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | private function decoder($id) |
||
257 | |||
258 | /** |
||
259 | * @param $id |
||
260 | * |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function encoder($id) |
||
267 | |||
268 | /** |
||
269 | * Automatically decode any found `id` in the URL, no need to be used anymore. |
||
270 | * Since now the user will define what needs to be decoded in the request. |
||
271 | * |
||
272 | * All ID's passed with all endpoints will be decoded before entering the Application |
||
273 | */ |
||
274 | public function runHashedIdsDecoder() |
||
294 | |||
295 | } |
||
296 |
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.