|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelPolicies; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\LaravelPolicies\Contracts\Ability as AbilityContract; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Ability |
|
13
|
|
|
* |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Ability implements AbilityContract |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Properties |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $key; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string|\Closure */ |
|
27
|
|
|
private $method; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
private $metas = []; |
|
31
|
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
|
33
|
|
|
| Constructor |
|
34
|
|
|
| ----------------------------------------------------------------- |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Ability constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
* @param string|\Closure|null $method |
|
42
|
|
|
*/ |
|
43
|
76 |
|
public function __construct(string $key, $method = null) |
|
44
|
|
|
{ |
|
45
|
76 |
|
$this->setKey($key); |
|
46
|
76 |
|
$this->setMethod($method ?: function () {}); |
|
47
|
76 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Make an ability. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key |
|
53
|
|
|
* @param string|\Closure|null $method |
|
54
|
|
|
* |
|
55
|
|
|
* @return self |
|
56
|
|
|
*/ |
|
57
|
72 |
|
public static function make(string $key, $method = null): self |
|
58
|
|
|
{ |
|
59
|
72 |
|
return new static($key, $method); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* ----------------------------------------------------------------- |
|
63
|
|
|
| Getters & Setters |
|
64
|
|
|
| ----------------------------------------------------------------- |
|
65
|
|
|
*/ |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the ability's key. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
52 |
|
public function key(): string |
|
73
|
|
|
{ |
|
74
|
52 |
|
return $this->key; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the ability's key. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $key |
|
81
|
|
|
* |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
76 |
|
public function setKey(string $key): self |
|
85
|
|
|
{ |
|
86
|
76 |
|
$this->key = $key; |
|
87
|
|
|
|
|
88
|
76 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the ability's method. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Closure|string |
|
95
|
|
|
*/ |
|
96
|
56 |
|
public function method() |
|
97
|
|
|
{ |
|
98
|
56 |
|
return $this->method; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the ability's method name. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string|null |
|
105
|
|
|
*/ |
|
106
|
8 |
|
public function methodName() |
|
107
|
|
|
{ |
|
108
|
8 |
|
if ($this->isClosure()) |
|
109
|
4 |
|
return null; |
|
110
|
|
|
|
|
111
|
4 |
|
return last(explode('@', $this->method())); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the ability's class name. |
|
116
|
|
|
* |
|
117
|
|
|
* @param bool $fqn |
|
118
|
|
|
* |
|
119
|
|
|
* @return string|null |
|
120
|
|
|
*/ |
|
121
|
8 |
|
public function className(bool $fqn = true) |
|
122
|
|
|
{ |
|
123
|
8 |
|
if ($this->isClosure()) |
|
124
|
4 |
|
return null; |
|
125
|
|
|
|
|
126
|
4 |
|
$class = head(explode('@', $this->method())); |
|
127
|
|
|
|
|
128
|
4 |
|
return $fqn |
|
129
|
4 |
|
? $class |
|
130
|
4 |
|
: class_basename($class); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set the callback as method. |
|
135
|
|
|
* |
|
136
|
|
|
* @param \Closure $callback |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
16 |
|
public function callback(Closure $callback): self |
|
141
|
|
|
{ |
|
142
|
16 |
|
return $this->setMethod($callback); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set the ability's method. |
|
147
|
|
|
* |
|
148
|
|
|
* @param \Closure|string $method |
|
149
|
|
|
* |
|
150
|
|
|
* @return self |
|
151
|
|
|
*/ |
|
152
|
76 |
|
public function setMethod($method): self |
|
153
|
|
|
{ |
|
154
|
76 |
|
$this->method = $method; |
|
155
|
|
|
|
|
156
|
76 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get the ability's meta. |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
16 |
|
public function metas(): array |
|
165
|
|
|
{ |
|
166
|
16 |
|
return $this->metas; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Set the ability's meta. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $metas |
|
173
|
|
|
* @param bool $keepMetas |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
|
|
*/ |
|
177
|
40 |
|
public function setMetas(array $metas, bool $keepMetas = true): self |
|
178
|
|
|
{ |
|
179
|
40 |
|
$this->metas = array_merge($keepMetas ? $this->metas : [], $metas); |
|
180
|
|
|
|
|
181
|
40 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get a meta. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $key |
|
188
|
|
|
* @param mixed|null $default |
|
189
|
|
|
* |
|
190
|
|
|
* @return mixed |
|
191
|
|
|
*/ |
|
192
|
12 |
|
public function meta(string $key, $default = null) |
|
193
|
|
|
{ |
|
194
|
12 |
|
return Arr::get($this->metas, $key, $default); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set a meta. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $key |
|
201
|
|
|
* @param mixed $value |
|
202
|
|
|
* |
|
203
|
|
|
* @return self |
|
204
|
|
|
*/ |
|
205
|
8 |
|
public function setMeta(string $key, $value): self |
|
206
|
|
|
{ |
|
207
|
8 |
|
Arr::set($this->metas, $key, $value); |
|
208
|
|
|
|
|
209
|
8 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/* ----------------------------------------------------------------- |
|
213
|
|
|
| Check Methods |
|
214
|
|
|
| ----------------------------------------------------------------- |
|
215
|
|
|
*/ |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Check if the ability is a callback method. |
|
219
|
|
|
* |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
24 |
|
public function isClosure(): bool |
|
223
|
|
|
{ |
|
224
|
24 |
|
return $this->method instanceof Closure; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/* ----------------------------------------------------------------- |
|
228
|
|
|
| Other Methods |
|
229
|
|
|
| ----------------------------------------------------------------- |
|
230
|
|
|
*/ |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the instance as an array. |
|
234
|
|
|
* |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
12 |
|
public function toArray(): array |
|
238
|
|
|
{ |
|
239
|
|
|
return [ |
|
240
|
12 |
|
'key' => $this->key(), |
|
241
|
12 |
|
'method' => $this->isClosure() ? 'Closure' : $this->method(), |
|
242
|
12 |
|
'metas' => $this->metas(), |
|
243
|
|
|
]; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Convert the object to its JSON representation. |
|
248
|
|
|
* |
|
249
|
|
|
* @param int $options |
|
250
|
|
|
* |
|
251
|
|
|
* @return string |
|
252
|
|
|
*/ |
|
253
|
4 |
|
public function toJson($options = 0): string |
|
254
|
|
|
{ |
|
255
|
4 |
|
return json_encode($this->jsonSerialize(), $options); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Convert the object into something JSON serializable. |
|
260
|
|
|
* |
|
261
|
|
|
* @return array |
|
262
|
|
|
*/ |
|
263
|
4 |
|
public function jsonSerialize(): array |
|
264
|
|
|
{ |
|
265
|
4 |
|
return $this->toArray(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Convert the object to string as JSON representation. |
|
270
|
|
|
* |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
4 |
|
public function __toString(): string |
|
274
|
|
|
{ |
|
275
|
4 |
|
return $this->toJson(); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|