Skip to content

Commit aedc120

Browse files
committed
Merge branch 'release/0.2.8'
2 parents a5fda32 + d31e810 commit aedc120

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ if(!$validator->validate()){
128128
* `ipv6` Validate that value is IPv6
129129
* `date`, `datetime` Validate that value is date/datetime
130130

131+
**Hint:** You can add `[]` to the end of any type to validate an array of values.
132+
131133
#### Validator Methods
132134

133135
##### `__construct(array $values, array $rules)`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nonamephp/php7-common",
33
"description": "A collection of common PHP 7 libraries",
4-
"version": "0.2.7",
4+
"version": "0.2.8",
55
"license": "MIT",
66
"authors": [
77
{

src/Validator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public function validate() : bool
243243
$rule['required'] = true;
244244
}
245245

246+
// Add key/name to $rule
247+
$rule['name'] = $name;
248+
246249
// Check if value exists
247250
if ($value = $this->values->get($name, false)) {
248251
// Validate using type validators
@@ -328,9 +331,32 @@ public function hasErrors() : bool
328331
public function validateType($type, $value, array $rule = []) : bool
329332
{
330333
$type = strtolower($type);
334+
335+
// Check for type[]
336+
$arrayType = (substr($type, -2) == '[]');
337+
if ($arrayType) {
338+
$type = substr($type, 0, -2);
339+
}
340+
331341
if (isset($this->typeValidateFunctionMap[$type])) {
342+
if ($arrayType) {
343+
// Validate an array of values
344+
$values = (array) $value;
345+
foreach ($values as $index => $value) {
346+
if (!$this->{$this->typeValidateFunctionMap[$type]}($value, $rule)) {
347+
$this->setError($rule['name'], "'$value' failed to validate as '$type'");
348+
}
349+
}
350+
351+
// Return true regardless of any errors to avoid default error
352+
// that is set when this method returns false
353+
return true;
354+
}
355+
356+
// Validate the value
332357
return $this->{$this->typeValidateFunctionMap[$type]}($value, $rule);
333358
}
359+
334360
throw new \InvalidArgumentException("Type '$type' is not a valid rule type");
335361
}
336362

tests/ValidatorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,34 @@ public function testCustomValidatorRule()
786786
}
787787
}
788788
}
789+
790+
/**
791+
* Test type[] validation
792+
*/
793+
public function testValidateArrayTypes()
794+
{
795+
$passValues = [
796+
'strings' => $this->stringValues,
797+
'integers' => $this->integerValues,
798+
'emails' => $this->emailValues
799+
];
800+
801+
$failValues = [
802+
'strings' => array_merge($this->stringValues, $this->integerValues),
803+
'integers' => array_merge($this->integerValues, $this->stringValues, $this->emailValues),
804+
'emails' => array_merge($this->emailValues, $this->stringValues, $this->integerValues)
805+
];
806+
807+
$rules = [
808+
'strings' => 'string[]',
809+
'integers' => 'int[]',
810+
'emails' => 'email[]'
811+
];
812+
813+
$passValidator = new Validator($passValues, $rules);
814+
$this->assertTrue($passValidator->validate());
815+
816+
$failValidator = new Validator($failValues, $rules);
817+
$this->assertFalse($failValidator->validate());
818+
}
789819
}

0 commit comments

Comments
 (0)