diff --git a/library/Hydrator/Strategy/HasManyStrategy.php b/library/Hydrator/Strategy/HasManyStrategy.php index 751c475..4533d44 100644 --- a/library/Hydrator/Strategy/HasManyStrategy.php +++ b/library/Hydrator/Strategy/HasManyStrategy.php @@ -12,13 +12,19 @@ use Matryoshka\Model\Exception; use Zend\Stdlib\Hydrator\HydratorAwareInterface; use Zend\Stdlib\Hydrator\Strategy\StrategyInterface; +use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareTrait; +use Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareInterface; /** * Class HasManyStrategy */ -class HasManyStrategy implements StrategyInterface, NullableStrategyInterface +class HasManyStrategy implements + StrategyInterface, + NullableStrategyInterface, + PrototypeStrategyAwareInterface { use NullableStrategyTrait; + use PrototypeStrategyAwareTrait; /** * @var HasOneStrategy @@ -42,7 +48,7 @@ public function __construct( \ArrayAccess $arrayObjectPrototype = null, $nullable = true ) { - $this->hasOneStrategy = new HasOneStrategy($objectPrototype); + $this->hasOneStrategy = new HasOneStrategy($objectPrototype, false); $this->arrayObjectPrototype = $arrayObjectPrototype ? $arrayObjectPrototype : new ArrayObject([], ArrayObject::ARRAY_AS_PROPS); $this->setNullable($nullable); } @@ -94,6 +100,7 @@ public function hydrate($value) return null; } + $this->hasOneStrategy->setPrototypeStrategy($this->getPrototypeStrategy()); $return = clone $this->arrayObjectPrototype; if (is_array($value) || $value instanceof \Traversable) { foreach ($value as $key => $data) { diff --git a/tests/Hydrator/Strategy/HasManyStrategyTest.php b/tests/Hydrator/Strategy/HasManyStrategyTest.php index fe3a703..e8ba601 100644 --- a/tests/Hydrator/Strategy/HasManyStrategyTest.php +++ b/tests/Hydrator/Strategy/HasManyStrategyTest.php @@ -76,4 +76,25 @@ public function testExtract() $this->setExpectedException('\Matryoshka\Model\Exception\InvalidArgumentException'); $strategy->extract('wrong value'); } + + public function testPrototypeStrategy() + { + $abstractObject = $this->getMockForAbstractClass('\Matryoshka\Model\Object\AbstractObject'); + $strategy = new HasManyStrategy($abstractObject); + + $this->assertInstanceOf('\Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyAwareInterface', $strategy); + + $prototypeStrategy = $this->getMockForAbstractClass( + '\Matryoshka\Model\Object\PrototypeStrategy\PrototypeStrategyInterface' + ); + + $prototypeStrategy->expects($this->atLeastOnce()) + ->method('createObject') + ->with($this->equalTo($abstractObject)) + ->willReturn($abstractObject); + + $strategy->setPrototypeStrategy($prototypeStrategy); + $this->assertInstanceOf(\ArrayObject::class, $strategy->hydrate([[]])); + + } }