This repository was archived by the owner on Jul 28, 2023. It is now read-only.
File tree Expand file tree Collapse file tree
src/Integrations/HydratorStrategy
tests/Integrations/HydratorStrategy Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ start :
2+ docker-compose up -d
3+ docker-compose exec app bash
4+
5+ stop :
6+ docker-compose down
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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.$/'
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments