diff --git a/library/Exceptions/ArrayValException.php b/library/Exceptions/ArrayValException.php index de1228c1..c7b5b028 100644 --- a/library/Exceptions/ArrayValException.php +++ b/library/Exceptions/ArrayValException.php @@ -13,14 +13,21 @@ declare(strict_types=1); namespace Respect\Validation\Exceptions; -class ArrayValException extends ValidationException +/** + * @author Alexandre Gomes Gaigalas + * @author Henrique Moody + */ +final class ArrayValException extends ValidationException { + /** + * {@inheritdoc} + */ public static $defaultTemplates = [ self::MODE_DEFAULT => [ - self::STANDARD => '{{name}} must be an array', + self::STANDARD => '{{name}} must be an array value', ], self::MODE_NEGATIVE => [ - self::STANDARD => '{{name}} must not be an array', + self::STANDARD => '{{name}} must not be an array value', ], ]; } diff --git a/library/Rules/ArrayVal.php b/library/Rules/ArrayVal.php index 5607f0c4..753cc912 100644 --- a/library/Rules/ArrayVal.php +++ b/library/Rules/ArrayVal.php @@ -16,8 +16,17 @@ namespace Respect\Validation\Rules; use ArrayAccess; use SimpleXMLElement; -class ArrayVal extends AbstractRule +/** + * Validates if the input is an array or if the input can be used as an array (instance of `ArrayAccess` or `SimpleXMLElement`). + * + * @author Alexandre Gomes Gaigalas + * @author Henrique Moody + */ +final class ArrayVal extends AbstractRule { + /** + * {@inheritdoc} + */ public function validate($input): bool { return is_array($input) || $input instanceof ArrayAccess || $input instanceof SimpleXMLElement; diff --git a/tests/integration/rules/arrayVal.phpt b/tests/integration/rules/arrayVal.phpt new file mode 100644 index 00000000..3e820a0f --- /dev/null +++ b/tests/integration/rules/arrayVal.phpt @@ -0,0 +1,38 @@ +--FILE-- +check('Bla %123'); +} catch (ArrayValException $exception) { + echo $exception->getMainMessage().PHP_EOL; +} + +try { + v::not(v::arrayVal())->check([42]); +} catch (ArrayValException $exception) { + echo $exception->getMainMessage().PHP_EOL; +} + +try { + v::arrayVal()->assert(new stdClass()); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} + +try { + v::not(v::arrayVal())->assert(new ArrayObject([2, 3])); +} catch (NestedValidationException $exception) { + echo $exception->getFullMessage().PHP_EOL; +} +?> +--EXPECTF-- +"Bla %123" must be an array value +`{ 42 }` must not be an array value +- `[object] (stdClass: { })` must be an array value +- `[traversable] (ArrayObject: { 2, 3 })` must not be an array value diff --git a/tests/integration/rules/arrayVal_1.phpt b/tests/integration/rules/arrayVal_1.phpt deleted file mode 100644 index a76ca0a5..00000000 --- a/tests/integration/rules/arrayVal_1.phpt +++ /dev/null @@ -1,11 +0,0 @@ ---FILE-- -assert(['asdf', 'lkjh']); -v::arrayVal()->check(new ArrayObject([2, 3])); -?> ---EXPECTF-- diff --git a/tests/integration/rules/arrayVal_2.phpt b/tests/integration/rules/arrayVal_2.phpt deleted file mode 100644 index bcf9fa92..00000000 --- a/tests/integration/rules/arrayVal_2.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---FILE-- -check('Bla %123'); -} catch (ArrayValException $exception) { - echo $exception->getMainMessage(); -} -?> ---EXPECTF-- -"Bla %123" must be an array diff --git a/tests/integration/rules/arrayVal_3.phpt b/tests/integration/rules/arrayVal_3.phpt deleted file mode 100644 index ef1be452..00000000 --- a/tests/integration/rules/arrayVal_3.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---FILE-- -assert(new stdClass()); -} catch (AllOfException $exception) { - echo $exception->getFullMessage(); -} -?> ---EXPECTF-- -- `[object] (stdClass: { })` must be an array diff --git a/tests/integration/rules/arrayVal_4.phpt b/tests/integration/rules/arrayVal_4.phpt deleted file mode 100644 index 330b4bf6..00000000 --- a/tests/integration/rules/arrayVal_4.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---FILE-- -check([42]); -} catch (ArrayValException $exception) { - echo $exception->getMainMessage(); -} -?> ---EXPECTF-- -`{ 42 }` must not be an array diff --git a/tests/integration/rules/arrayVal_5.phpt b/tests/integration/rules/arrayVal_5.phpt deleted file mode 100644 index 0f4194b8..00000000 --- a/tests/integration/rules/arrayVal_5.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---FILE-- -assert(new ArrayObject([2, 3])); -} catch (AllOfException $exception) { - echo $exception->getFullMessage(); -} -?> ---EXPECTF-- -- `[traversable] (ArrayObject: { 2, 3 })` must not be an array diff --git a/tests/unit/Rules/ArrayValTest.php b/tests/unit/Rules/ArrayValTest.php index b7307683..a55c91c6 100644 --- a/tests/unit/Rules/ArrayValTest.php +++ b/tests/unit/Rules/ArrayValTest.php @@ -22,8 +22,11 @@ use stdClass; * @group rule * @covers \Respect\Validation\Rules\ArrayVal */ -class ArrayValTest extends RuleTestCase +final class ArrayValTest extends RuleTestCase { + /** + * {@inheritdoc} + */ public function providerForValidInput(): array { $rule = new ArrayVal(); @@ -36,6 +39,9 @@ class ArrayValTest extends RuleTestCase ]; } + /** + * {@inheritdoc} + */ public function providerForInvalidInput(): array { $rule = new ArrayVal();