-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mediact/feature/JHMG-19
2.8.0 Add an interface and implementation to iterate over containers
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] | ||
], | ||
[ | ||
[] | ||
] | ||
]; | ||
} | ||
} |