Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/IntegerValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions src/NumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions src/UnsignedIntegerValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
38 changes: 36 additions & 2 deletions tests/src/NumberValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class NumberValidatorTest extends TestCase
{

/**
* @var NumberValidator
*/
Expand Down Expand Up @@ -69,7 +68,7 @@ public function testConstructFailure6()
}

/**
* @dataProvider validateSuccessProvider
* @dataProvider validateSuccessProvider
*/
public function testCreateFromJSON($input, $expected)
{
Expand All @@ -82,6 +81,7 @@ public function testCreateFromJSON($input, $expected)
"x-extra": "not existing"
}';

/** @var NumberValidator $validatorObject */
$validatorObject = NumberValidator::createFromJSON($json);

$this->assertSame(
Expand All @@ -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,
Expand Down Expand Up @@ -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
);
}
}