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