Skip to content
This repository has been archived by the owner on May 12, 2019. It is now read-only.

Commit

Permalink
make collection iteratable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Dec 1, 2016
1 parent e32de9b commit 2778258
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Collection/LazyModelCollection.php
Expand Up @@ -112,6 +112,16 @@ public function getInitialModels(): array
return $this->initialModels;
}

/**
* @return \ArrayIterator
*/
public function getIterator()
{
$this->resolveModels();

return new \ArrayIterator($this->models);
}

/**
* @return array
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Collection/ModelCollection.php
Expand Up @@ -84,6 +84,14 @@ public function getInitialModels(): array
return $this->initialModels;
}

/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->models);
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/ModelCollectionInterface.php
Expand Up @@ -6,7 +6,7 @@

use Chubbyphp\Model\ModelInterface;

interface ModelCollectionInterface extends \JsonSerializable
interface ModelCollectionInterface extends \IteratorAggregate, \JsonSerializable
{
/**
* @param ModelInterface $model
Expand Down
24 changes: 24 additions & 0 deletions tests/Collection/LazyModelCollectionTest.php
Expand Up @@ -4,6 +4,7 @@

use Chubbyphp\Model\Collection\LazyModelCollection;
use Chubbyphp\Tests\Model\Resources\User;
use Ramsey\Uuid\Uuid;

/**
* @covers Chubbyphp\Model\Collection\LazyModelCollection
Expand Down Expand Up @@ -63,6 +64,29 @@ public function testSetModels()
self::assertCount(1, $modelCollection->getModels());
}

public function testIteratable()
{
$id = (string) Uuid::uuid4();

$user = new User($id);
$user->setUsername('username1');
$user->setPassword('password');
$user->setActive(true);

$modelCollection = new LazyModelCollection(function () use ($user) {
return [$user];
});

foreach ($modelCollection as $key => $model) {
self::assertSame($id, $key);
self::assertSame($user, $model);

return;
}

self::fail('collection is not iteratable');
}

public function testJsonSerialize()
{
$user = new User();
Expand Down
22 changes: 22 additions & 0 deletions tests/Collection/ModelCollectionTest.php
Expand Up @@ -4,6 +4,7 @@

use Chubbyphp\Model\Collection\ModelCollection;
use Chubbyphp\Tests\Model\Resources\User;
use Ramsey\Uuid\Uuid;

/**
* @covers Chubbyphp\Model\Collection\ModelCollection
Expand Down Expand Up @@ -57,6 +58,27 @@ public function testSetModels()
self::assertCount(1, $modelCollection->getModels());
}

public function testIteratable()
{
$id = (string) Uuid::uuid4();

$user = new User($id);
$user->setUsername('username1');
$user->setPassword('password');
$user->setActive(true);

$modelCollection = new ModelCollection([$user]);

foreach ($modelCollection as $key => $model) {
self::assertSame($id, $key);
self::assertSame($user, $model);

return;
}

self::fail('collection is not iteratable');
}

public function testJsonSerialize()
{
$user = new User();
Expand Down

0 comments on commit 2778258

Please sign in to comment.