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