Skip to content

Commit a221ff4

Browse files
committed
Merge branch 'release/0.3.2'
2 parents ee2b8ec + 18000a1 commit a221ff4

File tree

4 files changed

+160
-6
lines changed

4 files changed

+160
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: php
22

33
php:
44
- 7.0
5+
- 7.1
56

67
before_script:
78
- composer install

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.3.1",
4+
"version": "0.3.2",
55
"license": "MIT",
66
"authors": [
77
{

src/Validator.php

Lines changed: 94 additions & 5 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
/**
@@ -606,7 +624,33 @@ protected function validateObject($value, array $rule = []): bool
606624
*/
607625
protected function validateString($value, array $rule = []): bool
608626
{
609-
return is_string($value);
627+
$valid = is_string($value);
628+
629+
if (isset($rule['equals']) && is_string($rule['equals'])) {
630+
$valid = $valid && $value == $rule['equals'];
631+
} elseif (isset($rule['in']) && is_array($rule['in'])) {
632+
$valid = $valid && in_array($value, $rule['in']);
633+
} elseif (isset($rule['regex']) && is_string($rule['regex'])) {
634+
$valid = $valid && preg_match($rule['regex'], $value);
635+
}
636+
637+
if (isset($rule['allow_empty']) && $rule['allow_empty'] === false) {
638+
$valid = $valid && !empty($value);
639+
}
640+
641+
if (isset($rule['allow_null']) && $rule['allow_null'] === true) {
642+
$valid = $valid || is_null($value);
643+
}
644+
645+
if (isset($rule['min_length']) && is_int($rule['min_length'])) {
646+
$valid = $valid && (strlen($value) >= $rule['min_length']);
647+
}
648+
649+
if (isset($rule['max_length']) && is_int($rule['max_length'])) {
650+
$valid = $valid && (strlen($value) <= $rule['max_length']);
651+
}
652+
653+
return $valid;
610654
}
611655

612656
/**
@@ -615,10 +659,47 @@ protected function validateString($value, array $rule = []): bool
615659
* @param mixed $value
616660
* @param array $rule
617661
* @return bool
662+
* @throws \Exception
618663
*/
619664
protected function validateInteger($value, array $rule = []): bool
620665
{
621-
return is_int($value);
666+
$valid = is_int($value);
667+
668+
if (isset($rule['unsigned']) && $rule['unsigned'] === true) {
669+
// Validate that int is unsigned
670+
if ($valid = $valid && ($value > 0)) {
671+
// Unsigned integer violation check
672+
foreach (['>', '>=', '<', '<=', 'equals'] as $r) {
673+
if (isset($rule[$r]) && $rule[$r] < 0) {
674+
throw new \Exception("'$r' must be >= 0 for unsigned integers.");
675+
}
676+
}
677+
}
678+
}
679+
680+
if (isset($rule['equals']) && is_int($rule['equals'])) {
681+
$valid = $valid && $value == $rule['equals'];
682+
}
683+
684+
if (isset($rule['in']) && is_array($rule['in'])) {
685+
$valid = $valid && in_array($value, $rule['in']);
686+
}
687+
688+
// Validate > or >=, with > getting priority over >=
689+
if (isset($rule['>']) && is_int($rule['>'])) {
690+
$valid = $valid && ($value > $rule['>']);
691+
} elseif (isset($rule['>=']) && is_int($rule['>='])) {
692+
$valid = $valid && ($value >= $rule['>=']);
693+
}
694+
695+
// Validate < or <=, with < getting priority over <=
696+
if (isset($rule['<']) && is_int($rule['<'])) {
697+
$valid = $valid && ($value < $rule['<']);
698+
} elseif (isset($rule['<=']) && is_int($rule['<='])) {
699+
$valid = $valid && ($value <= $rule['<=']);
700+
}
701+
702+
return $valid;
622703
}
623704

624705
/**
@@ -743,18 +824,26 @@ protected function validateDateTime($value, array $rule = []): bool
743824
}
744825

745826
$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+
}
746834

747835
// Returns a timestamp on success, FALSE otherwise
748-
if (($time = strtotime($value)) === false) {
836+
if (($time = strtotime($compare)) === false) {
749837
return false;
750838
}
751839

752840
// Returns new \DateTime instance on success, FALSE otherwise
753-
if (($date = date_create($value)) === false) {
841+
if (($date = date_create($compare)) === false) {
754842
return false;
755843
}
756844

757845
if (isset($rule['format'])) {
846+
// Use original value for comparing format
758847
return $date->format($rule['format']) == $value;
759848
}
760849

tests/ValidatorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,29 @@ public function testValidateString()
334334
Validator::is('string', $value)
335335
);
336336
}
337+
338+
// Test string rules
339+
$str_min_length = ['min_length' => 1];
340+
$str_max_length = ['max_length' => 3];
341+
$str_allow_empty = ['allow_empty' => false];
342+
$str_allow_null = ['allow_null' => true];
343+
$str_equals = ['equals' => 'Hello, World!'];
344+
$str_in = ['in' => ['hello', 'world', 'foo', 'bar']];
345+
$str_regex = ['regex' => "/php/i"];
346+
347+
$this->assertTrue(Validator::isString('abc', array_merge($str_min_length, $str_max_length)));
348+
$this->assertFalse(Validator::isString('', $str_min_length));
349+
$this->assertFalse(Validator::isString('abcd', $str_max_length));
350+
$this->assertTrue(Validator::isString(''));
351+
$this->assertFalse(Validator::isString('', $str_allow_empty));
352+
$this->assertFalse(Validator::isString(null));
353+
$this->assertTrue(Validator::isString(null, $str_allow_null));
354+
$this->assertTrue(Validator::isString('Hello, World!', $str_equals));
355+
$this->assertFalse(Validator::isString('Hello World!', $str_equals));
356+
$this->assertTrue(Validator::isString('foo', $str_in));
357+
$this->assertFalse(Validator::isString('cat', $str_in));
358+
$this->assertTrue(Validator::isString('PHP is my favorite!', $str_regex));
359+
$this->assertFalse(Validator::isString('Java is my favorite!', $str_regex));
337360
}
338361

339362
/**
@@ -361,6 +384,32 @@ public function testValidateInteger()
361384
Validator::is('integer', $value)
362385
);
363386
}
387+
388+
// Test integer rules
389+
$int_unsigned = ['unsigned' => true];
390+
$int_gt = ['>' => 3];
391+
$int_gteq = ['>=' => 3];
392+
$int_lt = ['<' => 10];
393+
$int_lteq = ['<=' => 10];
394+
$int_equals = ['equals' => 5];
395+
$int_in = ['in' => [1, 2, 3]];
396+
397+
$this->assertTrue(Validator::isInteger(3, $int_unsigned));
398+
$this->assertFalse(Validator::isInteger(-1, $int_unsigned));
399+
$this->assertTrue(Validator::isInteger(4, $int_gt));
400+
$this->assertFalse(Validator::isInteger(3, $int_gt));
401+
$this->assertTrue(Validator::isInteger(3, $int_gteq));
402+
$this->assertFalse(Validator::isInteger(2, $int_gteq));
403+
$this->assertTrue(Validator::isInteger(3, $int_lt));
404+
$this->assertFalse(Validator::isInteger(10, $int_lt));
405+
$this->assertTrue(Validator::isInteger(10, $int_lteq));
406+
$this->assertFalse(Validator::isInteger(11, $int_lteq));
407+
$this->assertTrue(Validator::isInteger(5, array_merge($int_gt, $int_lt)));
408+
$this->assertTrue(Validator::isInteger(5, array_merge($int_gteq, $int_lteq)));
409+
$this->assertTrue(Validator::isInteger(5, $int_equals));
410+
$this->assertFalse(Validator::isInteger(1, $int_equals));
411+
$this->assertTrue(Validator::isInteger(2, $int_in));
412+
$this->assertFalse(Validator::isInteger(4, $int_in));
364413
}
365414

366415
/**
@@ -488,6 +537,21 @@ public function testValidateArray()
488537
Validator::isArray($value)
489538
);
490539
}
540+
541+
// Test array rules
542+
$arr_count = ['count' => 3];
543+
$arr_min_count = ['min_count' => 3];
544+
$arr_max_count = ['max_count' => 3];
545+
$arr_allow_empty = ['allow_empty' => false];
546+
547+
$this->assertTrue(Validator::isArray([1,2,3], $arr_count));
548+
$this->assertFalse(Validator::isArray([1], $arr_count));
549+
$this->assertTrue(Validator::isArray([1,2,3,4,5], $arr_min_count));
550+
$this->assertFalse(Validator::isArray([1,2], $arr_min_count));
551+
$this->assertTrue(Validator::isArray([1,2,3], $arr_max_count));
552+
$this->assertFalse(Validator::isArray([1,2,3,4], $arr_max_count));
553+
$this->assertTrue(Validator::isArray([1,2,3], $arr_allow_empty));
554+
$this->assertFalse(Validator::isArray([], $arr_allow_empty));
491555
}
492556

493557
/**

0 commit comments

Comments
 (0)