diff --git a/src/IntegerValidator.php b/src/IntegerValidator.php index 20cd52e..3cec5e9 100644 --- a/src/IntegerValidator.php +++ b/src/IntegerValidator.php @@ -50,8 +50,8 @@ class IntegerValidator extends \Phramework\Validate\NumberValidator public function __construct( ?int $minimum = null, ?int $maximum = null, - bool $exclusiveMinimum = null, - bool $exclusiveMaximum = null, + ?bool $exclusiveMinimum = false, + ?bool $exclusiveMaximum = false, int $multipleOf = 1 ) { if ($multipleOf !== null && $multipleOf <= 0) { diff --git a/src/NumberValidator.php b/src/NumberValidator.php index 1a8fde0..d25703d 100644 --- a/src/NumberValidator.php +++ b/src/NumberValidator.php @@ -53,13 +53,15 @@ class NumberValidator extends \Phramework\Validate\BaseValidator protected static $type = 'number'; /** + * @param bool|null $exclusiveMinimum When null will behave as false + * @param bool|null $exclusiveMaximum When null will behave as false * @throws \DomainException When minimum is not less than the maximum */ public function __construct( ?float $minimum = null, ?float $maximum = null, - ?bool $exclusiveMinimum = null, - ?bool $exclusiveMaximum = null, + ?bool $exclusiveMinimum = false, + ?bool $exclusiveMaximum = false, ?float $multipleOf = null ) { parent::__construct(); @@ -72,6 +74,14 @@ public function __construct( throw new \InvalidArgumentException('multipleOf must be a positive number'); } + if ($exclusiveMinimum && $minimum === null) { + throw new \DomainException('If "exclusiveMinimum" is set to true, "minimum" MUST also be set'); + } + + if ($exclusiveMaximum && $maximum === null) { + throw new \DomainException('If "exclusiveMaximum" is set to true, "maximum" MUST also be set'); + } + $this->minimum = $minimum; $this->maximum = $maximum; $this->exclusiveMinimum = $exclusiveMinimum; diff --git a/src/UnsignedIntegerValidator.php b/src/UnsignedIntegerValidator.php index 296475b..49fab32 100644 --- a/src/UnsignedIntegerValidator.php +++ b/src/UnsignedIntegerValidator.php @@ -16,9 +16,6 @@ */ namespace Phramework\Validate; -use Phramework\Validate\Result\Result; -use \Phramework\Exceptions\IncorrectParametersException; - /** * UnsignedInteger validator * @uses \Phramework\Validate\Integer As base implementation's rules to @@ -49,8 +46,8 @@ class UnsignedIntegerValidator extends \Phramework\Validate\IntegerValidator public function __construct( int $minimum = 0, ?int $maximum = null, - bool $exclusiveMinimum = null, - bool $exclusiveMaximum = null, + ?bool $exclusiveMinimum = false, + ?bool $exclusiveMaximum = false, ?int $multipleOf = 1 ) { if ($minimum < 0) { diff --git a/tests/src/NumberValidatorTest.php b/tests/src/NumberValidatorTest.php index 064e72e..cb95118 100644 --- a/tests/src/NumberValidatorTest.php +++ b/tests/src/NumberValidatorTest.php @@ -9,7 +9,6 @@ */ class NumberValidatorTest extends TestCase { - /** * @var NumberValidator */ @@ -69,7 +68,7 @@ public function testConstructFailure6() } /** - * @dataProvider validateSuccessProvider + * @dataProvider validateSuccessProvider */ public function testCreateFromJSON($input, $expected) { @@ -82,6 +81,7 @@ public function testCreateFromJSON($input, $expected) "x-extra": "not existing" }'; + /** @var NumberValidator $validatorObject */ $validatorObject = NumberValidator::createFromJSON($json); $this->assertSame( @@ -90,6 +90,14 @@ public function testCreateFromJSON($input, $expected) 'Title must be passed' ); + $this->assertFalse( + $validatorObject->exclusiveMaximum + ); + + $this->assertFalse( + $validatorObject->exclusiveMinimum + ); + $this->assertSame( 10, $validatorObject->default, @@ -196,4 +204,30 @@ public function testShouldFailWhenMultipleOfIsNotPositiveNumber() -1 ); } + + /** + * @expectedException \DomainException + */ + public function testAllowExclusiveMaximumOnlyIfMaximumIsSet() + { + new NumberValidator( + null, + null, + null, + true + ); + } + + /** + * @expectedException \DomainException + */ + public function testAllowExclusiveMinimumOnlyIfMinimumIsSet() + { + new NumberValidator( + null, + null, + true, + false + ); + } }