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 runHashedIdsDecoder() |
||
30 | { |
||
31 | if (Config::get('hello.hash-id')) { |
||
32 | Route::bind('id', function ($id, $route) { |
||
33 | // skip decoding some endpoints |
||
34 | if (!in_array($route->uri(), $this->skippedEndpoints)) { |
||
35 | |||
36 | // decode the ID in the URL |
||
37 | $decoded = $this->decoder($id); |
||
38 | |||
39 | if (empty($decoded)) { |
||
40 | throw new IncorrectIdException('ID (' . $id . ') is incorrect, consider using the hashed ID |
||
41 | instead of the numeric ID.'); |
||
42 | } |
||
43 | |||
44 | return $decoded[0]; |
||
45 | } |
||
46 | }); |
||
47 | } |
||
48 | } |
||
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 decodeHashedIdsBeforeValidation(Array $requestData) |
||
87 | |||
88 | /** |
||
89 | * Expected Keys formats: |
||
90 | * |
||
91 | * Type 1: |
||
92 | * A |
||
93 | * Type 2: |
||
94 | * A.*.B |
||
95 | * A.*.B.*.C |
||
96 | * Type 3: |
||
97 | * A.* |
||
98 | * A.*.B.* |
||
99 | * |
||
100 | * @param $requestData |
||
101 | * @param $key |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | private function locateAndDecodeIds($requestData, $key) |
||
120 | |||
121 | /** |
||
122 | * @param $requestData |
||
123 | * @param $key |
||
124 | */ |
||
125 | private function decodeType1Key(&$requestData, $key) |
||
132 | |||
133 | /** |
||
134 | * @param $requestData |
||
135 | * @param $key |
||
136 | */ |
||
137 | private function decodeType2Key(&$requestData, $key) |
||
151 | |||
152 | /** |
||
153 | * @param $requestData |
||
154 | * @param $key |
||
155 | */ |
||
156 | private function decodeType3Key(&$requestData, $key) |
||
176 | |||
177 | /** |
||
178 | * @param $subject |
||
179 | * @param $findKey |
||
180 | * @param $callback |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function findKeyAndReturnValue(&$subject, $findKey, $callback) |
||
202 | |||
203 | /** |
||
204 | * @param $search |
||
205 | * @param $subject |
||
206 | * |
||
207 | * @return mixed |
||
208 | */ |
||
209 | private function removeLastOccurrenceFromString($subject, $search) |
||
221 | |||
222 | /** |
||
223 | * @param $needle |
||
224 | * @param $haystack |
||
225 | * |
||
226 | * @return int |
||
227 | */ |
||
228 | private function stringEndsWithChars($needle, $haystack) |
||
232 | |||
233 | /** |
||
234 | * @param array $ids |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | public function decodeArray(array $ids) |
||
247 | |||
248 | /** |
||
249 | * @param $id |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function decode($id) |
||
261 | |||
262 | /** |
||
263 | * @param $id |
||
264 | * |
||
265 | * @return mixed |
||
266 | */ |
||
267 | public function encode($id) |
||
271 | |||
272 | /** |
||
273 | * @param $id |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | private function decoder($id) |
||
281 | |||
282 | /** |
||
283 | * @param $id |
||
284 | * |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function encoder($id) |
||
291 | |||
292 | } |
||
293 |
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.