From 9c3902816b32a3bbe12b9fce43d6bda6d1ff7466 Mon Sep 17 00:00:00 2001 From: twinh Date: Wed, 28 Oct 2020 18:42:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(Validator):=20=E5=A2=9E=E5=8A=A0=20Childre?= =?UTF-8?q?n=20=E8=A7=84=E5=88=99=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=A4=9A=E7=BA=A7=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Validator/Children.php | 69 +++++++++++++++++++ lib/Validator/i18n/zh-CN.php | 3 + tests/unit/Validator/ChildrenTest.php | 97 +++++++++++++++++++++++++++ tests/unit/Validator/TestCase.php | 23 +++++-- 4 files changed, 187 insertions(+), 5 deletions(-) create mode 100644 lib/Validator/Children.php create mode 100644 tests/unit/Validator/ChildrenTest.php diff --git a/lib/Validator/Children.php b/lib/Validator/Children.php new file mode 100644 index 000000000..c695d5faa --- /dev/null +++ b/lib/Validator/Children.php @@ -0,0 +1,69 @@ + + * @mixin \ValidateMixin + */ +class Children extends BaseValidator +{ + protected $typeMessage = '%name% must be array or object'; + + /** + * @var V + */ + protected $v; + + /** + * @var Validate + */ + private $selfValidator; + + /** + * {@inheritdoc} + */ + public function __invoke($input, V $v = null) + { + $v && $this->storeOption('v', $v); + + return $this->isValid($input); + } + + /** + * {@inheritdoc} + */ + protected function doValidate($input) + { + if (!is_array($input) && !is_object($input)) { + $this->addError('type'); + return false; + } + + $options = $this->v->getOptions(); + $this->selfValidator = $this->validate(['data' => $input] + $options); + return $this->selfValidator->isValid(); + } + + /** + * {@inheritdoc} + */ + public function getMessages($name = null) + { + if ($this->getErrors()) { + return parent::getMessages($name); + } + return $this->selfValidator->getFlatMessages(); + } +} diff --git a/lib/Validator/i18n/zh-CN.php b/lib/Validator/i18n/zh-CN.php index a9b53b706..fac2dd622 100644 --- a/lib/Validator/i18n/zh-CN.php +++ b/lib/Validator/i18n/zh-CN.php @@ -37,6 +37,9 @@ '%name% must be %length% characters' => '%name%必须是%length%个字符', '%name% must be between %min% to %max% characters' => '%name%必须包含%min%-%max%个字符', + // children + '%name% must be array or object' => '%name%必须是数组或对象', + // chinese '%name% must contain only Chinese characters' => '%name%只能由中文组成', '%name% must not contain only Chinese characters' => '%name%不能只由中文组成', diff --git a/tests/unit/Validator/ChildrenTest.php b/tests/unit/Validator/ChildrenTest.php new file mode 100644 index 000000000..adc2d45b1 --- /dev/null +++ b/tests/unit/Validator/ChildrenTest.php @@ -0,0 +1,97 @@ +t->setLocale('en'); + } + + public function testChildrenSuc() + { + $ret = V + ::key('configs')->children( + V + ::key('key1', '配置1')->minLength(3) + ->key('key2', '配置2')->minLength(2) + ) + ->check([ + 'configs' => [ + 'key1' => '123', + 'key2' => '22', + ], + ]); + + $this->assertRetSuc($ret); + } + + public function testChildrenErr() + { + $ret = V + ::key('configs')->children( + V + ::key('key1', '配置1')->minLength(3) + ->key('key2', '配置2')->minLength(2) + ) + ->check([ + 'configs' => [ + 'key1' => '1', + 'key2' => '2', + ], + ]); + + $this->assertRetErr($ret, null, '配置1 must have a length greater than 3'); + } + + public function testChildrenNestedErr() + { + $ret = V + ::key('configs') + ->children( + V + ::key('key1', '配置1')->minLength(3) + ->key('key2', '配置2')->children( + V::key('key2.1', '配置2.1')->minLength(2) + ) + ) + ->check([ + 'configs' => [ + 'key1' => '123', + 'key2' => [ + 'key2.1' => '1', + ], + ], + ]); + + $this->assertRetErr($ret, null, '配置2.1 must have a length greater than 2'); + } + + public function testInvalidInputType() + { + $ret = V + ::key('configs', '配置')->children( + V::key('key1', '配置1')->minLength(3) + ) + ->check([ + 'configs' => 123, + ]); + + $this->assertRetErr($ret, null, '配置 must be array or object'); + } + + protected function getInputTestOptions() + { + return [ + 'v' => V::key('name'), + ]; + } +} diff --git a/tests/unit/Validator/TestCase.php b/tests/unit/Validator/TestCase.php index a4c434426..20ef41250 100644 --- a/tests/unit/Validator/TestCase.php +++ b/tests/unit/Validator/TestCase.php @@ -37,15 +37,23 @@ public function providerForInput() // Initial test fixtures $data = [ // boolean - true, false, + true, + false, // integer - 1234, -123, 0123, 0x1A, + 1234, + -123, + 0123, + 0x1A, // float - 1.234, 1.2e3, 7E-10, + 1.234, + 1.2e3, + 7E-10, // string 'this is a simple string', // object - new \stdClass(), new \ArrayObject([1, 3]), new \DateTime(), + new \stdClass(), + new \ArrayObject([1, 3]), + new \DateTime(), // resource curl_init(), // null @@ -71,7 +79,7 @@ public function testInput($input) { // Gets validator name WeiTest\Validator\LengthTest => Length $name = $this->name ?: substr(static::class, strrpos(static::class, '\\') + 1, -4); - $validator = $this->validate->createRuleValidator($name, $this->inputTestOptions); + $validator = $this->validate->createRuleValidator($name, $this->getInputTestOptions()); // The validator should accept any type of INPUT and do NOT raise any // exceptions or errors @@ -86,4 +94,9 @@ public function createResource() return static::$resource; } + + protected function getInputTestOptions() + { + return $this->inputTestOptions; + } }