1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larapie\Actions; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
use Faker\Generator; |
7
|
|
|
use Larapie\Actions\Attributes\Rules\GeneralRules; |
8
|
|
|
|
9
|
|
|
class Attribute |
10
|
|
|
{ |
11
|
|
|
use GeneralRules; |
12
|
|
|
|
13
|
|
|
protected $data = []; |
14
|
|
|
|
15
|
|
|
protected $cast = null; |
16
|
|
|
|
17
|
13 |
|
final public function __construct() |
18
|
|
|
{ |
19
|
13 |
|
$this->boot(); |
20
|
13 |
|
} |
21
|
|
|
|
22
|
13 |
|
protected function boot() |
23
|
|
|
{ |
24
|
13 |
|
$this->initializeRules(); |
25
|
13 |
|
} |
26
|
|
|
|
27
|
13 |
|
protected function initializeRules() |
28
|
|
|
{ |
29
|
13 |
|
if (method_exists($this, 'rules')) { |
30
|
6 |
|
$this->data['rules'] = $this->extractRules($this->rules()); |
31
|
|
|
} else { |
32
|
9 |
|
$this->data['rules'] = []; |
33
|
|
|
} |
34
|
13 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return static |
38
|
|
|
*/ |
39
|
12 |
|
protected static function make() |
40
|
|
|
{ |
41
|
12 |
|
return new static(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
4 |
|
public static function default($value) |
49
|
|
|
{ |
50
|
4 |
|
$attribute = static::make(); |
51
|
4 |
|
$attribute->setDefault($value); |
52
|
|
|
|
53
|
4 |
|
return $attribute; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
9 |
|
public static function required() |
60
|
|
|
{ |
61
|
9 |
|
return static::make()->require(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
9 |
|
public function require() |
68
|
|
|
{ |
69
|
9 |
|
return $this->rule('required'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
4 |
|
public function cast($cast) |
76
|
|
|
{ |
77
|
4 |
|
if (is_callable($cast) || is_string($cast)) { |
78
|
2 |
|
$this->cast = $cast; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
1 |
|
public static function optional() |
88
|
|
|
{ |
89
|
|
|
return tap(static::make(), function (Attribute $attribute) { |
90
|
1 |
|
$attribute->setRules( |
91
|
1 |
|
collect($attribute->getRules()) |
92
|
|
|
->reject(function ($value) { |
93
|
1 |
|
return $value === 'required'; |
94
|
1 |
|
}) |
95
|
1 |
|
->toArray() |
96
|
|
|
); |
97
|
1 |
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public static function fake() |
104
|
|
|
{ |
105
|
|
|
return static::make()->factory(Factory::create()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function factory(Generator $faker) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
throw new \RuntimeException('The fake method for this attribute is not implemented.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
13 |
|
protected function setRules(array $rules) |
114
|
|
|
{ |
115
|
13 |
|
$this->data['rules'] = $rules; |
116
|
13 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed ...$rules |
120
|
|
|
* @return static |
121
|
|
|
*/ |
122
|
12 |
|
public function rule(...$rules) |
123
|
|
|
{ |
124
|
|
|
collect($rules)->each(function ($rule) { |
125
|
12 |
|
$this->setRules( |
126
|
12 |
|
collect($this->extractRules($rule)) |
127
|
12 |
|
->flatten() |
128
|
12 |
|
->merge($this->data['rules']) |
129
|
12 |
|
->unique() |
130
|
12 |
|
->toArray() |
131
|
|
|
); |
132
|
12 |
|
}); |
133
|
|
|
|
134
|
12 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return static |
139
|
|
|
*/ |
140
|
1 |
|
public function nullable() |
141
|
|
|
{ |
142
|
1 |
|
return $this->rule('nullable'); |
143
|
|
|
} |
144
|
|
|
|
145
|
6 |
|
public function isNullable(): bool |
146
|
|
|
{ |
147
|
6 |
|
return $this->hasRule('nullable'); |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
public function hasRule(string $rule): bool |
151
|
|
|
{ |
152
|
6 |
|
return in_array($rule, $this->getRules()); |
153
|
|
|
} |
154
|
|
|
|
155
|
13 |
|
protected function extractRules($rules): array |
156
|
|
|
{ |
157
|
13 |
|
if (is_array($rules)) { |
158
|
1 |
|
return $rules; |
159
|
13 |
|
} elseif (is_string($rules)) { |
160
|
13 |
|
return explode('|', $rules); |
161
|
1 |
|
} elseif (is_object($rules)) { |
162
|
1 |
|
return [$rules]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return []; |
166
|
|
|
} |
167
|
|
|
|
168
|
13 |
|
public function getRules(): array |
169
|
|
|
{ |
170
|
13 |
|
return $this->data['rules']; |
171
|
|
|
} |
172
|
|
|
|
173
|
5 |
|
public function getCast() |
174
|
|
|
{ |
175
|
5 |
|
return $this->cast; |
176
|
|
|
} |
177
|
|
|
|
178
|
9 |
|
public function hasDefault(): bool |
179
|
|
|
{ |
180
|
9 |
|
return array_key_exists('default', $this->data); |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
public function getDefault() |
184
|
|
|
{ |
185
|
4 |
|
return $this->data['default'] ?? null; |
186
|
|
|
} |
187
|
|
|
|
188
|
4 |
|
protected function setDefault($value) |
189
|
|
|
{ |
190
|
4 |
|
$this->data['default'] = $value; |
191
|
4 |
|
} |
192
|
|
|
|
193
|
|
|
public function __toString() |
194
|
|
|
{ |
195
|
|
|
return implode('|', $this->getRules()); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.