Skip to content

Commit

Permalink
Merge pull request #10 from mediact/feature/JHMG-19
Browse files Browse the repository at this point in the history
2.8.0 Add an interface and implementation to iterate over containers
  • Loading branch information
sjokki authored Oct 3, 2018
2 parents 0098a16 + ecdd06d commit 502dc1c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/DataContainerIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer;

use ArrayIterator;
use IteratorIterator;

class DataContainerIterator extends IteratorIterator implements
DataContainerIteratorInterface
{
/**
* Constructor
*
* @param DataContainerInterface ...$items
*/
public function __construct(
DataContainerInterface ...$items
) {
parent::__construct(new ArrayIterator($items));
}

/**
* Get the current item.
*
* @return DataContainerInterface
*/
public function current(): DataContainerInterface
{
return parent::current();
}
}
19 changes: 19 additions & 0 deletions src/DataContainerIteratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer;

use Iterator;

interface DataContainerIteratorInterface extends Iterator
{
/**
* Get the current item.
*
* @return DataContainerInterface
*/
public function current(): DataContainerInterface;
}
54 changes: 54 additions & 0 deletions tests/DataContainerIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer\Tests;

use Mediact\DataContainer\DataContainerInterface;
use PHPUnit\Framework\TestCase;
use Mediact\DataContainer\DataContainerIterator;

/**
* @coversDefaultClass \Mediact\DataContainer\DataContainerIterator
*/
class DataContainerIteratorTest extends TestCase
{
/**
* @param array $containers
*
* @return void
*
* @covers ::__construct
* @covers ::current
*
* @dataProvider dataProvider
*/
public function testCurrent(array $containers): void
{
$subject = new DataContainerIterator(...$containers);
$this->assertSame(
$containers,
iterator_to_array($subject)
);
}

/**
* @return array
*/
public function dataProvider(): array
{
return [
[
[
$this->createMock(DataContainerInterface::class),
$this->createMock(DataContainerInterface::class),
]
],
[
[]
]
];
}
}

0 comments on commit 502dc1c

Please sign in to comment.