Skip to content

Commit

Permalink
collection: implement fetch pairs support for embeddables
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 23, 2020
1 parent c0e0cca commit 6a7877a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Collection/Helpers/FetchPairsHelper.php
Expand Up @@ -9,6 +9,7 @@
namespace Nextras\Orm\Collection\Helpers;

use Nextras\Dbal\Utils\DateTimeImmutable;
use Nextras\Orm\Entity\Embeddable\IEmbeddable;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\InvalidStateException;
Expand Down Expand Up @@ -66,8 +67,8 @@ private static function getProperty(IEntity $row, array $chain)
$lastPropertyName = "";
do {
$propertyName = array_shift($chain);
if (!$result instanceof IEntity) {
throw new InvalidStateException("Part '$lastPropertyName' of the chain expression does not select IEntity value.");
if (!$result instanceof IEntity && !$result instanceof IEmbeddable) {
throw new InvalidStateException("Part '$lastPropertyName' of the chain expression does not select an IEntity or IEmbeddable.");
}
$lastPropertyName = $propertyName;
$result = $result->getValue($propertyName);
Expand Down
25 changes: 23 additions & 2 deletions tests/cases/unit/Collection/FetchPairsHelperTest.phpt
Expand Up @@ -13,7 +13,9 @@ use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\InvalidStateException;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\Currency;
use NextrasTests\Orm\Ean;
use NextrasTests\Orm\Money;
use NextrasTests\Orm\TestCase;
use Tester\Assert;

Expand Down Expand Up @@ -82,7 +84,7 @@ class FetchPairsHelperTest extends TestCase
}


public function testNester()
public function testNested()
{
$data = new ArrayIterator([
$one = $this->e(
Expand Down Expand Up @@ -167,6 +169,25 @@ class FetchPairsHelperTest extends TestCase
}


public function testEmbeddable()
{
$data = new ArrayIterator([
$this->e(
Book::class,
['price' => new Money(100, Currency::CZK())]
),
$this->e(
Book::class,
['price' => new Money(200, Currency::CZK())]
),
]);
Assert::same(
[100, 200],
FetchPairsHelper::process($data, null, 'price->cents')
);
}


public function testUnsupportedHasMany()
{
Assert::throws(function () {
Expand All @@ -183,7 +204,7 @@ class FetchPairsHelperTest extends TestCase
),
]);
FetchPairsHelper::process($data, null, 'books->id');
}, InvalidStateException::class, "Part 'books' of the chain expression does not select IEntity value.");
}, InvalidStateException::class, "Part 'books' of the chain expression does not select an IEntity or IEmbeddable.");
}


Expand Down

0 comments on commit 6a7877a

Please sign in to comment.