1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelPolicies; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelPolicies\Contracts\Ability as AbilityContract; |
8
|
|
|
use Arcanedev\LaravelPolicies\Contracts\Policy as PolicyContract; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Policy |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class Policy implements PolicyContract |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Properties |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Default metas for the abilities. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $metas = []; |
29
|
|
|
|
30
|
|
|
/* ----------------------------------------------------------------- |
31
|
|
|
| Getters & Setters |
32
|
|
|
| ----------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the ability's prefix. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
28 |
|
protected static function prefix(): string |
41
|
|
|
{ |
42
|
28 |
|
return ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the default metas for the abilities. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
28 |
|
protected function getMetas(): array |
51
|
|
|
{ |
52
|
28 |
|
return $this->metas; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the default metas for the abilities. |
57
|
|
|
* |
58
|
|
|
* @param array $metas |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
20 |
|
protected function setMetas(array $metas) |
63
|
|
|
{ |
64
|
20 |
|
$this->metas = $metas; |
65
|
|
|
|
66
|
20 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* ----------------------------------------------------------------- |
70
|
|
|
| Main Methods |
71
|
|
|
| ----------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the ability's key. |
76
|
|
|
* |
77
|
|
|
* @param array|string $keys |
78
|
|
|
* |
79
|
|
|
* @return array|string |
80
|
|
|
*/ |
81
|
12 |
|
public static function ability($keys) |
82
|
|
|
{ |
83
|
12 |
|
if (is_string($keys)) |
84
|
8 |
|
return static::prefixedKey($keys); |
85
|
|
|
|
86
|
4 |
|
return array_map(function (string $key) { |
87
|
4 |
|
return static::prefixedKey($key); |
88
|
4 |
|
}, (array) $keys); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Make a new ability. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @param string|null $method |
96
|
|
|
* |
97
|
|
|
* @return \Arcanedev\LaravelPolicies\Contracts\Ability |
98
|
|
|
*/ |
99
|
28 |
|
protected function makeAbility(string $key, $method = null): AbilityContract |
100
|
|
|
{ |
101
|
28 |
|
return Ability::make( |
102
|
28 |
|
static::prefixedKey($key), |
103
|
28 |
|
static::prepareMethod($method ?: $key) |
104
|
28 |
|
)->setMetas($this->getMetas()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a prefixed key. |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
36 |
|
protected static function prefixedKey(string $key): string |
115
|
|
|
{ |
116
|
36 |
|
return empty($prefix = static::prefix()) |
117
|
28 |
|
? $key |
118
|
36 |
|
: trim($prefix.$key); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Prepare the method name. |
123
|
|
|
* |
124
|
|
|
* @param string $method |
125
|
|
|
* |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
28 |
|
protected static function prepareMethod(string $method): ?string |
129
|
|
|
{ |
130
|
|
|
// Dedicated Class |
131
|
28 |
|
if (class_exists($method)) |
132
|
16 |
|
return $method; |
133
|
|
|
|
134
|
|
|
// Dedicated Method |
135
|
28 |
|
$method = Str::camel($method); |
136
|
|
|
|
137
|
14 |
|
if (method_exists(static::class, $method)) |
138
|
14 |
|
return static::class.'@'.$method; |
139
|
|
|
|
140
|
16 |
|
return null; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|