Skip to content

Commit

Permalink
Allow null for Boolean and Collection strategies.
Browse files Browse the repository at this point in the history
  • Loading branch information
xorock committed Nov 8, 2020
1 parent b0edb14 commit 47fac04
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/Strategy/BooleanStrategy.php
Expand Up @@ -63,12 +63,16 @@ public function __construct($trueValue, $falseValue)
/**
* Converts the given value so that it can be extracted by the hydrator.
*
* @param bool $value The original value.
* @param bool|null $value The original value.
* @throws InvalidArgumentException
* @return int|string Returns the value that should be extracted.
*/
public function extract($value, ?object $object = null)
{
if (null === $value) {
return $value;
}

if (! is_bool($value)) {
throw new InvalidArgumentException(sprintf(
'Unable to extract. Expected bool. %s was given.',
Expand All @@ -82,12 +86,16 @@ public function extract($value, ?object $object = null)
/**
* Converts the given value so that it can be hydrated by the hydrator.
*
* @param bool|int|string $value The original value.
* @param bool|int|string|null $value The original value.
* @throws InvalidArgumentException
* @return bool Returns the value that should be hydrated.
*/
public function hydrate($value, ?array $data = null)
{
if (null === $value) {
return $value;
}

if (is_bool($value)) {
return $value;
}
Expand Down
12 changes: 10 additions & 2 deletions src/Strategy/CollectionStrategy.php
Expand Up @@ -53,12 +53,16 @@ public function __construct(HydratorInterface $objectHydrator, string $objectCla
/**
* Converts the given value so that it can be extracted by the hydrator.
*
* @param mixed[] $value The original value.
* @param mixed[]|null $value The original value.
* @throws Exception\InvalidArgumentException
* @return mixed Returns the value that should be extracted.
*/
public function extract($value, ?object $object = null)
{
if (null === $value) {
return $value;
}

if (! is_array($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Value needs to be an array, got "%s" instead.',
Expand All @@ -82,12 +86,16 @@ public function extract($value, ?object $object = null)
/**
* Converts the given value so that it can be hydrated by the hydrator.
*
* @param mixed[] $value The original value.
* @param mixed[]|null $value The original value.
* @throws Exception\InvalidArgumentException
* @return object[] Returns the value that should be hydrated.
*/
public function hydrate($value, ?array $data = null)
{
if (null === $value) {
return $value;
}

if (! is_array($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Value needs to be an array, got "%s" instead.',
Expand Down
13 changes: 13 additions & 0 deletions test/Strategy/BooleanStrategyTest.php
Expand Up @@ -62,6 +62,13 @@ public function testExtractInteger(): void
$this->assertEquals(0, $hydrator->extract(false));
}

public function testExtractNull(): void
{
$hydrator = new BooleanStrategy(1, 0);

$this->assertEquals(null, $hydrator->extract(null));
}

public function testExtractThrowsExceptionOnUnknownValue(): void
{
$hydrator = new BooleanStrategy(1, 0);
Expand Down Expand Up @@ -93,6 +100,12 @@ public function testHydrateBool(): void
$this->assertEquals(false, $hydrator->hydrate(false));
}

public function testHydrateNull(): void
{
$hydrator = new BooleanStrategy(1, 0);
$this->assertEquals(null, $hydrator->hydrate(null));
}

public function testHydrateUnexpectedValueThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
2 changes: 0 additions & 2 deletions test/Strategy/CollectionStrategyTest.php
Expand Up @@ -121,7 +121,6 @@ public function providerInvalidValueForExtraction()
'boolean-true' => true,
'float' => mt_rand() / mt_getrandmax(),
'integer' => mt_rand(),
'null' => null,
'object' => new stdClass(),
'resource' => fopen(__FILE__, 'r'),
'string-non-existent-class' => 'FooBarBaz9000',
Expand Down Expand Up @@ -248,7 +247,6 @@ public function providerInvalidValueForHydration()
'boolean-true' => true,
'float' => mt_rand() / mt_getrandmax(),
'integer' => mt_rand(),
'null' => null,
'object' => new stdClass(),
'resource' => fopen(__FILE__, 'r'),
'string-non-existent-class' => 'FooBarBaz9000',
Expand Down

0 comments on commit 47fac04

Please sign in to comment.