Skip to content

Commit 506327e

Browse files
committed
Add basic rule parameters for array
1 parent ba307c0 commit 506327e

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/Validator.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,25 @@ protected function validateScalar($value, array $rule = []): bool
582582
*/
583583
protected function validateArray($value, array $rule = []): bool
584584
{
585-
return is_array($value);
585+
$valid = is_array($value);
586+
587+
if (isset($rule['allow_empty']) && $rule['allow_empty'] === false) {
588+
$valid = $valid && !empty($value);
589+
}
590+
591+
if (isset($rule['count']) && is_int($rule['count'])) {
592+
$valid = $valid && (count($value) == $rule['count']);
593+
}
594+
595+
if (isset($rule['min_count']) && is_int($rule['min_count'])) {
596+
$valid = $valid && (count($value) >= $rule['min_count']);
597+
}
598+
599+
if (isset($rule['max_count']) && is_int($rule['max_count'])) {
600+
$valid = $valid && (count($value) <= $rule['max_count']);
601+
}
602+
603+
return $valid;
586604
}
587605

588606
/**
@@ -624,11 +642,11 @@ protected function validateString($value, array $rule = []): bool
624642
$valid = $valid || is_null($value);
625643
}
626644

627-
if (isset($rule['min_length']) && is_int($rule['min_length']) && $rule['min_length'] > 0) {
645+
if (isset($rule['min_length']) && is_int($rule['min_length'])) {
628646
$valid = $valid && (strlen($value) >= $rule['min_length']);
629647
}
630648

631-
if (isset($rule['max_length']) && is_int($rule['max_length']) && $rule['max_length'] > 0) {
649+
if (isset($rule['max_length']) && is_int($rule['max_length'])) {
632650
$valid = $valid && (strlen($value) <= $rule['max_length']);
633651
}
634652

@@ -670,14 +688,14 @@ protected function validateInteger($value, array $rule = []): bool
670688
// Validate > or >=, with > getting priority over >=
671689
if (isset($rule['>']) && is_int($rule['>'])) {
672690
$valid = $valid && ($value > $rule['>']);
673-
} elseif(isset($rule['>=']) && is_int($rule['>='])) {
691+
} elseif (isset($rule['>=']) && is_int($rule['>='])) {
674692
$valid = $valid && ($value >= $rule['>=']);
675693
}
676694

677695
// Validate < or <=, with < getting priority over <=
678696
if (isset($rule['<']) && is_int($rule['<'])) {
679697
$valid = $valid && ($value < $rule['<']);
680-
} elseif(isset($rule['<=']) && is_int($rule['<='])) {
698+
} elseif (isset($rule['<=']) && is_int($rule['<='])) {
681699
$valid = $valid && ($value <= $rule['<=']);
682700
}
683701

@@ -806,18 +824,26 @@ protected function validateDateTime($value, array $rule = []): bool
806824
}
807825

808826
$value = (string) $value;
827+
$compare = $value;
828+
829+
if(strlen($compare) == 4){
830+
// Value is presumably a 4-digit year (e.g. 2016) and as-is doesn't
831+
// play nice with strtotime() or date_create()
832+
$compare .= "-01-01";
833+
}
809834

810835
// Returns a timestamp on success, FALSE otherwise
811-
if (($time = strtotime($value)) === false) {
836+
if (($time = strtotime($compare)) === false) {
812837
return false;
813838
}
814839

815840
// Returns new \DateTime instance on success, FALSE otherwise
816-
if (($date = date_create($value)) === false) {
841+
if (($date = date_create($compare)) === false) {
817842
return false;
818843
}
819844

820845
if (isset($rule['format'])) {
846+
// Use original value for comparing format
821847
return $date->format($rule['format']) == $value;
822848
}
823849

tests/ValidatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ public function testValidateArray()
534534
Validator::isArray($value)
535535
);
536536
}
537+
538+
// Test array rules
539+
$arr_count = ['count' => 3];
540+
$arr_min_count = ['min_count' => 3];
541+
$arr_max_count = ['max_count' => 3];
542+
$arr_allow_empty = ['allow_empty' => false];
543+
544+
$this->assertTrue(Validator::isArray([1,2,3], $arr_count));
545+
$this->assertFalse(Validator::isArray([1], $arr_count));
546+
$this->assertTrue(Validator::isArray([1,2,3,4,5], $arr_min_count));
547+
$this->assertFalse(Validator::isArray([1,2], $arr_min_count));
548+
$this->assertTrue(Validator::isArray([1,2,3], $arr_max_count));
549+
$this->assertFalse(Validator::isArray([1,2,3,4], $arr_max_count));
550+
$this->assertTrue(Validator::isArray([1,2,3], $arr_allow_empty));
551+
$this->assertFalse(Validator::isArray([], $arr_allow_empty));
537552
}
538553

539554
/**

0 commit comments

Comments
 (0)