Skip to content

Commit

Permalink
AbstractCollection exchageArray improved
Browse files Browse the repository at this point in the history
  • Loading branch information
leogr committed Aug 8, 2015
1 parent 7789342 commit 6aa8507
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
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 6aa8507

Please sign in to comment.