diff --git a/spec/Check/NumericRangeCheckSpec.php b/spec/Check/NumericRangeCheckSpec.php new file mode 100644 index 0000000..b0fe05c --- /dev/null +++ b/spec/Check/NumericRangeCheckSpec.php @@ -0,0 +1,47 @@ +beConstructedWith($child, 0, 1); + + $this->shouldHaveType(NumericRangeCheck::class); + } + + function it_implements_type_interface(TypeInterface $child) + { + $this->beConstructedWith($child, 0, 1); + + $this->shouldImplement(TypeInterface::class); + } + + function it_returns_a_result_on_invalid_child_check(TypeInterface $child, ResultInterface $result) + { + $result->isValid()->willReturn(false); + $child->check(Argument::any())->willReturn($result); + + $this->beConstructedWith($child, 0, 1); + + $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); + + $this->beConstructedWith($child, 0, 1); + + $this->check(0)->shouldHaveType(ResultInterface::class); + } +} diff --git a/src/Check/NumericRangeCheck.php b/src/Check/NumericRangeCheck.php new file mode 100644 index 0000000..b14c5c2 --- /dev/null +++ b/src/Check/NumericRangeCheck.php @@ -0,0 +1,78 @@ +child = $child; + $this->upperBound = $upperBound; + $this->lowerBound = $lowerBound; + } + + /** + * @inheritdoc + */ + public function check($value) + { + $result = $this->child->check($value); + + if (!$result->isValid()) { + return $result; + } + + if ($this->lowerBound > $value) { + return new Result( + false, + [sprintf(self::$lowerBoundErrorMessage, $this->lowerBound, $value)] + ); + } + + if ($this->upperBound < $value) { + return new Result( + false, + [sprintf(self::$upperBoundErrorMessage, $this->upperBound, $value)] + ); + } + + return new Result(true); + } +}