1 | <?php |
||
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) |
|
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 |
|
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 |
|
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 |
|
90 | |||
91 | /** |
||
92 | * Get the ability's method. |
||
93 | * |
||
94 | * @return \Closure|string |
||
95 | */ |
||
96 | 56 | public function method() |
|
100 | |||
101 | /** |
||
102 | * Get the ability's method name. |
||
103 | * |
||
104 | * @return string|null |
||
105 | */ |
||
106 | 8 | public function methodName() |
|
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) |
|
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 |
|
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 |
|
158 | |||
159 | /** |
||
160 | * Get the ability's meta. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 16 | public function metas(): array |
|
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 |
|
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) |
|
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 |
|
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 |
|
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 |
|
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 |
|
257 | |||
258 | /** |
||
259 | * Convert the object into something JSON serializable. |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | 4 | public function jsonSerialize(): array |
|
267 | |||
268 | /** |
||
269 | * Convert the object to string as JSON representation. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | 4 | public function __toString(): string |
|
277 | } |
||
278 |