|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder; |
|
4
|
|
|
|
|
5
|
|
|
class Rules |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var string|null |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $fieldName; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $rules; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $attributes; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $messages; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $rules |
|
30
|
|
|
* @param array $attributes |
|
31
|
|
|
* @param array $messages |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function __construct(array $rules, array $attributes = [], array $messages = []) { |
|
34
|
9 |
|
$this->rules = $rules; |
|
35
|
9 |
|
$this->attributes = $attributes; |
|
36
|
9 |
|
$this->messages = $messages; |
|
37
|
9 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
*/ |
|
42
|
9 |
|
public function setFieldName($name) { |
|
43
|
9 |
|
$this->fieldName = $name; |
|
44
|
|
|
|
|
45
|
9 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $fieldName |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getFieldRules($fieldName = null) { |
|
52
|
|
|
$fieldName = $this->ensureFieldName($fieldName); |
|
53
|
|
|
|
|
54
|
|
|
$rules = $this->rules; |
|
55
|
|
|
return isset($rules[$fieldName]) ? $rules[$fieldName] : []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixes $rule |
|
60
|
|
|
* @param string $fieldName |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addFieldRule($rule, $fieldName = null) { |
|
63
|
|
|
$rules = $this->getFieldRules($fieldName); |
|
64
|
|
|
$rules[] = $rule; |
|
65
|
|
|
$this->setFieldRules($rules, $fieldName); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $rules |
|
70
|
|
|
* @param string $fieldName |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setFieldRules(array $rules, $fieldName = null) { |
|
73
|
|
|
$fieldName = $this->ensureFieldName($fieldName); |
|
74
|
|
|
$this->rules[$fieldName] = $rules; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $fieldName |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function ensureFieldName($fieldName) { |
|
81
|
|
|
if (!$fieldName) { |
|
82
|
|
|
if (!$this->fieldName) { |
|
|
|
|
|
|
83
|
|
|
throw new InvalidArgumentException("Field functions on non-field Rules need explicit field name"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$fieldName = $this->fieldName; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $fieldName; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $rules |
|
94
|
|
|
* @param array $attributes |
|
|
|
|
|
|
95
|
|
|
* @param array $messages |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
9 |
|
public function append($rules) { |
|
98
|
9 |
|
if (is_array($rules)) { |
|
99
|
|
|
$rules = static::fromArray($rules); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
9 |
|
$this->rules = array_replace_recursive($this->rules, $rules->getRules()); |
|
103
|
9 |
|
$this->attributes = array_replace_recursive($this->attributes, $rules->getAttributes()); |
|
104
|
9 |
|
$this->messages = array_replace_recursive($this->messages, $rules->getMessages()); |
|
105
|
|
|
|
|
106
|
9 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
9 |
|
public function getRules() { |
|
113
|
9 |
|
return $this->rules; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public function getAttributes() { |
|
120
|
9 |
|
return $this->attributes; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
9 |
|
public function getMessages() { |
|
127
|
9 |
|
return $this->messages; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param array[] $rules |
|
132
|
|
|
* @return static |
|
133
|
|
|
*/ |
|
134
|
|
|
static public function fromArray($rules) { |
|
135
|
|
|
if (!$rules) { |
|
|
|
|
|
|
136
|
|
|
return new static([]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$rules += [ |
|
140
|
|
|
'rules' => [], |
|
141
|
|
|
'attributes' => [], |
|
142
|
|
|
'error_messages' => [], |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
return new static($rules['rules'], $rules['attributes'], $rules['error_messages']); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: