Skip to content

Commit

Permalink
collection: add toMemoryCollection() (BC break!)
Browse files Browse the repository at this point in the history
BC breaks - ICollection interface has changed
  • Loading branch information
hrach committed Aug 14, 2021
1 parent 57d7de9 commit 5d4bb86
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @template E of IEntity
* @implements ICollection<E>
*/
class ArrayCollection implements ICollection
class ArrayCollection implements ICollection, MemoryCollection
{
/**
* @var callable[]
Expand Down Expand Up @@ -246,6 +246,12 @@ public function countStored(): int
}


public function toMemoryCollection(): MemoryCollection
{
return clone $this;
}


public function setRelationshipMapper(IRelationshipMapper $mapper = null, IEntity $parent = null): ICollection
{
$this->relationshipMapper = $mapper;
Expand Down
8 changes: 8 additions & 0 deletions src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ public function countStored(): int
}


public function toMemoryCollection(): MemoryCollection
{
$collection = clone $this;
$entities = $collection->fetchAll();
return new ArrayCollection($entities, $this->mapper->getRepository());
}


public function setRelationshipMapper(IRelationshipMapper $mapper = null, IEntity $parent = null): ICollection
{
$this->relationshipMapper = $mapper;
Expand Down
8 changes: 7 additions & 1 deletion src/Collection/EmptyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @template E of IEntity
* @implements ICollection<E>
*/
final class EmptyCollection implements ICollection
final class EmptyCollection implements ICollection, MemoryCollection
{
/** @var IRelationshipMapper|null */
private $relationshipMapper;
Expand Down Expand Up @@ -126,6 +126,12 @@ public function count(): int
}


public function toMemoryCollection(): MemoryCollection
{
return clone $this;
}


public function subscribeOnEntityFetch(callable $callback): void
{
}
Expand Down
12 changes: 12 additions & 0 deletions src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class HasManyCollection implements ICollection
*/
public $onEntityFetch = [];

/** @var IRepository */
private $repository;

/**
* @var ICollection<IEntity>
* @phpstan-var ICollection<E>
Expand Down Expand Up @@ -63,6 +66,7 @@ public function __construct(
callable $diffCallback
)
{
$this->repository = $repository;
$this->storageCollection = $innerCollection;
$this->diffCallback = $diffCallback;
$this->inMemoryCollection = new MutableArrayCollection([], $repository); // @phpstan-ignore-line
Expand Down Expand Up @@ -213,6 +217,14 @@ public function countStored(): int
}


public function toMemoryCollection(): MemoryCollection
{
$collection = clone $this;
$entities = $collection->fetchAll();
return new ArrayCollection($entities, $this->repository);
}


public function setRelationshipMapper(IRelationshipMapper $mapper = null): ICollection
{
$this->storageCollection->setRelationshipMapper($mapper);
Expand Down
6 changes: 6 additions & 0 deletions src/Collection/ICollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public function fetchPairs(?string $key = null, ?string $value = null): array;
public function getIterator();


/**
* Fetches requsted data and returns MemoryCollection instance with the fetched entities.
*/
public function toMemoryCollection(): MemoryCollection;


/**
* Sets relationship mapping over the collection.
* @return static
Expand Down
12 changes: 12 additions & 0 deletions src/Collection/MemoryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Nextras\Orm\Collection;


/**
* This kind of collection promises in-memory processing of data.
*/
interface MemoryCollection extends ICollection
{
}

2 changes: 1 addition & 1 deletion src/Relationships/IRelationshipCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function toCollection(): ICollection;


/**
* Returns true if colletion was loaded.
* Returns true if collection was loaded.
*/
public function isLoaded(): bool;

Expand Down
40 changes: 37 additions & 3 deletions tests/cases/integration/Collection/collection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace NextrasTests\Orm\Integration\Collection;


use Nextras\Orm\Collection\ArrayCollection;
use Nextras\Orm\Collection\DbalCollection;
use Nextras\Orm\Collection\EmptyCollection;
use Nextras\Orm\Collection\HasManyCollection;
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Exception\NoResultException;
use Nextras\Orm\NoResultException;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Ean;
use NextrasTests\Orm\Helper;
use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\TagFollower;
use Tester\Assert;
Expand Down Expand Up @@ -309,6 +312,37 @@ class CollectionTest extends DataTestCase
$books = $this->orm->tagFollowers->findBy(['tag->books->id' => 1]);
Assert::count(2, $books);
}


public function testToArrayCollection()
{
$c1 = $this->orm->authors->findAll();
$c2 = $c1->toMemoryCollection();
Assert::type(ArrayCollection::class, $c2);
Assert::same($c2->count(), $c1->countStored());

$author = $this->orm->authors->getByIdChecked(1);
$c3 = $author->books->toCollection();
$c4 = $c3->toMemoryCollection();
Assert::type(DbalCollection::class, $c3);
Assert::type(ArrayCollection::class, $c4);
Assert::same($c4->count(), $c3->countStored());

$author->books->add(new Book());
$c5 = $author->books->toCollection();
$c6 = $c5->toMemoryCollection();
Assert::type(HasManyCollection::class, $c5);
Assert::type(ArrayCollection::class, $c6);
Assert::same($c6->count(), $c5->countStored());

$author = new Author();
$this->orm->authors->attach($author);
$c7 = $author->tagFollowers->toCollection();
$c8 = $c7->toMemoryCollection();
Assert::type(EmptyCollection::class, $c7);
Assert::type(ArrayCollection::class, $c8);
Assert::same($c8->count(), $c7->countStored());
}
}


Expand Down

0 comments on commit 5d4bb86

Please sign in to comment.