Skip to content

Commit

Permalink
Support creating collections directly from Doctrine Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
benr77 committed Jan 7, 2024
1 parent 0f54742 commit 034154b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Change Log
- Added the factory methods `Collection::from()` and `Collection::empty()`.
- Added helper methods `Collection::isEmpty()` and `Collection::isNotEmpty()`.
- Added `Collection::reverse()` helper to reverse the order of the collection.
- Support creating collections from Doctrine Collections using `Collection::fromDoctrine()`.

# 0.2.0
- Minimum PHP version bumped to 8.1.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"issues": "https://github.com/headsnet/collections/issues"
},
"require": {
"php": ">=8.1"
"php": ">=8.1",
"doctrine/collections": ">1"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
Expand Down
11 changes: 11 additions & 0 deletions src/AbstractImmutableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Headsnet\Collections;

use ArrayIterator;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
use Headsnet\Collections\Exception\InvalidTypeException;
use Headsnet\Collections\Exception\ItemNotFoundException;
use Headsnet\Collections\Exception\OutOfRangeException;
Expand Down Expand Up @@ -55,6 +56,16 @@ public static function from(array $items): static
return new $class($items);
}

/**
* @param DoctrineCollection<int, TValue> $items
*/
public static function fromDoctrine(DoctrineCollection $items): static
{
$class = static::class;

return new $class($items->toArray());
}

public static function empty(): static
{
$class = static::class;
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/ImmutableCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

namespace Headsnet\Collections\Test;

use Doctrine\Common\Collections\ArrayCollection;
use Headsnet\Collections\Exception\InvalidTypeException;
use Headsnet\Collections\Exception\ItemNotFoundException;
use Headsnet\Collections\Test\Fixtures\AugmentedImmutableCollection;
use Headsnet\Collections\Test\Fixtures\CompanionObject;
use Headsnet\Collections\Test\Fixtures\DummyCollectionItem;
use Headsnet\Collections\Test\Fixtures\DummyImmutableCollection;
use Headsnet\Collections\Test\Fixtures\OtherCollectionItem;
use Headsnet\Collections\Test\Fixtures\CompanionObject;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -68,6 +69,18 @@ public function test_can_create_collection_with_extra_properties(): void
$this->assertEquals($someValueObject1, $sut->companionObject);
}

public function test_can_create_collection_from_doctrine_collection(): void
{
$collectionItem1 = new DummyCollectionItem();
$collectionItem2 = new DummyCollectionItem();
$doctrineCollection = new ArrayCollection([$collectionItem1, $collectionItem2]);

$sut = DummyImmutableCollection::fromDoctrine($doctrineCollection);

$this->assertEquals($collectionItem1, $sut->firstOrFail());
$this->assertEquals($collectionItem2, $sut->lastOrFail());
}

public function test_is_empty_check_is_correct(): void
{
$sut = DummyImmutableCollection::empty();
Expand Down

0 comments on commit 034154b

Please sign in to comment.