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
|
6 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public static function optional() |
64
|
|
|
{ |
65
|
1 |
|
$attribute = static::make(); |
66
|
1 |
|
$attribute->setRules( |
67
|
1 |
|
collect($attribute->getRules()) |
68
|
|
|
->reject(function ($value) { |
69
|
1 |
|
return $value === 'required'; |
70
|
1 |
|
}) |
71
|
1 |
|
->toArray() |
72
|
|
|
); |
73
|
|
|
|
74
|
1 |
|
return $attribute; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return mixed |
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('Faking this attribute is not supported. Is the factory method 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
|
9 |
|
foreach ($rules as $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
|
|
|
} |
105
|
|
|
|
106
|
9 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
10 |
|
protected function extractRules($rules): array |
110
|
|
|
{ |
111
|
10 |
|
if (is_array($rules)) { |
112
|
1 |
|
return $rules; |
113
|
10 |
|
} elseif (is_string($rules)) { |
114
|
10 |
|
return explode('|', $rules); |
115
|
1 |
|
} elseif (is_object($rules)) { |
116
|
1 |
|
return [$rules]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return []; |
120
|
|
|
} |
121
|
|
|
|
122
|
10 |
|
public function getRules() |
123
|
|
|
{ |
124
|
10 |
|
return $this->data['rules']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getPreProcessing(){ |
128
|
|
|
return $this->preProcess; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
6 |
|
public function getCast(){ |
132
|
6 |
|
return $this->cast; |
133
|
|
|
} |
134
|
|
|
|
135
|
6 |
|
public function hasDefault() |
136
|
|
|
{ |
137
|
6 |
|
return array_key_exists('default', $this->data); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
public function getDefault() |
141
|
|
|
{ |
142
|
3 |
|
return $this->data['default'] ?? null; |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
protected function setDefault($value) |
146
|
|
|
{ |
147
|
3 |
|
$this->data['default'] = $value; |
148
|
3 |
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.