Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 51a18f5

Browse files
authored
Merge pull request #47 from vaclavvanik/feature/bool-strategy-hydrate-bool
BooleanStrategy hydrate accepts bool value
2 parents 50c80e6 + fe62924 commit 51a18f5

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Strategy/BooleanStrategy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ public function extract($value)
7373
/**
7474
* Converts the given value so that it can be hydrated by the hydrator.
7575
*
76-
* @param int|string $value The original value.
76+
* @param bool|int|string $value The original value.
7777
* @throws InvalidArgumentException
7878
* @return bool Returns the value that should be hydrated.
7979
*/
8080
public function hydrate($value)
8181
{
82+
if (is_bool($value)) {
83+
return $value;
84+
}
85+
8286
if (!is_string($value) && !is_int($value)) {
8387
throw new InvalidArgumentException(sprintf(
84-
'Unable to hydrate. Expected string or int. %s was given.',
88+
'Unable to hydrate. Expected bool, string or int. %s was given.',
8589
is_object($value) ? get_class($value) : gettype($value)
8690
));
8791
}

test/Strategy/BooleanStrategyTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public function testHydrateInteger()
8686
$this->assertEquals(false, $hydrator->hydrate(0));
8787
}
8888

89+
public function testHydrateBool()
90+
{
91+
$hydrator = new BooleanStrategy(1, 0);
92+
$this->assertEquals(true, $hydrator->hydrate(true));
93+
$this->assertEquals(false, $hydrator->hydrate(false));
94+
}
95+
8996
public function testHydrateUnexpectedValueThrowsException()
9097
{
9198
$this->setExpectedException('Zend\Hydrator\Exception\InvalidArgumentException', 'Unexpected value');

0 commit comments

Comments
 (0)