Skip to content

Commit

Permalink
Merge pull request #39 from matryoshka-model/develop
Browse files Browse the repository at this point in the history
Backport
  • Loading branch information
leogr committed Aug 8, 2015
2 parents 8aecf15 + 6aa8507 commit fe04658
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
14 changes: 6 additions & 8 deletions library/Hydrator/Strategy/HasOneStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,17 @@ public function extract($value)
return $this->nullable ? null : [];
}

if (is_object($value)) {
$objectPrototype = $this->getObjectPrototype();
return $objectPrototype->getHydrator()->extract($value);
}

if (is_array($value)) {
return $value;
}

$objectPrototype = $this->getObjectPrototype();

if ($value instanceof $objectPrototype) {
return $objectPrototype->getHydrator()->extract($value);
}

throw new Exception\InvalidArgumentException(sprintf(
'Invalid value: must be null (only if nullable option is enabled), or an array, or an instance of "%s": "%s" given',
get_class($objectPrototype),
'Invalid value: must be null, or an array, or an object: "%s" given',
is_object($value) ? get_class($value) : gettype($value)
));
}
Expand Down
28 changes: 10 additions & 18 deletions library/Object/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,18 @@ public function append($value)
* @return array
* @throws Exception\InvalidArgumentException
*/
/**
* {@inheritdoc}
*/
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException(
'Passed variable is not an array or object, using empty array instead'
);
}

if (is_object($data) && ($data instanceof parent || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
$oldData = parent::exchangeArray($data);
try {
$this->validateData($this->storage);
} catch (\Exception $e) {
$this->storage = $oldData;
throw $e;
}

if (!is_array($data)) {
$data = (array) $data;
}

$this->validateData($data);

$storage = $this->storage;
$this->storage = $data;
return $storage;
return $oldData;
}
}
11 changes: 9 additions & 2 deletions tests/Object/AbstractCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ public function testExchangeArrayArrayIterator()
}
public function testExchangeArrayStringArgumentFail()
{
$data = ['foo' => 'bar'];
$this->setExpectedException('InvalidArgumentException');
$ar = $this->getMockForAbstractClass(AbstractCollection::class, [['foo' => 'bar']]);
$old = $ar->exchangeArray('Bacon');
$ar = $this->getMockForAbstractClass(AbstractCollection::class, [$data]);
try {
$old = $ar->exchangeArray('Bacon');
} catch (\Exception $e) {
// Test data did not change
$this->assertEquals($data, $ar->getArrayCopy());
throw $e;
}
}
}

0 comments on commit fe04658

Please sign in to comment.