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