Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuking exclusive, as it's unneeded functionality. #42

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Validate the value to consist only out of alphanumeric characters.

Validate that the value only consists our of alphabetic characters.

### between($min, $max, $inclusive = true)
### between($min, $max)

Validate that the value is between $min and $max (inclusive by default).
Validate that the value is between $min and $max (inclusive).

### callback(callable $callable)

Expand Down Expand Up @@ -48,10 +48,10 @@ Validates that the value is in the array with optional "loose" checking.

Validate the value to be of precisely length $length.

### lengthBetween($min, $max, $inclusive = true)
### lengthBetween($min, $max)

Validates that the length of the value is between $min and $max.
If $max is null, it has no upper limit. The default is inclusive.
Validates that the length of the value is between $min and $max (inclusive).
If $max is null, it has no upper limit.

### regex($regex)

Expand Down
5 changes: 2 additions & 3 deletions src/Particle/Validator/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,11 @@ public function length($length)
*
* @param int $min
* @param int|null $max
* @param bool $inclusive
* @return $this
*/
public function lengthBetween($min, $max, $inclusive = true)
public function lengthBetween($min, $max)
{
return $this->addRule(new Rule\LengthBetween($min, $max, $inclusive));
return $this->addRule(new Rule\LengthBetween($min, $max));
}

/**
Expand Down
32 changes: 29 additions & 3 deletions src/Particle/Validator/Rule/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,40 @@ public function __construct($min, $max)
* @return bool
*/
public function validate($value)
{
return !$this->tooSmall($value, self::TOO_SMALL) && !$this->tooLarge($value, self::TOO_BIG);
}

/**
* Returns whether or not the value is too small, and logs an error if it is.
*
* @param mixed $value
* @param string $error
* @return bool
*/
protected function tooSmall($value, $error)
{
if ($value < $this->min) {
return $this->error(self::TOO_SMALL);
$this->error($error);
return true;
}
return false;
}

/**
* Returns whether or not the value is too large, and logs an error if it is.
*
* @param mixed $value
* @param string $error
* @return bool
*/
protected function tooLarge($value, $error)
{
if ($value > $this->max) {
return $this->error(self::TOO_BIG);
$this->error($error);
return true;
}
return true;
return false;
}

/**
Expand Down
42 changes: 18 additions & 24 deletions src/Particle/Validator/Rule/LengthBetween.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @package Particle\Validator\Rule
*/
class LengthBetween extends Rule
class LengthBetween extends Between
{
/**
* A constant that is used when the value is too long.
Expand All @@ -33,8 +33,8 @@ class LengthBetween extends Rule
* @var array
*/
protected $messageTemplates = [
self::TOO_LONG => '{{ name }} is too long and must be shorter than {{ max }} characters',
self::TOO_SHORT => '{{ name }} is too short and must be longer than {{ min }} characters'
self::TOO_LONG => '{{ name }} must be shorter than {{ max }} characters',
self::TOO_SHORT => '{{ name }} must be longer than {{ min }} characters'
];

/**
Expand All @@ -51,23 +51,14 @@ class LengthBetween extends Rule
*/
protected $min;

/**
* Whether or not the min and max length are inclusive.
*
* @var bool
*/
protected $inclusive;

/**
* @param int $min
* @param int|null $max
* @param bool $inclusive
*/
public function __construct($min, $max, $inclusive = true)
public function __construct($min, $max)
{
$this->min = $min;
$this->max = $max;
$this->inclusive = $inclusive;
}

/**
Expand All @@ -80,19 +71,22 @@ public function validate($value)
{
$length = strlen($value);

if ($this->max !== null) {
$maxLength = $this->inclusive ? $this->max : $this->max - 1;
if ($length > $maxLength) {
return $this->error(self::TOO_LONG);
}
}
return !$this->tooSmall($length, self::TOO_SHORT) && !$this->tooLarge($length, self::TOO_LONG);
}

$minLength = $this->inclusive ? $this->min : $this->min + 1;
if ($length < $minLength) {
return $this->error(self::TOO_SHORT);
/**
* Returns whether or not the value is too long, and logs an error if it is.
*
* @param mixed $value
* @param string $error
* @return bool
*/
protected function tooLarge($value, $error)
{
if ($this->max !== null) {
return parent::tooLarge($value, $error);
}

return true;
return false;
}

/**
Expand Down
38 changes: 19 additions & 19 deletions tests/Particle/Validator/Rule/LengthBetweenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,26 @@ public function testReturnsTrueIfMaxIsNull()
$this->assertEquals([], $this->validator->getMessages());
}

/**
* @dataProvider getValues
* @param $value
* @param $error
*/
public function testReturnsFalseIfLengthIsExactlyMinOrMaxAndRuleIsExclusive($value, $error)
public function testReturnsFalseIfInvalid()
{
$this->validator->required('first_name')->lengthBetween(2, 7, false);
$this->assertFalse($this->validator->validate(['first_name' => $value]));
$this->validator->required('first_name')->lengthBetween(3, 6);
$result = $this->validator->validate(['first_name' => 'ad']);

$this->assertFalse($result);

$expected = [
'first_name' => [
$error => $this->getMessage($error)
LengthBetween::TOO_SHORT => $this->getMessage(LengthBetween::TOO_SHORT)
]
];
$this->assertEquals($expected, $this->validator->getMessages());

$result = $this->validator->validate(['first_name' => 'Richard']);

$this->assertFalse($result);
$expected = [
'first_name' => [
LengthBetween::TOO_LONG => $this->getMessage(LengthBetween::TOO_LONG)
]
];
$this->assertEquals($expected, $this->validator->getMessages());
Expand All @@ -51,18 +59,10 @@ public function testReturnsFalseIfLengthIsExactlyMinOrMaxAndRuleIsExclusive($val
public function getMessage($reason)
{
$messages = [
LengthBetween::TOO_SHORT => 'first name is too short and must be longer than 2 characters',
LengthBetween::TOO_LONG => 'first name is too long and must be shorter than 7 characters',
LengthBetween::TOO_SHORT => 'first name must be longer than 3 characters',
LengthBetween::TOO_LONG => 'first name must be shorter than 6 characters',
];

return $messages[$reason];
}

public function getValues()
{
return [
['Ad', LengthBetween::TOO_SHORT],
['Richard', LengthBetween::TOO_LONG]
];
}
}