-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathHook.php
More file actions
282 lines (246 loc) · 5.23 KB
/
Hook.php
File metadata and controls
282 lines (246 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Utopia\Http;
use Utopia\Validator;
class Hook
{
/**
* Description
*
* @var string
*/
protected string $desc = '';
/**
* Parameters
*
* List of route params names and validators
*
* @var array
*/
protected array $params = [];
/**
* Group
*
* @var array
*/
protected array $groups = [];
/**
* Labels
*
* List of route label names
*
* @var array
*/
protected array $labels = [];
/**
* Action Callback
*
* @var callable
*/
protected $action;
/**
* Injections
*
* List of route required injections for action callback
*
* @var array
*/
protected array $injections = [];
public function __construct()
{
$this->action = function (): void {
};
}
/**
* Add Description
*
* @param string $desc
* @return static
*/
public function desc(string $desc): static
{
$this->desc = $desc;
return $this;
}
/**
* Get Description
*
* @return string
*/
public function getDesc(): string
{
return $this->desc;
}
/**
* Add Group
*
* @param array $groups
* @return static
*/
public function groups(array $groups): static
{
$this->groups = $groups;
return $this;
}
/**
* Get Groups
*
* @return array
*/
public function getGroups(): array
{
return $this->groups;
}
/**
* Add Label
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function label(string $key, mixed $value): static
{
$this->labels[$key] = $value;
return $this;
}
/**
* Get Label
*
* Return given label value or default value if label doesn't exists
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getLabel(string $key, mixed $default): mixed
{
return (isset($this->labels[$key])) ? $this->labels[$key] : $default;
}
/**
* Add Action
*
* @param callable $action
* @return static
*/
public function action(callable $action): static
{
$this->action = $action;
return $this;
}
/**
* Get Action
*
* @return callable
*/
public function getAction()
{
return $this->action;
}
/**
* Get Injections
*
* @return array
*/
public function getInjections(): array
{
return $this->injections;
}
/**
* Inject
*
* @param string $injection
* @return static
*
* @throws Exception
*/
public function inject(string $injection): static
{
if (array_key_exists($injection, $this->injections)) {
throw new Exception('Injection already declared for '.$injection);
}
$this->injections[$injection] = [
'name' => $injection,
'order' => count($this->params) + count($this->injections),
];
return $this;
}
/**
* Add Param
*
* @param string $key
* @param mixed $default
* @param Validator|callable $validator
* @param string $description
* @param bool $optional
* @param array $injections
* @param bool $skipValidation
* @return static
*/
public function param(string $key, mixed $default, Validator|callable $validator, string $description = '', bool $optional = false, array $injections = [], bool $skipValidation = false): static
{
$this->params[$key] = [
'default' => $default,
'validator' => $validator,
'description' => $description,
'optional' => $optional,
'injections' => $injections,
'skipValidation' => $skipValidation,
'value' => null,
'order' => count($this->params) + count($this->injections),
];
return $this;
}
/**
* Get Params
*
* @return array
*/
public function getParams(): array
{
return $this->params;
}
/**
* Get Param Values
*
* @return array
*/
public function getParamsValues(): array
{
$values = [];
foreach ($this->params as $key => $param) {
$values[$key] = $param['value'];
}
return $values;
}
/**
* Set Param Value
*
* @param string $key
* @param mixed $value
* @return static
*
* @throws Exception
*/
public function setParamValue(string $key, mixed $value): static
{
if (!isset($this->params[$key])) {
throw new Exception('Unknown key');
}
$this->params[$key]['value'] = $value;
return $this;
}
/**
* Get Param Value
*
* @param string $key
* @return mixed
*
* @throws Exception
*/
public function getParamValue(string $key): mixed
{
if (!isset($this->params[$key])) {
throw new Exception('Unknown key');
}
return $this->params[$key]['value'];
}
}