Skip to content

Commit 4dc604c

Browse files
authored
Merge pull request #67 from peter279k/add_type_hint
Complete type hints and annotation comments
2 parents 825bf69 + 340db05 commit 4dc604c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1277
-176
lines changed

src/Attribute.php

Lines changed: 153 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,45 @@
55
class Attribute
66
{
77

8+
/** @var array */
89
protected $rules = [];
910

11+
/** @var string */
1012
protected $key;
1113

14+
/** @var string|null */
1215
protected $alias;
1316

17+
/** @var Rakit\Validation\Validation */
1418
protected $validation;
1519

20+
/** @var bool */
1621
protected $required = false;
1722

23+
/** @var Rakit\Validation\Validation|null */
1824
protected $primaryAttribute = null;
1925

26+
/** @var array */
2027
protected $otherAttributes = [];
2128

29+
/** @var array */
2230
protected $keyIndexes = [];
2331

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+
) {
2647
$this->validation = $validation;
2748
$this->alias = $alias;
2849
$this->key = $key;
@@ -31,21 +52,44 @@ public function __construct(Validation $validation, $key, $alias = null, array $
3152
}
3253
}
3354

55+
/**
56+
* Set the primary attribute
57+
*
58+
* @param Rakit\Validation\Attribute $primaryAttribute
59+
* @return void
60+
*/
3461
public function setPrimaryAttribute(Attribute $primaryAttribute)
3562
{
3663
$this->primaryAttribute = $primaryAttribute;
3764
}
3865

66+
/**
67+
* Set key indexes
68+
*
69+
* @param array $keyIndexes
70+
* @return void
71+
*/
3972
public function setKeyIndexes(array $keyIndexes)
4073
{
4174
$this->keyIndexes = $keyIndexes;
4275
}
4376

77+
/**
78+
* Get primary attributes
79+
*
80+
* @return Rakit\Validation\Attribute|null
81+
*/
4482
public function getPrimaryAttribute()
4583
{
4684
return $this->primaryAttribute;
4785
}
4886

87+
/**
88+
* Set other attributes
89+
*
90+
* @param array $otherAttributes
91+
* @return void
92+
*/
4993
public function setOtherAttributes(array $otherAttributes)
5094
{
5195
$this->otherAttributes = [];
@@ -54,59 +98,120 @@ public function setOtherAttributes(array $otherAttributes)
5498
}
5599
}
56100

101+
/**
102+
* Add other attributes
103+
*
104+
* @param Rakit\Validation\Attribute $otherAttribute
105+
* @return void
106+
*/
57107
public function addOtherAttribute(Attribute $otherAttribute)
58108
{
59109
$this->otherAttributes[] = $otherAttribute;
60110
}
61111

62-
public function getOtherAttributes()
112+
/**
113+
* Get other attributes
114+
*
115+
* @return array
116+
*/
117+
public function getOtherAttributes(): array
63118
{
64119
return $this->otherAttributes;
65120
}
66121

122+
/**
123+
* Add rule
124+
*
125+
* @param Rakit\Validation\Rule $rule
126+
* @return void
127+
*/
67128
public function addRule(Rule $rule)
68129
{
69130
$rule->setAttribute($this);
70131
$rule->setValidation($this->validation);
71132
$this->rules[$rule->getKey()] = $rule;
72133
}
73134

74-
public function getRule($ruleKey)
135+
/**
136+
* Get rule
137+
*
138+
* @param string $ruleKey
139+
* @return void
140+
*/
141+
public function getRule(string $ruleKey)
75142
{
76143
return $this->hasRule($ruleKey)? $this->rules[$ruleKey] : null;
77144
}
78145

79-
public function getRules()
146+
/**
147+
* Get rules
148+
*
149+
* @return array
150+
*/
151+
public function getRules(): array
80152
{
81153
return $this->rules;
82154
}
83155

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
85163
{
86164
return isset($this->rules[$ruleKey]);
87165
}
88166

89-
public function setRequired($required)
167+
/**
168+
* Set required
169+
*
170+
* @param boolean $required
171+
* @return void
172+
*/
173+
public function setRequired(bool $required)
90174
{
91175
$this->required = $required;
92176
}
93177

94-
public function isRequired()
178+
/**
179+
* Set rule is required
180+
*
181+
* @return boolean
182+
*/
183+
public function isRequired(): bool
95184
{
96-
return $this->required === true;
185+
return $this->required;
97186
}
98187

99-
public function getKey()
188+
/**
189+
* Get key
190+
*
191+
* @return string
192+
*/
193+
public function getKey(): string
100194
{
101195
return $this->key;
102196
}
103197

104-
public function getKeyIndexes()
198+
/**
199+
* Get key indexes
200+
*
201+
* @return array
202+
*/
203+
public function getKeyIndexes(): array
105204
{
106205
return $this->keyIndexes;
107206
}
108207

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)
110215
{
111216
if ($key && $this->isArrayAttribute()) {
112217
$key = $this->resolveSiblingKey($key);
@@ -119,17 +224,33 @@ public function getValue($key = null)
119224
return $this->validation->getValue($key);
120225
}
121226

122-
public function isArrayAttribute()
227+
/**
228+
* Get that is array attribute
229+
*
230+
* @return boolean
231+
*/
232+
public function isArrayAttribute(): bool
123233
{
124234
return count($this->getKeyIndexes()) > 0;
125235
}
126236

127-
public function isUsingDotNotation()
237+
/**
238+
* Check this attribute is using dot notation
239+
*
240+
* @return boolean
241+
*/
242+
public function isUsingDotNotation(): bool
128243
{
129244
return strpos($this->getKey(), '.') !== false;
130245
}
131246

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
133254
{
134255
$indexes = $this->getKeyIndexes();
135256
$keys = explode("*", $key);
@@ -141,6 +262,11 @@ public function resolveSiblingKey($key)
141262
return call_user_func_array('sprintf', $args);
142263
}
143264

265+
/**
266+
* Get humanize key
267+
*
268+
* @return string
269+
*/
144270
public function getHumanizedKey()
145271
{
146272
$primaryAttribute = $this->getPrimaryAttribute();
@@ -160,11 +286,22 @@ public function getHumanizedKey()
160286
return ucfirst($key);
161287
}
162288

163-
public function setAlias($alias)
289+
/**
290+
* Set alias
291+
*
292+
* @param string $alias
293+
* @return void
294+
*/
295+
public function setAlias(string $alias)
164296
{
165297
$this->alias = $alias;
166298
}
167299

300+
/**
301+
* Get alias
302+
*
303+
* @return string|null
304+
*/
168305
public function getAlias()
169306
{
170307
return $this->alias;

0 commit comments

Comments
 (0)