Skip to content

Commit

Permalink
Merge pull request #42 from berry-langerak/feature/nuking-exclusive-o…
Browse files Browse the repository at this point in the history
…n-lengthbetween

Nuking exclusive, as it's unneeded functionality.
  • Loading branch information
berry-langerak committed May 25, 2015
2 parents bf034d5 + 6ee431f commit 2e1b9de
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 54 deletions.
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]
];
}
}

0 comments on commit 2e1b9de

Please sign in to comment.