Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Commit 09eee0f

Browse files
committed
feat(hydrator): add NullableStrategy
1 parent 0c111c8 commit 09eee0f

6 files changed

Lines changed: 103 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
2222
- Nothing
2323
-->
2424

25+
## [1.2.0] - 2020-06-12
26+
### Added
27+
- Added `NullableStrategy`
28+
2529
## [1.1.0] - 2020-04-21
2630
### Added
2731
- Added phpDoc comments as possible

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
start:
2+
docker-compose up -d
3+
docker-compose exec app bash
4+
5+
stop:
6+
docker-compose down

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"phpstan/phpstan-webmozart-assert": "^0.12",
3030
"phpunit/phpunit": "^8.0",
3131
"ramsey/uuid": "^3.9|^4.0",
32-
"symplify/easy-coding-standard-prefixed": "^7.2"
32+
"symplify/easy-coding-standard-prefixed": "^8.0"
3333
},
3434
"suggest": {
3535
"ramsey/uuid": "To use UuidValue",

phpstan.neon.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ parameters:
88
inferPrivatePropertyTypeFromConstructor: true
99
checkMissingIterableValueType: false
1010
checkGenericClassInNonGenericObjectType: false
11+
bootstrapFiles:
12+
- vendor/autoload.php
1113
paths:
1214
- src/
13-
autoload_files:
14-
- vendor/autoload.php
1515
ignoreErrors:
1616
- '/^PHPDoc tag \@param for parameter (.*) with type (.*)\|string is not subtype of native type string.$/'
1717
- '/^Parameter #2 \$class of static method Webmozart\\Assert\\Assert::isInstanceOf\(\) expects class-string\<object\>, (.*) given.$/'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spaceonfire\ValueObject\Integrations\HydratorStrategy;
6+
7+
use Laminas\Hydrator\Strategy\StrategyInterface;
8+
9+
final class NullableStrategy implements StrategyInterface
10+
{
11+
/**
12+
* @var StrategyInterface
13+
*/
14+
private $strategy;
15+
16+
/**
17+
* NullableStrategy constructor.
18+
* @param StrategyInterface $strategy
19+
*/
20+
public function __construct(StrategyInterface $strategy)
21+
{
22+
$this->strategy = $strategy;
23+
}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function extract($value, ?object $object = null)
29+
{
30+
if ($value === null) {
31+
return null;
32+
}
33+
34+
return $this->strategy->extract($value, $object);
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function hydrate($value, ?array $data)
41+
{
42+
if ($value === null) {
43+
return null;
44+
}
45+
46+
return $this->strategy->hydrate($value, $data);
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spaceonfire\ValueObject\Integrations\HydratorStrategy;
6+
7+
use Laminas\Hydrator\Strategy\StrategyInterface;
8+
use PHPUnit\Framework\TestCase;
9+
use Prophecy\Argument;
10+
11+
class NullableStrategyTest extends TestCase
12+
{
13+
public function testExtract(): void
14+
{
15+
$strategyMock = $this->prophesize(StrategyInterface::class);
16+
$strategyMock->extract(Argument::any(), Argument::any())
17+
->shouldBeCalledTimes(1)
18+
->will(function ($args) {
19+
return $args[0];
20+
});
21+
22+
$strategy = new NullableStrategy($strategyMock->reveal());
23+
24+
self::assertSame('foo', $strategy->extract('foo'));
25+
self::assertNull($strategy->extract(null));
26+
}
27+
28+
public function testHydrate(): void
29+
{
30+
$strategyMock = $this->prophesize(StrategyInterface::class);
31+
$strategyMock->hydrate(Argument::any(), Argument::any())
32+
->shouldBeCalledTimes(1)
33+
->will(function ($args) {
34+
return $args[0];
35+
});
36+
37+
$strategy = new NullableStrategy($strategyMock->reveal());
38+
39+
self::assertSame('foo', $strategy->hydrate('foo', []));
40+
self::assertNull($strategy->hydrate(null, []));
41+
}
42+
}

0 commit comments

Comments
 (0)