Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #37 from jutein/fix-boolean-required
Browse files Browse the repository at this point in the history
fix(Parameter): allow required boolean with value false
  • Loading branch information
yannickroger committed Nov 23, 2020
2 parents 020d03b + 6866f95 commit d0aa2ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function setDateFormat($dateFormat)
public function assertValue($value)
{
// required?
if (empty($value)) {
if (!$this->hasValue($value)) {
if ($this->getRequired()) {
$this->throwInvalidParameter($this->getName().' is required');
}
Expand Down Expand Up @@ -361,4 +361,20 @@ protected function throwInvalidParameter($message)
new Error($message, 'parameter-invalid'),
]);
}

/**
* @param mixed $value
*/
private function hasValue($value): bool
{
return !empty($value) || $this->isBooleanWithValueFalse($value);
}

/**
* @param mixed $value
*/
private function isBooleanWithValueFalse($value): bool
{
return $this->getType() === self::TYPE_BOOLEAN && is_bool($value) && $value === false;
}
}
10 changes: 10 additions & 0 deletions tests/units/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,14 @@ function () {
->isInstanceOf('\RuntimeException')
;
}

public function testAssertValueOnMandatoryFalseBoolean()
{
$this->newTestedInstance('name', 'boolean', true);
$this
->given($this->testedInstance)
->variable($this->testedInstance->assertValue(false))
->isNull
;
}
}

0 comments on commit d0aa2ef

Please sign in to comment.