Completed
Pull Request — master (#5)
by ARCANEDEV
14:16
created

Ability::callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
        if ($fqn) {
129 4
            return $class;
130
        }
131
132 4
        return class_basename($class);
133
    }
134
135
    /**
136
     * Set the callback as method.
137
     *
138
     * @param  \Closure  $callback
139
     *
140
     * @return $this
141
     */
142 16
    public function callback(Closure $callback): self
143
    {
144 16
        return $this->setMethod($callback);
145
    }
146
147
    /**
148
     * Set the ability's method.
149
     *
150
     * @param  \Closure|string  $method
151
     *
152
     * @return self
153
     */
154 76
    public function setMethod($method): self
155
    {
156 76
        $this->method = $method;
157
158 76
        return $this;
159
    }
160
161
    /**
162
     * Get the ability's meta.
163
     *
164
     * @return array
165
     */
166 16
    public function metas(): array
167
    {
168 16
        return $this->metas;
169
    }
170
171
    /**
172
     * Set the ability's meta.
173
     *
174
     * @param  array  $metas
175
     * @param  bool   $keepMetas
176
     *
177
     * @return self
178
     */
179 40
    public function setMetas(array $metas, bool $keepMetas = true): self
180
    {
181 40
        $this->metas = array_merge($keepMetas ? $this->metas : [], $metas);
182
183 40
        return $this;
184
    }
185
186
    /**
187
     * Get a meta.
188
     *
189
     * @param  string      $key
190
     * @param  mixed|null  $default
191
     *
192
     * @return mixed
193
     */
194 12
    public function meta(string $key, $default = null)
195
    {
196 12
        return Arr::get($this->metas, $key, $default);
197
    }
198
199
    /**
200
     * Set a meta.
201
     *
202
     * @param  string  $key
203
     * @param  mixed   $value
204
     *
205
     * @return self
206
     */
207 8
    public function setMeta(string $key, $value): self
208
    {
209 8
        Arr::set($this->metas, $key, $value);
210
211 8
        return $this;
212
    }
213
214
    /* -----------------------------------------------------------------
215
     |  Check Methods
216
     | -----------------------------------------------------------------
217
     */
218
219
    /**
220
     * Check if the ability is a callback method.
221
     *
222
     * @return bool
223
     */
224 24
    public function isClosure(): bool
225
    {
226 24
        return $this->method instanceof Closure;
227
    }
228
229
    /* -----------------------------------------------------------------
230
     |  Other Methods
231
     | -----------------------------------------------------------------
232
     */
233
234
    /**
235
     * Get the instance as an array.
236
     *
237
     * @return array
238
     */
239 12
    public function toArray(): array
240
    {
241
        return [
242 12
            'key'    => $this->key(),
243 12
            'method' => $this->isClosure() ? 'Closure' : $this->method(),
244 12
            'metas'  => $this->metas(),
245
        ];
246
    }
247
248
    /**
249
     * Convert the object to its JSON representation.
250
     *
251
     * @param  int  $options
252
     *
253
     * @return string
254
     */
255 4
    public function toJson($options = 0): string
256
    {
257 4
        return json_encode($this->jsonSerialize(), $options);
258
    }
259
260
    /**
261
     * Convert the object into something JSON serializable.
262
     *
263
     * @return array
264
     */
265 4
    public function jsonSerialize(): array
266
    {
267 4
        return $this->toArray();
268
    }
269
270
    /**
271
     * Convert the object to string as JSON representation.
272
     *
273
     * @return string
274
     */
275 4
    public function __toString(): string
276
    {
277 4
        return $this->toJson();
278
    }
279
}
280