diff --git a/src/Rule/DateRangeRule.php b/src/Rule/DateRangeRule.php index 477a25f..a2dc57e 100644 --- a/src/Rule/DateRangeRule.php +++ b/src/Rule/DateRangeRule.php @@ -10,11 +10,11 @@ namespace Swoft\Validator\Rule; -use DateTime; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Validator\Annotation\Mapping\DateRange; use Swoft\Validator\Contract\RuleInterface; use Swoft\Validator\Exception\ValidatorException; +use function strtotime; /** * Class AlphaRule @@ -26,37 +26,40 @@ class DateRangeRule implements RuleInterface { /** - * @param array $data - * @param string $propertyName - * @param object $item - * @param null $default + * @param array $data + * @param string $propertyName + * @param object|DateRange $item + * @param null $default + * @param bool $strict * * @return array - * @throws ValidatorException */ public function validate(array $data, string $propertyName, $item, $default = null, $strict = false): array { - /* @var DateRange $item */ - $start = $item->getStart(); - $end = $item->getEnd(); + $endTs = strtotime($end = $item->getEnd()); $value = $data[$propertyName]; + + $startTs = strtotime($start = $item->getStart()); if (is_string($value)) { - $dt = DateTime::createFromFormat('Y-m-d H:i:s', $value); - if (($dt !== false && !array_sum($dt::getLastErrors())) && strtotime($value) >= strtotime($start) && $value <= strtotime($end)) { + // $dt = DateTime::createFromFormat('Y-m-d H:i:s', $value); + $ts = strtotime($value); + if ($ts > 0 && $ts >= $startTs && $ts <= $endTs) { + return $data; + } + + // is timestamp + if (ctype_digit($value) && date('Y-m-d', (int)$value) && $value >= $startTs && $value <= $endTs) { return $data; - } elseif (ctype_digit($value)) { - if (date('Y-m-d', (int)$value) && $value >= strtotime($start) && $value <= strtotime($end)) { - return $data; - } } } elseif (filter_var($value, FILTER_VALIDATE_INT)) { - if ($value >= strtotime($start) && $value <= strtotime($end)) { + if ($value >= $startTs && $value <= $endTs) { return $data; } } + $message = $item->getMessage(); - $message = (empty($message)) ? - sprintf('%s is invalid date range(start=%s, end=%s)', $propertyName, $start, $end) : $message; + $message = $message ?: sprintf('%s is invalid date range(start=%s, end=%s)', $propertyName, $start, $end); + throw new ValidatorException($message); } } diff --git a/test/unit/ValidatorRuleTest.php b/test/unit/ValidatorRuleTest.php index 75e6770..bb78e0b 100644 --- a/test/unit/ValidatorRuleTest.php +++ b/test/unit/ValidatorRuleTest.php @@ -21,7 +21,7 @@ public function testAfterDateError(): void $data = [ 'dataAfterDate' => '2019-07-06' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('dataAfterDate must be after 2019-07-08'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAfterDate')); } @@ -43,7 +43,7 @@ public function testAlphaError(): void $data = [ 'dataAlpha' => 'abcde0123' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('alpha message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlpha')); } @@ -65,7 +65,7 @@ public function testAlphaDashError(): void $data = [ 'dataAlphaDash' => '.=' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('alphadash message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlphaDash')); } @@ -88,7 +88,7 @@ public function testAlphaNumError(): void 'dataAlphaNum' => 'abcde-' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('alphanum message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testAlphaNum')); } @@ -110,7 +110,7 @@ public function testBeforeDateError(): void $data = [ 'dataBeforeDate' => '2019-07-10' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('before date message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testBeforeDate')); } @@ -132,7 +132,7 @@ public function testChsError(): void $data = [ 'dataChs' => 'english' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('chs message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChs')); } @@ -154,7 +154,7 @@ public function testChsAlphaError(): void $data = [ 'dataChsAlpha' => '-_' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('chsalpha message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlpha')); } @@ -176,7 +176,7 @@ public function testChsAlphaDashError(): void $data = [ 'dataChsAlphaDash' => '>?' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('chsalphadash message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlphaDash')); } @@ -198,7 +198,7 @@ public function testChsAlphaNumError(): void $data = [ 'dataChsAlphaNum' => '-_' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('chsalphanum message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testChsAlphaNum')); } @@ -221,7 +221,7 @@ public function testConfirmError(): void 'dataConfirm' => '123', 'confirm' => '456' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('confirm message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testConfirm')); } @@ -245,7 +245,7 @@ public function testDifferentError(): void 'dataDifferent' => '123', 'different' => '123' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('different message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDifferent')); } @@ -269,7 +269,7 @@ public function testGreaterThanError(): void 'dataGreaterThan' => '12', 'gt' => '123' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('greaterthan message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testGreaterThan')); } @@ -293,7 +293,7 @@ public function testLessThanError(): void 'dataLessThan' => '124', 'lt' => '123' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('lessthan message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testLessThan')); } @@ -316,7 +316,7 @@ public function testDateError(): void $data = [ 'dataDate' => '2019f' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('date message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDate')); } @@ -338,7 +338,7 @@ public function testDateRangeError(): void $data = [ 'dataDateRange' => '2019-06-18' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('daterange message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDateRange')); } @@ -352,6 +352,7 @@ public function testDateRangeSuccess(): void $data, $this->getValidates(ValidatorRule::class, 'testDateRange') ); + $this->assertEquals($data, $result); } @@ -360,7 +361,7 @@ public function testDnsError(): void $data = [ 'dataDns' => 'swoft.con' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('dns message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testDns')); } @@ -412,7 +413,7 @@ public function testLowError(): void $data = [ 'dataLow' => 'swofT' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('low message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testLow')); } @@ -434,7 +435,7 @@ public function testNotInEnumError(): void $data = [ 'dataNotInEnum' => '1' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('notinenum message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testNotInEnum')); } @@ -456,7 +457,7 @@ public function testNotInRangeError(): void $data = [ 'dataNotInRange' => '1' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('notinrange message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testNotInRange')); } @@ -478,7 +479,7 @@ public function testUpperError(): void $data = [ 'dataUpper' => 'sWOFT' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('upper message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testUpper')); } @@ -500,7 +501,7 @@ public function testUrlError(): void $data = [ 'dataUrl' => 'baidu.com' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('url message'); (new Validator())->validateRequest($data, $this->getValidates(ValidatorRule::class, 'testUrl')); } diff --git a/test/unit/ValidatorTest.php b/test/unit/ValidatorTest.php index ed24ca9..c2bee04 100644 --- a/test/unit/ValidatorTest.php +++ b/test/unit/ValidatorTest.php @@ -24,7 +24,7 @@ class ValidatorTest extends TestCase public function testTypeEmail(): void { $data = []; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('email must exist!'); (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testEmail')); } @@ -34,7 +34,7 @@ public function testFailEmail(): void $data = [ 'email' => 'swoft' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('email messsage'); (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testEmail')); } @@ -45,7 +45,7 @@ public function testFailEmail2(): void 'email' => 'swoft' ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); + $this->expectException(ValidatorException::class); $this->expectExceptionMessage('email messsage'); (new Validator())->validate($data, 'testDefaultValidator', ['email']); } @@ -79,8 +79,8 @@ public function testFailEnum(): void $data = [ 'enum' => 1, ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); - $this->expectExceptionMessage('enum messsage'); + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('enum message'); (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testEnum')); } @@ -97,25 +97,16 @@ public function testEnum(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage ip message - * - * @throws ValidatorException - */ public function testFailIp(): void { $data = [ 'ip' => '11', ]; - $this->expectException(\Swoft\Validator\Exception\ValidatorException::class); - $this->expectExceptionMessage('ip messsage'); + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('ip message'); (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testIp')); } - /** - * @throws ValidatorException - */ public function testIp(): void { $data = [ @@ -126,17 +117,14 @@ public function testIp(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage length message - * - * @throws ValidatorException - */ public function testFailLength(): void { $data = [ 'length' => '1', ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('length message'); (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testLength')); } @@ -153,17 +141,15 @@ public function testLength(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage max message - * - * @throws ValidatorException - */ public function testFailMax(): void { $data = [ 'max' => 18, ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('max message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testMax')); } @@ -180,17 +166,15 @@ public function testMax(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage min message - * - * @throws ValidatorException - */ public function testFailMin(): void { $data = [ 'min' => 0, ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('min message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testMin')); } @@ -207,17 +191,15 @@ public function testMin(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage mobile message - * - * @throws ValidatorException - */ public function testFailMobile(): void { $data = [ 'mobile' => '13442', ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('mobile message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testMobile')); } @@ -234,17 +216,15 @@ public function testMobile(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage not empty message - * - * @throws ValidatorException - */ public function testFailNotEmpty(): void { $data = [ 'notEmpty' => '', ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('not empty message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testNotEmpty')); } @@ -264,17 +244,15 @@ public function testNotEmpty(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage pattern message - * - * @throws ValidatorException - */ public function testFailPattern(): void { $data = [ 'pattern' => 'swift', ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('pattern message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testPattern')); } @@ -291,31 +269,27 @@ public function testPattern(): void $this->assertEquals($result, $data); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage range message - * - * @throws ValidatorException - */ public function testFailRange(): void { $data = [ 'range' => 100, ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('range message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo2::class, 'testRange')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage range message - * - * @throws ValidatorException - */ public function testFailRange2(): void { $data = [ 'range' => 100, ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('range message'); + (new Validator())->validate($data, 'testDefaultValidator', ['range']); } diff --git a/test/unit/ValidatorTypeTest.php b/test/unit/ValidatorTypeTest.php index df53810..f1299bf 100644 --- a/test/unit/ValidatorTypeTest.php +++ b/test/unit/ValidatorTypeTest.php @@ -29,111 +29,89 @@ public function testArrayType(): void (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testArray')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage int must exist! - * - * @throws ValidatorException - */ public function testIntType(): void { $data = []; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('int must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testInt')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage bool must exist! - * - * @throws ValidatorException - */ public function testBoolType(): void { $data = []; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('bool must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testBool')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage string must exist! - * - * @throws ValidatorException - */ public function testStringType(): void { $data = []; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('string must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testString')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage float must exist! - * - * @throws ValidatorException - */ public function testFloatType(): void { $data = []; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('float must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testFloat')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage array message - * - * @throws ValidatorException - */ public function testArrayTypeMessage(): void { $data = []; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('array message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testArrayMessage')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage int message - * - * @throws ValidatorException - */ public function testIntTypeMessage(): void { $data = []; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('int message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testIntMessage')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage bool message - * - * @throws ValidatorException - */ public function testBoolTypeMessage(): void { $data = []; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('bool message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testBoolMessage')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage string message - * - * @throws ValidatorException - */ public function testStringTypeMessage(): void { $data = []; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('string message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testStringMessage')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage float message - * - * @throws ValidatorException - */ public function testFloatTypeMessage(): void { $data = []; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('float message'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testFloatMessage')); } @@ -166,73 +144,59 @@ public function testName(): void $this->assertEquals(['swoftName' => 'swoft'], $result); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage swoftName must string! - * - * @throws ValidatorException - */ public function testFailName(): void { $data = [ 'swoftName' => 12 ]; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('swoftName must string!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testName')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage int must exist! - * - * @throws ValidatorException - */ public function testIntTypeQuery(): void { $data = [ 'int' => 1, ]; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('int must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testIntQuery')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage bool must exist! - * - * @throws ValidatorException - */ public function testBoolTypeQuery(): void { $data = [ 'bool' => false ]; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('bool must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testBoolQuery')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage string must exist! - * - * @throws ValidatorException - */ public function testStringTypeQuery(): void { $data = [ 'string' => 'string' ]; + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('string must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testStringQuery')); } - /** - * @expectedException Swoft\Validator\Exception\ValidatorException - * @expectedExceptionMessage float must exist! - * - * @throws ValidatorException - */ public function testFloatTypeQuery(): void { $data = [ 'float' => 1.1 ]; + + $this->expectException(ValidatorException::class); + $this->expectExceptionMessage('float must exist!'); + (new Validator())->validateRequest($data, $this->getValidates(ValidateDemo::class, 'testFloatQuery')); }