/** * Converts the given value so that it can be hydrated by the hydrator. * * @param array|\Traversable|null $value The original value. * @return \ArrayAccess|null Returns the value that should be hydrated. */ public function hydrate($value) { if ($this->nullable && $value === null) { return null; } $this->hasOneStrategy->setPrototypeStrategy($this->getPrototypeStrategy()); $return = clone $this->arrayObjectPrototype; if (is_array($value) || $value instanceof \Traversable) { foreach ($value as $key => $data) { $return[$key] = $this->hasOneStrategy->hydrate($data); } } else { throw new Exception\InvalidArgumentException(sprintf('Value must be null (only if nullable option is enabled), or an array, or a Traversable: "%s" given', is_object($value) ? get_class($value) : gettype($value))); } return $return; }
public function testExtract() { $abstractObject = $this->getMockForAbstractClass('\\Matryoshka\\Model\\Object\\AbstractObject'); $strategy = new HasOneStrategy($abstractObject); $strategy->setNullable(false); $this->assertInternalType('array', $strategy->extract($abstractObject)); $this->assertInternalType('array', $strategy->extract([])); $this->assertInternalType('array', $strategy->extract(null)); $strategy->setNullable(true); $this->assertNull($strategy->extract(null)); $this->setExpectedException('\\Matryoshka\\Model\\Exception\\InvalidArgumentException'); $strategy->extract('wrong value'); }