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

Commit 6866f95

Browse files
author
Julien Teindas
committed
fix(Parameter): allow required boolean with value false
1 parent 020d03b commit 6866f95

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Parameter.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function setDateFormat($dateFormat)
257257
public function assertValue($value)
258258
{
259259
// required?
260-
if (empty($value)) {
260+
if (!$this->hasValue($value)) {
261261
if ($this->getRequired()) {
262262
$this->throwInvalidParameter($this->getName().' is required');
263263
}
@@ -361,4 +361,20 @@ protected function throwInvalidParameter($message)
361361
new Error($message, 'parameter-invalid'),
362362
]);
363363
}
364+
365+
/**
366+
* @param mixed $value
367+
*/
368+
private function hasValue($value): bool
369+
{
370+
return !empty($value) || $this->isBooleanWithValueFalse($value);
371+
}
372+
373+
/**
374+
* @param mixed $value
375+
*/
376+
private function isBooleanWithValueFalse($value): bool
377+
{
378+
return $this->getType() === self::TYPE_BOOLEAN && is_bool($value) && $value === false;
379+
}
364380
}

tests/units/Parameter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,14 @@ function () {
250250
->isInstanceOf('\RuntimeException')
251251
;
252252
}
253+
254+
public function testAssertValueOnMandatoryFalseBoolean()
255+
{
256+
$this->newTestedInstance('name', 'boolean', true);
257+
$this
258+
->given($this->testedInstance)
259+
->variable($this->testedInstance->assertValue(false))
260+
->isNull
261+
;
262+
}
253263
}

0 commit comments

Comments
 (0)