5
5
class Attribute
6
6
{
7
7
8
+ /** @var array */
8
9
protected $ rules = [];
9
10
11
+ /** @var string */
10
12
protected $ key ;
11
13
14
+ /** @var string|null */
12
15
protected $ alias ;
13
16
17
+ /** @var Rakit\Validation\Validation */
14
18
protected $ validation ;
15
19
20
+ /** @var bool */
16
21
protected $ required = false ;
17
22
23
+ /** @var Rakit\Validation\Validation|null */
18
24
protected $ primaryAttribute = null ;
19
25
26
+ /** @var array */
20
27
protected $ otherAttributes = [];
21
28
29
+ /** @var array */
22
30
protected $ keyIndexes = [];
23
31
24
- public function __construct (Validation $ validation , $ key , $ alias = null , array $ rules = array ())
25
- {
32
+ /**
33
+ * Constructor
34
+ *
35
+ * @param Rakit\Validation\Validation $validation
36
+ * @param string $key
37
+ * @param string|null $alias
38
+ * @param array $rules
39
+ * @return void
40
+ */
41
+ public function __construct (
42
+ Validation $ validation ,
43
+ string $ key ,
44
+ $ alias = null ,
45
+ array $ rules = []
46
+ ) {
26
47
$ this ->validation = $ validation ;
27
48
$ this ->alias = $ alias ;
28
49
$ this ->key = $ key ;
@@ -31,21 +52,44 @@ public function __construct(Validation $validation, $key, $alias = null, array $
31
52
}
32
53
}
33
54
55
+ /**
56
+ * Set the primary attribute
57
+ *
58
+ * @param Rakit\Validation\Attribute $primaryAttribute
59
+ * @return void
60
+ */
34
61
public function setPrimaryAttribute (Attribute $ primaryAttribute )
35
62
{
36
63
$ this ->primaryAttribute = $ primaryAttribute ;
37
64
}
38
65
66
+ /**
67
+ * Set key indexes
68
+ *
69
+ * @param array $keyIndexes
70
+ * @return void
71
+ */
39
72
public function setKeyIndexes (array $ keyIndexes )
40
73
{
41
74
$ this ->keyIndexes = $ keyIndexes ;
42
75
}
43
76
77
+ /**
78
+ * Get primary attributes
79
+ *
80
+ * @return Rakit\Validation\Attribute|null
81
+ */
44
82
public function getPrimaryAttribute ()
45
83
{
46
84
return $ this ->primaryAttribute ;
47
85
}
48
86
87
+ /**
88
+ * Set other attributes
89
+ *
90
+ * @param array $otherAttributes
91
+ * @return void
92
+ */
49
93
public function setOtherAttributes (array $ otherAttributes )
50
94
{
51
95
$ this ->otherAttributes = [];
@@ -54,59 +98,120 @@ public function setOtherAttributes(array $otherAttributes)
54
98
}
55
99
}
56
100
101
+ /**
102
+ * Add other attributes
103
+ *
104
+ * @param Rakit\Validation\Attribute $otherAttribute
105
+ * @return void
106
+ */
57
107
public function addOtherAttribute (Attribute $ otherAttribute )
58
108
{
59
109
$ this ->otherAttributes [] = $ otherAttribute ;
60
110
}
61
111
62
- public function getOtherAttributes ()
112
+ /**
113
+ * Get other attributes
114
+ *
115
+ * @return array
116
+ */
117
+ public function getOtherAttributes (): array
63
118
{
64
119
return $ this ->otherAttributes ;
65
120
}
66
121
122
+ /**
123
+ * Add rule
124
+ *
125
+ * @param Rakit\Validation\Rule $rule
126
+ * @return void
127
+ */
67
128
public function addRule (Rule $ rule )
68
129
{
69
130
$ rule ->setAttribute ($ this );
70
131
$ rule ->setValidation ($ this ->validation );
71
132
$ this ->rules [$ rule ->getKey ()] = $ rule ;
72
133
}
73
134
74
- public function getRule ($ ruleKey )
135
+ /**
136
+ * Get rule
137
+ *
138
+ * @param string $ruleKey
139
+ * @return void
140
+ */
141
+ public function getRule (string $ ruleKey )
75
142
{
76
143
return $ this ->hasRule ($ ruleKey )? $ this ->rules [$ ruleKey ] : null ;
77
144
}
78
145
79
- public function getRules ()
146
+ /**
147
+ * Get rules
148
+ *
149
+ * @return array
150
+ */
151
+ public function getRules (): array
80
152
{
81
153
return $ this ->rules ;
82
154
}
83
155
84
- public function hasRule ($ ruleKey )
156
+ /**
157
+ * Check the $ruleKey has in the rule
158
+ *
159
+ * @param string $ruleKey
160
+ * @return bool
161
+ */
162
+ public function hasRule (string $ ruleKey ): bool
85
163
{
86
164
return isset ($ this ->rules [$ ruleKey ]);
87
165
}
88
166
89
- public function setRequired ($ required )
167
+ /**
168
+ * Set required
169
+ *
170
+ * @param boolean $required
171
+ * @return void
172
+ */
173
+ public function setRequired (bool $ required )
90
174
{
91
175
$ this ->required = $ required ;
92
176
}
93
177
94
- public function isRequired ()
178
+ /**
179
+ * Set rule is required
180
+ *
181
+ * @return boolean
182
+ */
183
+ public function isRequired (): bool
95
184
{
96
- return $ this ->required === true ;
185
+ return $ this ->required ;
97
186
}
98
187
99
- public function getKey ()
188
+ /**
189
+ * Get key
190
+ *
191
+ * @return string
192
+ */
193
+ public function getKey (): string
100
194
{
101
195
return $ this ->key ;
102
196
}
103
197
104
- public function getKeyIndexes ()
198
+ /**
199
+ * Get key indexes
200
+ *
201
+ * @return array
202
+ */
203
+ public function getKeyIndexes (): array
105
204
{
106
205
return $ this ->keyIndexes ;
107
206
}
108
207
109
- public function getValue ($ key = null )
208
+ /**
209
+ * Get value
210
+ *
211
+ * @param string|null $key
212
+ * @return void
213
+ */
214
+ public function getValue (string $ key = null )
110
215
{
111
216
if ($ key && $ this ->isArrayAttribute ()) {
112
217
$ key = $ this ->resolveSiblingKey ($ key );
@@ -119,17 +224,33 @@ public function getValue($key = null)
119
224
return $ this ->validation ->getValue ($ key );
120
225
}
121
226
122
- public function isArrayAttribute ()
227
+ /**
228
+ * Get that is array attribute
229
+ *
230
+ * @return boolean
231
+ */
232
+ public function isArrayAttribute (): bool
123
233
{
124
234
return count ($ this ->getKeyIndexes ()) > 0 ;
125
235
}
126
236
127
- public function isUsingDotNotation ()
237
+ /**
238
+ * Check this attribute is using dot notation
239
+ *
240
+ * @return boolean
241
+ */
242
+ public function isUsingDotNotation (): bool
128
243
{
129
244
return strpos ($ this ->getKey (), '. ' ) !== false ;
130
245
}
131
246
132
- public function resolveSiblingKey ($ key )
247
+ /**
248
+ * Resolve sibling key
249
+ *
250
+ * @param string $key
251
+ * @return string
252
+ */
253
+ public function resolveSiblingKey (string $ key ): string
133
254
{
134
255
$ indexes = $ this ->getKeyIndexes ();
135
256
$ keys = explode ("* " , $ key );
@@ -141,6 +262,11 @@ public function resolveSiblingKey($key)
141
262
return call_user_func_array ('sprintf ' , $ args );
142
263
}
143
264
265
+ /**
266
+ * Get humanize key
267
+ *
268
+ * @return string
269
+ */
144
270
public function getHumanizedKey ()
145
271
{
146
272
$ primaryAttribute = $ this ->getPrimaryAttribute ();
@@ -160,11 +286,22 @@ public function getHumanizedKey()
160
286
return ucfirst ($ key );
161
287
}
162
288
163
- public function setAlias ($ alias )
289
+ /**
290
+ * Set alias
291
+ *
292
+ * @param string $alias
293
+ * @return void
294
+ */
295
+ public function setAlias (string $ alias )
164
296
{
165
297
$ this ->alias = $ alias ;
166
298
}
167
299
300
+ /**
301
+ * Get alias
302
+ *
303
+ * @return string|null
304
+ */
168
305
public function getAlias ()
169
306
{
170
307
return $ this ->alias ;
0 commit comments