|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Port\HashId\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App\Port\Exception\Exceptions\IncorrectIdException; |
|
6
|
|
|
use Illuminate\Support\Facades\Config; |
|
7
|
|
|
use Route; |
|
8
|
|
|
use Vinkla\Hashids\Facades\Hashids; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class HashIdTrait. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
14
|
|
|
*/ |
|
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() |
|
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) |
|
58
|
|
|
{ |
|
59
|
|
|
// hash the ID only if hash-id enabled in the config |
|
60
|
|
|
if (Config::get('hello.hash-id')) { |
|
61
|
|
|
return $this->encoder(($key) ? : $this->getKey()); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->getKey(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* without decoding the encoded ID's you won't be able to use |
|
70
|
|
|
* validation features like `exists:table,id` |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $requestData |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
private function decodeHashedIdsBeforeValidatingThem(Array $requestData) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->decode as $id) { |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if (isset($requestData[$id])) { |
|
81
|
|
|
// validate the user is not trying to pass real ID |
|
82
|
|
|
if (is_numeric($requestData[$id])) { |
|
83
|
|
|
throw new IncorrectIdException('Only Hashed ID\'s allowed to be passed.'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (Config::get('hello.hash-id')) { |
|
87
|
|
|
$requestData[$id] = is_array($requestData[$id]) ? |
|
88
|
|
|
$this->decodeThisArrayOfIds($requestData[$id]) : |
|
89
|
|
|
$this->decodeThisId($requestData[$id]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} // do nothing if the input is incorrect, because what if it's not required! |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $requestData; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $ids |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function decodeThisArrayOfIds(array $ids) |
|
104
|
|
|
{ |
|
105
|
|
|
$result = []; |
|
106
|
|
|
foreach ($ids as $id) { |
|
107
|
|
|
$result[] = $this->decodeThisId($id); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $result; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $id |
|
115
|
|
|
* |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function decodeThisId($id) |
|
119
|
|
|
{ |
|
120
|
|
|
return empty($this->decoder($id)) ? [] : $this->decoder($id)[0]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $id |
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
private function decoder($id) |
|
129
|
|
|
{ |
|
130
|
|
|
return Hashids::decode($id); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $id |
|
135
|
|
|
* |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
private function encoder($id) |
|
139
|
|
|
{ |
|
140
|
|
|
return Hashids::encode($id); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.