diff --git a/spec/Check/CountCheckSpec.php b/spec/Check/CountCheckSpec.php index 2dc068a..dd36f12 100644 --- a/spec/Check/CountCheckSpec.php +++ b/spec/Check/CountCheckSpec.php @@ -27,20 +27,20 @@ function it_implements_type_interface(TypeInterface $child) function it_returns_a_result_on_invalid_child_check(TypeInterface $child, ResultInterface $result) { $result->isValid()->willReturn(false); - $child->check(Argument::any())->willReturn($result); + $child->check('', Argument::any())->willReturn($result); $this->beConstructedWith($child, 1); - $this->check([])->shouldHaveType(ResultInterface::class); + $this->check('', [])->shouldHaveType(ResultInterface::class); } function it_returns_a_result_on_check(TypeInterface $child, ResultInterface $result) { $result->isValid()->willReturn(true); - $child->check(Argument::any())->willReturn($result); + $child->check('', Argument::any())->willReturn($result); $this->beConstructedWith($child, 1); - $this->check([3])->shouldHaveType(ResultInterface::class); + $this->check('', [3])->shouldHaveType(ResultInterface::class); } } diff --git a/spec/Check/NumericRangeCheckSpec.php b/spec/Check/NumericRangeCheckSpec.php index 25f72f1..ff38e28 100644 --- a/spec/Check/NumericRangeCheckSpec.php +++ b/spec/Check/NumericRangeCheckSpec.php @@ -28,20 +28,20 @@ function it_implements_type_interface(TypeInterface $child) function it_returns_a_result_on_invalid_child_check(TypeInterface $child, ResultInterface $result) { $result->isValid()->willReturn(false); - $child->check(Argument::any())->willReturn($result); + $child->check('', Argument::any())->willReturn($result); $this->beConstructedWith($child, 0, 1); - $this->check(Argument::any())->shouldHaveType(ResultInterface::class); + $this->check('', Argument::any())->shouldHaveType(ResultInterface::class); } function it_returns_a_result_on_check(TypeInterface $child, ResultInterface $result) { $result->isValid()->willReturn(true); - $child->check(Argument::any())->willReturn($result); + $child->check('', Argument::any())->willReturn($result); $this->beConstructedWith($child, 0, 1); - $this->check(0)->shouldHaveType(ResultInterface::class); + $this->check('', 0)->shouldHaveType(ResultInterface::class); } } diff --git a/spec/CheckerSpec.php b/spec/CheckerSpec.php index 73b3340..548ec51 100644 --- a/spec/CheckerSpec.php +++ b/spec/CheckerSpec.php @@ -14,14 +14,15 @@ function it_is_initializable() $this->shouldHaveType(Checker::class); } - function it_accepts_a_type_and_an_array_as_parameter_for_fulfills(TypeInterface $type) + function it_accepts_a_type_and_an_array_as_parameter_for_fulfills(TypeInterface $type, ResultInterface $result) { + $type->check('', [])->willReturn($result); $this->fulfills([], $type); } function it_returns_the_result_of_the_type_in_fulfills(TypeInterface $type, ResultInterface $result) { - $type->check([])->willReturn($result); + $type->check('', [])->willReturn($result); $this->fulfills([], $type)->shouldBe($result); } } diff --git a/spec/ResultSpec.php b/spec/ResultSpec.php index d56c5ab..5817098 100644 --- a/spec/ResultSpec.php +++ b/spec/ResultSpec.php @@ -28,7 +28,7 @@ function it_will_return_is_invalid_if_it_was_set_in_constructor() function it_will_return_an_empty_array_if_no_error_list_is_given() { - $this->beConstructedWith(true); + $this->beConstructedWith(true, []); $this->getErrors()->shouldHaveCount(0); } } diff --git a/spec/Type/AnyTypeSpec.php b/spec/Type/AnyTypeSpec.php index a6d7327..422935d 100644 --- a/spec/Type/AnyTypeSpec.php +++ b/spec/Type/AnyTypeSpec.php @@ -15,25 +15,25 @@ function it_is_initializable() function it_should_return_valid_for_null() { - $this->check(null)->isValid()->shouldBe(true); + $this->check('', null)->isValid()->shouldBe(true); } function it_should_return_empty_errors_for_null() { - $this->check(null)->getErrors()->shouldHaveCount(0); + $this->check('', null)->getErrors()->shouldHaveCount(0); } function it_should_return_valid_for_all_values() { - $this->check(true)->isValid()->shouldBe(true); - $this->check("foo")->isValid()->shouldBe(true); - $this->check(13)->isValid()->shouldBe(true); + $this->check('', true)->isValid()->shouldBe(true); + $this->check('', "foo")->isValid()->shouldBe(true); + $this->check('', 13)->isValid()->shouldBe(true); } function it_should_return_empty_errors_for_all_values() { - $this->check(true)->getErrors()->shouldHaveCount(0); - $this->check("foo")->getErrors()->shouldHaveCount(0); - $this->check(13)->getErrors()->shouldHaveCount(0); + $this->check('', true)->getErrors()->shouldHaveCount(0); + $this->check('', "foo")->getErrors()->shouldHaveCount(0); + $this->check('', 13)->getErrors()->shouldHaveCount(0); } } diff --git a/spec/Type/BoolTypeSpec.php b/spec/Type/BoolTypeSpec.php index 30fc953..6a81345 100644 --- a/spec/Type/BoolTypeSpec.php +++ b/spec/Type/BoolTypeSpec.php @@ -15,16 +15,16 @@ function it_is_initializable() function it_should_return_valid_for_bool() { - $this->check(true)->isValid()->shouldBe(true); - $this->check(false)->isValid()->shouldBe(true); + $this->check('', true)->isValid()->shouldBe(true); + $this->check('', false)->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { - $this->check(null)->isValid()->shouldBe(false); - $this->check("foo")->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(1)->isValid()->shouldBe(false); - $this->check(1.0)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', "foo")->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', 1)->isValid()->shouldBe(false); + $this->check('', 1.0)->isValid()->shouldBe(false); } } diff --git a/spec/Type/DatetimeTypeSpec.php b/spec/Type/DatetimeTypeSpec.php index 177c5b8..b54efd7 100644 --- a/spec/Type/DatetimeTypeSpec.php +++ b/spec/Type/DatetimeTypeSpec.php @@ -18,18 +18,18 @@ function it_should_return_valid_for_correct_values() { $this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin'); - $this->check('12-12-2012 12:12:10')->isValid()->shouldBe(true); + $this->check('', '12-12-2012 12:12:10')->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { $this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin'); - $this->check(null)->isValid()->shouldBe(false); - $this->check('foo')->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(1.234)->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); - $this->check(false)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', 'foo')->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', 1.234)->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/EnumTypeSpec.php b/spec/Type/EnumTypeSpec.php index 73e7436..36cc405 100644 --- a/spec/Type/EnumTypeSpec.php +++ b/spec/Type/EnumTypeSpec.php @@ -18,18 +18,18 @@ function it_is_valid_for_all_allowed_values() { $this->beConstructedWith(['test', 1, null]); - $this->check('test')->isValid()->shouldBe(true); - $this->check(1)->isValid()->shouldBe(true); - $this->check(null)->isValid()->shouldBe(true); + $this->check('', 'test')->isValid()->shouldBe(true); + $this->check('', 1)->isValid()->shouldBe(true); + $this->check('', null)->isValid()->shouldBe(true); } function it_is_invalid_for_not_allowed_values() { $this->beConstructedWith(['test', 1, null]); - - $this->check('array')->isValid()->shouldBe(false); - $this->check(100)->isValid()->shouldBe(false); - $this->check(1.5)->isValid()->shouldBe(false); - $this->check(['test'])->isValid()->shouldBe(false); + + $this->check('', 'array')->isValid()->shouldBe(false); + $this->check('', 100)->isValid()->shouldBe(false); + $this->check('', 1.5)->isValid()->shouldBe(false); + $this->check('', ['test'])->isValid()->shouldBe(false); } } diff --git a/spec/Type/ExactValueTypeSpec.php b/spec/Type/ExactValueTypeSpec.php index f88dc8c..e6ebeae 100644 --- a/spec/Type/ExactValueTypeSpec.php +++ b/spec/Type/ExactValueTypeSpec.php @@ -17,6 +17,6 @@ function it_is_initializable() function it_is_valid_for_null_if_null_is_the_value() { $this->beConstructedWith(null); - $this->check(null)->isValid()->shouldBe(true); + $this->check('', null)->isValid()->shouldBe(true); } } diff --git a/spec/Type/FloatTypeSpec.php b/spec/Type/FloatTypeSpec.php index 48e6dd1..2df6598 100644 --- a/spec/Type/FloatTypeSpec.php +++ b/spec/Type/FloatTypeSpec.php @@ -14,19 +14,19 @@ function it_is_initializable() function it_should_return_valid_for_floats() { - $this->check(0.0)->isValid()->shouldBe(true); - $this->check(1.1)->isValid()->shouldBe(true); - $this->check(2.0)->isValid()->shouldBe(true); - $this->check(-144.2)->isValid()->shouldBe(true); + $this->check('', 0.0)->isValid()->shouldBe(true); + $this->check('', 1.1)->isValid()->shouldBe(true); + $this->check('', 2.0)->isValid()->shouldBe(true); + $this->check('', -144.2)->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { - $this->check(null)->isValid()->shouldBe(false); - $this->check("foo")->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(1)->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); - $this->check(false)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', "foo")->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', 1)->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/IntTypeSpec.php b/spec/Type/IntTypeSpec.php index cb8eaf1..c710003 100644 --- a/spec/Type/IntTypeSpec.php +++ b/spec/Type/IntTypeSpec.php @@ -15,19 +15,19 @@ function it_is_initializable() function it_should_return_valid_for_integers() { - $this->check(0)->isValid()->shouldBe(true); - $this->check(1)->isValid()->shouldBe(true); - $this->check(20)->isValid()->shouldBe(true); - $this->check(-144)->isValid()->shouldBe(true); + $this->check('', 0)->isValid()->shouldBe(true); + $this->check('', 1)->isValid()->shouldBe(true); + $this->check('', 20)->isValid()->shouldBe(true); + $this->check('', -144)->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { - $this->check(null)->isValid()->shouldBe(false); - $this->check("foo")->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(1.234)->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); - $this->check(false)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', "foo")->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', 1.234)->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/NullableTypeSpec.php b/spec/Type/NullableTypeSpec.php index 13bc912..0d68924 100644 --- a/spec/Type/NullableTypeSpec.php +++ b/spec/Type/NullableTypeSpec.php @@ -17,12 +17,12 @@ function it_is_initializable(TypeInterface $childType) function it_should_return_valid_for_null(TypeInterface $childType) { $this->beConstructedWith($childType); - $this->check(null)->isValid()->shouldBe(true); + $this->check('', null)->isValid()->shouldBe(true); } function it_should_return_the_value_from_the_child(TypeInterface $childType) { $this->beConstructedWith($childType); - $childType->check(false)->willReturn(new Result(false, [])); - $this->check(false)->isValid()->shouldBe(false); + $childType->check('', false)->willReturn(new Result(false, [])); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/NumericTypeSpec.php b/spec/Type/NumericTypeSpec.php index 8cb7af4..b3cfc29 100644 --- a/spec/Type/NumericTypeSpec.php +++ b/spec/Type/NumericTypeSpec.php @@ -15,26 +15,26 @@ function it_is_initializable() function it_should_return_valid_for_integers() { - $this->check(0)->isValid()->shouldBe(true); - $this->check(1)->isValid()->shouldBe(true); - $this->check(20)->isValid()->shouldBe(true); - $this->check(-144)->isValid()->shouldBe(true); + $this->check('', 0)->isValid()->shouldBe(true); + $this->check('', 1)->isValid()->shouldBe(true); + $this->check('', 20)->isValid()->shouldBe(true); + $this->check('', -144)->isValid()->shouldBe(true); } function it_should_return_valid_for_floats() { - $this->check(0.0)->isValid()->shouldBe(true); - $this->check(1.1235)->isValid()->shouldBe(true); - $this->check(-0.00001)->isValid()->shouldBe(true); - $this->check(-144.12313131313)->isValid()->shouldBe(true); + $this->check('', 0.0)->isValid()->shouldBe(true); + $this->check('', 1.1235)->isValid()->shouldBe(true); + $this->check('', -0.00001)->isValid()->shouldBe(true); + $this->check('', -144.12313131313)->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { - $this->check(null)->isValid()->shouldBe(false); - $this->check("foo")->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); - $this->check(false)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', "foo")->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/OptionalTypeSpec.php b/spec/Type/OptionalTypeSpec.php index 5e3f6a8..61f4a38 100644 --- a/spec/Type/OptionalTypeSpec.php +++ b/spec/Type/OptionalTypeSpec.php @@ -17,7 +17,7 @@ function it_is_initializable(TypeInterface $childType) function it_should_return_the_value_from_the_child(TypeInterface $childType) { $this->beConstructedWith($childType); - $childType->check(false)->willReturn(new Result(false, [])); - $this->check(false)->isValid()->shouldBe(false); + $childType->check('', false)->willReturn(new Result(false, [])); + $this->check('', false)->isValid()->shouldBe(false); } } diff --git a/spec/Type/RegexTypeSpec.php b/spec/Type/RegexTypeSpec.php index e2ad031..904ee25 100644 --- a/spec/Type/RegexTypeSpec.php +++ b/spec/Type/RegexTypeSpec.php @@ -19,24 +19,24 @@ function it_should_return_valid_for_matching_strings() { $this->beConstructedWith('/^def/'); - $this->check('definitive')->isValid()->shouldBe(true); + $this->check('', 'definitive')->isValid()->shouldBe(true); } function it_should_return_invalid_for_not_matching_strings() { $this->beConstructedWith('/^def/'); - $this->check('developers')->isValid()->shouldBe(false); + $this->check('', 'developers')->isValid()->shouldBe(false); } function it_should_return_invalid_for_others() { $this->beConstructedWith('/^def/'); - $this->check(null)->isValid()->shouldBe(false); - $this->check(12.3)->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(-1)->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', 12.3)->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', -1)->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); } } diff --git a/spec/Type/StringTypeSpec.php b/spec/Type/StringTypeSpec.php index 5bb35d6..f514c5f 100644 --- a/spec/Type/StringTypeSpec.php +++ b/spec/Type/StringTypeSpec.php @@ -15,17 +15,17 @@ function it_is_initializable() function it_should_return_valid_for_strings() { - $this->check("")->isValid()->shouldBe(true); - $this->check("fooo")->isValid()->shouldBe(true); - $this->check('adadsad asd a')->isValid()->shouldBe(true); + $this->check('', '')->isValid()->shouldBe(true); + $this->check('', 'fooo')->isValid()->shouldBe(true); + $this->check('', 'adadsad asd a')->isValid()->shouldBe(true); } function it_should_return_invalid_for_others() { - $this->check(null)->isValid()->shouldBe(false); - $this->check(12.3)->isValid()->shouldBe(false); - $this->check([])->isValid()->shouldBe(false); - $this->check(-1)->isValid()->shouldBe(false); - $this->check(true)->isValid()->shouldBe(false); + $this->check('', null)->isValid()->shouldBe(false); + $this->check('', 12.3)->isValid()->shouldBe(false); + $this->check('', [])->isValid()->shouldBe(false); + $this->check('', -1)->isValid()->shouldBe(false); + $this->check('', true)->isValid()->shouldBe(false); } } diff --git a/src/Check/CountCheck.php b/src/Check/CountCheck.php index 72d283e..6f09a2d 100644 --- a/src/Check/CountCheck.php +++ b/src/Check/CountCheck.php @@ -3,13 +3,11 @@ namespace Cubicl\StructureCheck\Check; use Countable; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; use Cubicl\StructureCheck\Type\TypeInterface; -/** - * Class CountCheck - * @package Cubicl\StructureCheck\Check - */ class CountCheck implements TypeInterface { /** @@ -32,43 +30,32 @@ class CountCheck implements TypeInterface */ private $count; - /** - * CountCheck constructor. - * - * @param TypeInterface $child - * @param int $count - */ - public function __construct(TypeInterface $child, $count) + public function __construct(TypeInterface $child, int $count) { $this->child = $child; $this->count = $count; } - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { - $result = $this->child->check($value); + $result = $this->child->check($key, $value); if (!$result->isValid()) { return $result; } if (!$value instanceof Countable) { - return new Result( - false, - [sprintf(self::$countableErrorMessage, json_encode($value))] - ); + return Result::invalid([ + new Error($key, sprintf(self::$countableErrorMessage, json_encode($value))) + ]); } if (count($value) !== $this->count) { - return new Result( - false, - [sprintf(self::$countErrorMessage, json_encode($value), $this->count)] - ); + return Result::invalid([ + new Error($key, sprintf(self::$countErrorMessage, json_encode($value), $this->count)) + ]); } - return new Result(true); + return Result::valid(); } } diff --git a/src/Check/NumericRangeCheck.php b/src/Check/NumericRangeCheck.php index 414c2d9..a23a718 100644 --- a/src/Check/NumericRangeCheck.php +++ b/src/Check/NumericRangeCheck.php @@ -2,13 +2,11 @@ namespace Cubicl\StructureCheck\Check; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; use Cubicl\StructureCheck\Type\TypeInterface; -/** - * Class NumericRangeCheck - * @package Cubicl\StructureCheck\Check - */ class NumericRangeCheck implements TypeInterface { /** @@ -36,43 +34,33 @@ class NumericRangeCheck implements TypeInterface */ private $upperBound; - /** - * @param TypeInterface $child - * @param int $upperBound - * @param int $lowerBound - */ - public function __construct(TypeInterface $child, $upperBound, $lowerBound) + public function __construct(TypeInterface $child, int $upperBound, int $lowerBound) { $this->child = $child; $this->upperBound = $upperBound; $this->lowerBound = $lowerBound; } - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { - $result = $this->child->check($value); + $result = $this->child->check($key, $value); if (!$result->isValid()) { return $result; } if ($this->lowerBound > $value) { - return new Result( - false, - [sprintf(self::$lowerBoundErrorMessage, $this->lowerBound, $value)] - ); + return Result::invalid([ + new Error($key, sprintf(self::$lowerBoundErrorMessage, $this->lowerBound, $value)) + ]); } if ($this->upperBound < $value) { - return new Result( - false, - [sprintf(self::$upperBoundErrorMessage, $this->upperBound, $value)] - ); + return Result::invalid([ + new Error($key, sprintf(self::$upperBoundErrorMessage, $this->lowerBound, $value)) + ]); } - return new Result(true); + return Result::valid(); } } diff --git a/src/Checker.php b/src/Checker.php index b7097a0..861a490 100644 --- a/src/Checker.php +++ b/src/Checker.php @@ -4,19 +4,13 @@ use Cubicl\StructureCheck\Type\TypeInterface; -/** - * Class Checker - * @package Cubicl\StructureCheck - */ class Checker implements CheckerInterface { - /** * @inheritdoc */ - public function fulfills($element, TypeInterface $requirement) + public function fulfills($element, TypeInterface $requirement): ResultInterface { - return $requirement->check($element); + return $requirement->check('', $element); } - -} \ No newline at end of file +} diff --git a/src/CheckerInterface.php b/src/CheckerInterface.php index af764bc..1cc8ef3 100644 --- a/src/CheckerInterface.php +++ b/src/CheckerInterface.php @@ -4,18 +4,13 @@ use Cubicl\StructureCheck\Type\TypeInterface; -/** - * Interface CheckerInterface - * @package Cubicl\StructureCheck - */ interface CheckerInterface { - /** * @param mixed $element the element which should be tested * @param TypeInterface $requirement * * @return ResultInterface */ - public function fulfills($element, TypeInterface $requirement); -} \ No newline at end of file + public function fulfills($element, TypeInterface $requirement): ResultInterface; +} diff --git a/src/Error.php b/src/Error.php new file mode 100644 index 0000000..d21a104 --- /dev/null +++ b/src/Error.php @@ -0,0 +1,26 @@ +key = $key; + $this->message = $message; + } + + public function getKey(): string + { + return $this->key; + } + + public function getMessage(): string + { + return $this->message; + } +} diff --git a/src/ErrorInterface.php b/src/ErrorInterface.php new file mode 100644 index 0000000..ffd6246 --- /dev/null +++ b/src/ErrorInterface.php @@ -0,0 +1,10 @@ +valid = $valid; $this->errors = $errors; } + public static function valid(): ResultInterface + { + return new self(true, []); + } + + /** + * @param ErrorInterface[] $errors + * + * @return ResultInterface + */ + public static function invalid(array $errors): ResultInterface + { + return new self(false, $errors); + } + /** * @inheritdoc */ - public function isValid() + public function isValid(): bool { return $this->valid; } @@ -42,8 +53,8 @@ public function isValid() /** * @inheritdoc */ - public function getErrors() + public function getErrors(): array { return $this->errors; } -} \ No newline at end of file +} diff --git a/src/ResultInterface.php b/src/ResultInterface.php index dda5ac6..7bb0184 100644 --- a/src/ResultInterface.php +++ b/src/ResultInterface.php @@ -10,15 +10,13 @@ interface ResultInterface { /** * Returns TRUE if the check was successful. - * - * @return bool */ - public function isValid(); + public function isValid(): bool; /** * Returns a list of errors. If no error occurred, it will return an empty array. * - * @return array + * @return ErrorInterface[] */ - public function getErrors(); -} \ No newline at end of file + public function getErrors(): array; +} diff --git a/src/Type/AnyType.php b/src/Type/AnyType.php index 11a441d..dc2b925 100644 --- a/src/Type/AnyType.php +++ b/src/Type/AnyType.php @@ -3,6 +3,7 @@ namespace Cubicl\StructureCheck\Type; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; /** * Class AnyType @@ -13,8 +14,8 @@ class AnyType implements TypeInterface /** * @inheritdoc */ - public function check($value) + public function check(string $key, $value): ResultInterface { - return new Result(true); + return Result::valid(); } -} \ No newline at end of file +} diff --git a/src/Type/BoolType.php b/src/Type/BoolType.php index 1cd5003..10bede9 100644 --- a/src/Type/BoolType.php +++ b/src/Type/BoolType.php @@ -2,29 +2,20 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class BoolType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class BoolType implements TypeInterface { private static $errorMessage = 'The value %s is not a boolean.'; - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_bool($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } -} \ No newline at end of file +} diff --git a/src/Type/DatetimeType.php b/src/Type/DatetimeType.php index 9e24770..f4e212b 100644 --- a/src/Type/DatetimeType.php +++ b/src/Type/DatetimeType.php @@ -2,15 +2,12 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use DateTime; use DateTimeZone; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class DatetimeType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class DatetimeType implements TypeInterface { /** @@ -28,42 +25,26 @@ class DatetimeType implements TypeInterface */ private $datetimeZone; - /** - * DatetimeType constructor. - * - * @param string $format - * @param string $datetimeZone - */ - public function __construct($format, $datetimeZone) + public function __construct(string $format, string $datetimeZone) { $this->datetimeFormat = $format; $this->datetimeZone = $datetimeZone; } - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_string($value) && $this->isValidDatetime($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } - /** - * @param string $value - * - * @return bool - */ - private function isValidDatetime($value) { + private function isValidDatetime(string $value): bool + { $date = DateTime::createFromFormat($this->datetimeFormat, $value, new DateTimeZone($this->datetimeZone)); - $errors = DateTime::getLastErrors()["warning_count"]; + $errors = DateTime::getLastErrors(); - return $date && $errors["warning_count"] == 0 && $errors["error_count"] == 0; + return $date && $errors['warning_count'] === 0 && $errors['error_count'] === 0; } } diff --git a/src/Type/EnumType.php b/src/Type/EnumType.php index fa06419..f63f96d 100644 --- a/src/Type/EnumType.php +++ b/src/Type/EnumType.php @@ -2,20 +2,21 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; class EnumType implements TypeInterface { /** - * @var array + * @var string */ - private $values = []; + private static $errorMessage = 'The value %s is not in the allowed values (%s).'; /** - * @var string + * @var array */ - private static $errorMessage = 'The value %s is not in the allowed values (%s).'; + private $values; /** * EnumType constructor. @@ -27,18 +28,13 @@ public function __construct(array $values) $this->values = $values; } - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = in_array($value, $this->values, true); - - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value), implode(',', $this->values))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([ + new Error($key, sprintf(self::$errorMessage, json_encode($value), implode(',', $this->values))) + ]); } } diff --git a/src/Type/ExactValueType.php b/src/Type/ExactValueType.php index e1323c1..91c908d 100644 --- a/src/Type/ExactValueType.php +++ b/src/Type/ExactValueType.php @@ -2,12 +2,12 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; class ExactValueType implements TypeInterface { - /** * @var string */ @@ -26,18 +26,14 @@ public function __construct($value) $this->value = $value; } - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = $this->value === $value; - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value), $this->value)] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([ + new Error($key, sprintf(self::$errorMessage, json_encode($value), json_encode($this->value))) + ]); } } diff --git a/src/Type/FloatType.php b/src/Type/FloatType.php index 49889fc..87c7eb0 100644 --- a/src/Type/FloatType.php +++ b/src/Type/FloatType.php @@ -2,22 +2,20 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; class FloatType implements TypeInterface { private static $errorMessage = 'The value %s is not a float.'; - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_float($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } -} \ No newline at end of file +} diff --git a/src/Type/IntType.php b/src/Type/IntType.php index 2aaa8b4..7e4e036 100644 --- a/src/Type/IntType.php +++ b/src/Type/IntType.php @@ -2,29 +2,20 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class IntType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class IntType implements TypeInterface { private static $errorMessage = 'The value %s is not an integer.'; - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_int($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } -} \ No newline at end of file +} diff --git a/src/Type/ListType.php b/src/Type/ListType.php index 6d92ba7..0c5af64 100644 --- a/src/Type/ListType.php +++ b/src/Type/ListType.php @@ -2,12 +2,10 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; -/** - * Class ListType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class ListType implements TypeInterface { private static $isNotAnArrayMessage = 'The given value %s is not an array.'; @@ -17,38 +15,32 @@ class ListType implements TypeInterface */ private $child; - /** - * ListType constructor. - * - * @param TypeInterface $child - */ public function __construct(TypeInterface $child) { $this->child = $child; } - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { if (!is_array($value)) { - return new Result( - false, - [sprintf(self::$isNotAnArrayMessage, json_encode($value))] + return Result::invalid( + [new Error($key, sprintf(self::$isNotAnArrayMessage, json_encode($value)))] ); } $errors = []; $valid = true; - foreach ($value as $item) { - $result = $this->child->check($item); + foreach ($value as $idx => $item) { + $result = $this->child->check(sprintf('%s.%d', $key, $idx), $item); $valid = $valid && $result->isValid(); $errors += $result->getErrors(); } - return new Result($valid, $errors); + return $valid + ? Result::valid() + : Result::invalid($errors); + } } diff --git a/src/Type/NullableType.php b/src/Type/NullableType.php index 6f4d06f..c66f5a4 100644 --- a/src/Type/NullableType.php +++ b/src/Type/NullableType.php @@ -3,38 +3,26 @@ namespace Cubicl\StructureCheck\Type; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; -/** - * Class NullableType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class NullableType implements TypeInterface { - /** * @var TypeInterface */ private $child; - /** - * NullableType constructor. - * - * @param TypeInterface $child - */ public function __construct(TypeInterface $child) { $this->child = $child; } - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { - if(is_null($value)) { - return new Result(true); + if($value === null) { + return Result::valid(); } - return $this->child->check($value); + return $this->child->check($key, $value); } -} \ No newline at end of file +} diff --git a/src/Type/NumericType.php b/src/Type/NumericType.php index 68fa1c1..796f092 100644 --- a/src/Type/NumericType.php +++ b/src/Type/NumericType.php @@ -2,6 +2,7 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; @@ -9,18 +10,12 @@ class NumericType implements TypeInterface { private static $errorMessage = 'The value %s is not a numeric value.'; - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_numeric($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } } diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index da369b4..49a9fb6 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -2,7 +2,9 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; +use Cubicl\StructureCheck\ResultInterface; class ObjectType implements TypeInterface { @@ -23,25 +25,23 @@ public function __construct(array $children) $this->children = $children; } - /** - * @inheritdoc - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $errors = []; $valid = true; - foreach ($this->children as $key => $child) { - if (!array_key_exists($key, $value)) { + foreach ($this->children as $objectProperty => $child) { + $fullKey = sprintf('%s.%s', $key, $objectProperty); + if (!array_key_exists($objectProperty, $value)) { if (!$child instanceof OptionalType) { $valid = false; - $errors[] = sprintf(self::$missingKeyErrorMessage, $key); + $errors[] = new Error($fullKey, sprintf(self::$missingKeyErrorMessage, $objectProperty)); } continue; } - $result = $child->check($value[$key]); + $result = $child->check($fullKey, $value[$objectProperty]); $valid = $valid && $result->isValid(); $errors += $result->getErrors(); } diff --git a/src/Type/OptionalType.php b/src/Type/OptionalType.php index b300150..7b68ba4 100644 --- a/src/Type/OptionalType.php +++ b/src/Type/OptionalType.php @@ -4,34 +4,17 @@ use Cubicl\StructureCheck\ResultInterface; -/** - * Class OptionalType - * @package Cubicl\Cubicl\StructureCheck\Type - * @author Christian Blank - */ class OptionalType implements TypeInterface { - /** - * @var TypeInterface - */ private $child; - /** - * OptionalType constructor. - * @param TypeInterface $child - */ public function __construct(TypeInterface $child) { $this->child = $child; } - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { - return $this->child->check($value); + return $this->child->check($key, $value); } -} \ No newline at end of file +} diff --git a/src/Type/RegexType.php b/src/Type/RegexType.php index 6fb2888..84d5256 100644 --- a/src/Type/RegexType.php +++ b/src/Type/RegexType.php @@ -2,13 +2,10 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class RegexType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class RegexType implements TypeInterface { /** @@ -21,28 +18,19 @@ class RegexType implements TypeInterface */ private $regex; - /** - * RegexType constructor. - * - * @param string $regex - */ - public function __construct($regex) + public function __construct(string $regex) { $this->regex = $regex; } - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_string($value) && preg_match($this->regex, $value) === 1; - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value), $this->regex)] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([ + new Error($key, sprintf(self::$errorMessage, json_encode($value), json_encode($this->regex))) + ]); } } diff --git a/src/Type/StringType.php b/src/Type/StringType.php index 9c69f2a..4e2441d 100644 --- a/src/Type/StringType.php +++ b/src/Type/StringType.php @@ -2,29 +2,20 @@ namespace Cubicl\StructureCheck\Type; +use Cubicl\StructureCheck\Error; use Cubicl\StructureCheck\Result; use Cubicl\StructureCheck\ResultInterface; -/** - * Class StringType - * @package Cubicl\Cubicl\StructureCheck\Type - */ class StringType implements TypeInterface { private static $errorMessage = 'The value %s is not a string.'; - /** - * @param mixed $value - * - * @return ResultInterface - */ - public function check($value) + public function check(string $key, $value): ResultInterface { $checkResult = is_string($value); - return new Result( - $checkResult, - !$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : [] - ); + return $checkResult + ? Result::valid() + : Result::invalid([new Error($key, sprintf(self::$errorMessage, json_encode($value)))]); } -} \ No newline at end of file +} diff --git a/src/Type/TypeInterface.php b/src/Type/TypeInterface.php index 4ee25e5..450989c 100644 --- a/src/Type/TypeInterface.php +++ b/src/Type/TypeInterface.php @@ -11,9 +11,10 @@ interface TypeInterface { /** - * @param mixed $value + * @param string $key + * @param mixed $value * * @return ResultInterface */ - public function check($value); -} \ No newline at end of file + public function check(string $key, $value): ResultInterface; +} diff --git a/tests/Integration/Type/ObjectTypeTest.php b/tests/Integration/Type/ObjectTypeTest.php index 91a9592..0750194 100644 --- a/tests/Integration/Type/ObjectTypeTest.php +++ b/tests/Integration/Type/ObjectTypeTest.php @@ -24,7 +24,7 @@ class ObjectTypeTest extends TestCase /** * */ - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->checker = new Checker(); @@ -33,14 +33,14 @@ protected function setUp() /** * @test */ - public function itShouldHandleAbsenceOfOptionalDeclaredType() + public function itShouldHandleAbsenceOfOptionalDeclaredType(): void { $structure = new ObjectType([ 'opt' => new OptionalType(new AnyType()) ]); - $actual = $structure->check([]); + $actual = $structure->check('', []); $this->assertSame(true, $actual->isValid()); } -} \ No newline at end of file +}