Skip to content

Commit

Permalink
isEmpty could not be [] for StringRule #10
Browse files Browse the repository at this point in the history
  • Loading branch information
elie29 committed Feb 28, 2019
1 parent 1e0d1b3 commit 6fa3568
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#10](https://github.com/elie29/validator/issues/10) isEmpty could not be [] for StringRule.

### Deprecated

Expand Down
7 changes: 3 additions & 4 deletions src/Rule/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected function setValue($value): void
if (is_string($value) && $this->trim) {
$value = trim($value);
}

$this->value = $value;
}

Expand All @@ -130,15 +131,13 @@ protected function isRequired(): bool
}

/**
* If value is equal to 0 or false, we consider that value is not empty.
* Empty value is null or '' only.
*
* @return bool
*/
protected function isEmpty(): bool
{
return $this->value === null ||
$this->value === [] ||
$this->value === '';
return $this->value === null || $this->value === '';
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Rule/ArrayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* This class verifies that a value is a valid array.
* empty value could be null or []
*/
class ArrayRule extends AbstractRule
{
Expand Down Expand Up @@ -100,4 +101,14 @@ protected function checkMinMax(): int

return $this::VALID;
}

/**
* Empty value is null or [] only.
*
* @return bool
*/
protected function isEmpty(): bool
{
return $this->value === null || $this->value === [];
}
}
10 changes: 10 additions & 0 deletions src/Rule/BooleanRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ protected function isBool(): bool
$val === '0' || $val === '1' ||
$val === true || $val === false;
}

/**
* Empty value is null only.
*
* @return bool
*/
protected function isEmpty(): bool
{
return $this->value === null;
}
}
12 changes: 0 additions & 12 deletions tests/Rule/AbstractRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,5 @@ public function getValueProvider(): \Generator
yield 'Required value should not be null' => [
null, [RuleInterface::REQUIRED => true], 'key is required and should not be empty: ', RuleInterface::ERROR
];

yield 'Required value should not be an empty array' => [
[], [RuleInterface::REQUIRED => true], 'key is required and should not be empty: array ()',
RuleInterface::ERROR
];

yield 'Required value should not be an empty array, with specific message' => [
[], [RuleInterface::REQUIRED => true, 'messages' => [
RuleInterface::EMPTY_KEY => '%key% is required. `%value%` is empty!'
]],
'key is required. `array ()` is empty!', RuleInterface::ERROR
];
}
}
18 changes: 16 additions & 2 deletions tests/Rule/ArrayRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ArrayRuleTest extends TestCase
public function testValidateEmptyValue(): void
{
// Empty string value. Value is not required by default!
$rule = new ArrayRule('name', '');
$rule = new ArrayRule('name', []);
$res = $rule->validate();
assertThat($res, identicalTo(ArrayRule::VALID));
assertThat($rule->getValue(), emptyArray());
Expand Down Expand Up @@ -46,7 +46,7 @@ public function testValidate($value, $params, $expectedResult, $expectedError):
public function getArrayValueProvider(): \Generator
{
yield 'Given value could be empty' => [
'', [], ArrayRule::VALID, ''
[], [], ArrayRule::VALID, ''
];

yield 'Given value between 4 and 8' => [
Expand All @@ -65,5 +65,19 @@ public function getArrayValueProvider(): \Generator
['Peter', 'Ben', 'Harold'], [ArrayRule::MIN => 4, ArrayRule::MAX => 8], ArrayRule::ERROR,
"name: The length of array ( 0 => 'Peter', 1 => 'Ben', 2 => 'Harold',) is not between 4 and 8"
];

yield 'Required value should not be an empty array' => [
[], [RuleInterface::REQUIRED => true],
RuleInterface::ERROR,
'name is required and should not be empty: array ()'
];

yield 'Required value should not be an empty array, with specific message' => [
[], [RuleInterface::REQUIRED => true, 'messages' => [
RuleInterface::EMPTY_KEY => '%key% is required. `%value%` is empty!'
]],
RuleInterface::ERROR,
'name is required. `array ()` is empty!'
];
}
}
12 changes: 6 additions & 6 deletions tests/Rule/BooleanRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ class BooleanRuleTest extends TestCase

public function testValidateEmptyValue(): void
{
// Empty string value. Value is not required by default!
$rule = new BooleanRule('name', '');
// Value is not required by default!
$rule = new BooleanRule('name', null);

$res = $rule->validate();

assertThat($res, identicalTo(BooleanRule::VALID));
assertThat($rule->getValue(), emptyString());
assertThat($rule->getValue(), nullValue());

$rule = new BooleanRule('name', '', [
$rule = new BooleanRule('name', null, [
BooleanRule::CAST => true
]);

$res = $rule->validate();

assertThat($res, identicalTo(BooleanRule::VALID));
assertThat($rule->getValue(), identicalTo(false));
assertThat($rule->getValue(), is(false));
}

/**
Expand All @@ -46,7 +46,7 @@ public function testValidate($value, $expectedResult, $expectedError): void
public function getBooleanValueProvider(): \Generator
{
yield 'Given value could be empty' => [
'', BooleanRule::VALID, ''
null, BooleanRule::VALID, ''
];

yield 'Given value could be 1' => [
Expand Down

0 comments on commit 6fa3568

Please sign in to comment.