Skip to content

Commit

Permalink
Merge pull request #9 from mediact/feature/iterable-data-container
Browse files Browse the repository at this point in the history
2.7.1 Let data container interface be iterable
  • Loading branch information
janmartenjongerius authored Sep 4, 2018
2 parents 9f08612 + 011ca51 commit 0098a16
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/DataContainerDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Mediact\DataContainer;

use Traversable;

/**
* Implements DataContainerInterface
*/
Expand Down Expand Up @@ -194,6 +196,17 @@ public function move(string $pattern, string $replacement)
->move($pattern, $replacement);
}

/**
* Retrieve an external iterator.
*
* @return Traversable
*/
public function getIterator(): Traversable
{
return $this->getStorage()
->getIterator();
}

/**
* Prepare the object for cloning.
*
Expand Down
4 changes: 3 additions & 1 deletion src/DataContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace Mediact\DataContainer;

use IteratorAggregate;

/**
* Contains any data which can be accessed using dot-notation.
*/
interface DataContainerInterface
interface DataContainerInterface extends IteratorAggregate
{
/**
* The separator used in paths.
Expand Down
2 changes: 2 additions & 0 deletions src/DataContainerIteratorAggregateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Implements IteratorAggregate
*
* @deprecated IteratorAggregate is implemented by DataContainerDecoratorTrait.
*/
trait DataContainerIteratorAggregateTrait
{
Expand Down
7 changes: 4 additions & 3 deletions src/IterableDataContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

namespace Mediact\DataContainer;

use IteratorAggregate;
use Traversable;

/**
* @deprecated DataContainerInterface is iterable.
*/
interface IterableDataContainerInterface extends
DataContainerInterface,
IteratorAggregate
DataContainerInterface
{
/**
* Retrieve an external iterator.
Expand Down
21 changes: 21 additions & 0 deletions tests/DataContainerDecoratorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Mediact\DataContainer\Tests;

use ArrayIterator;
use Mediact\DataContainer\DataContainer;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\Tests\TestDouble\DataContainerImplementationDouble;
Expand Down Expand Up @@ -262,6 +263,26 @@ public function testMove()
$double->move($pattern, $destination);
}

/**
* @return void
*
* @covers ::getIterator
*/
public function testGetIterator()
{
$data = ['some_data'];
$iterator = new ArrayIterator($data);
$storage = $this->createMock(DataContainerInterface::class);

$storage
->expects(self::once())
->method('getIterator')
->willReturn($iterator);

$double = new DataContainerImplementationDouble($storage);
$this->assertSame($data, iterator_to_array($double));
}

/**
* @return void
*
Expand Down
32 changes: 30 additions & 2 deletions tests/DataContainerIteratorAggregateTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

namespace Mediact\DataContainer\Tests;

use IteratorAggregate;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\Tests\TestDouble\DataContainerImplementationDouble;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -30,7 +31,34 @@ public function testGetIterator()
->method('all')
->willReturn($data);

$double = new DataContainerImplementationDouble($storage);
$double = new class ($storage) implements IteratorAggregate
{
use DataContainerIteratorAggregateTrait;

/** @var DataContainerInterface */
private $storage;

/**
* Constructor.
*
* @param DataContainerInterface $storage
*/
public function __construct(DataContainerInterface $storage)
{
$this->storage = $storage;
}

/**
* Get the storage.
*
* @return DataContainerInterface
*/
protected function getStorage(): DataContainerInterface
{
return $this->storage;
}
};

$this->assertEquals($data, iterator_to_array($double));
}
}
3 changes: 1 addition & 2 deletions tests/TestDouble/DataContainerImplementationDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;

class DataContainerImplementationDouble implements DataContainerInterface, IteratorAggregate
class DataContainerImplementationDouble implements DataContainerInterface
{
use DataContainerDecoratorTrait;
use DataContainerIteratorAggregateTrait;

/**
* Constructor.
Expand Down
41 changes: 41 additions & 0 deletions tests/TestDouble/IterableImplementationDouble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright MediaCT. All rights reserved.
* https://www.mediact.nl
*/

namespace Mediact\DataContainer\Tests\TestDouble;

use IteratorAggregate;
use Mediact\DataContainer\DataContainer;
use Mediact\DataContainer\DataContainerDecoratorTrait;
use Mediact\DataContainer\DataContainerInterface;
use Mediact\DataContainer\DataContainerIteratorAggregateTrait;

class IterableImplementationDouble implements IteratorAggregate
{
use DataContainerIteratorAggregateTrait;

/** @var DataContainerInterface */
private $storage;

/**
* Constructor.
*
* @param DataContainerInterface $storage
*/
public function __construct(DataContainerInterface $storage)
{
$this->storage = $storage;
}

/**
* Get the storage.
*
* @return DataContainerInterface
*/
protected function getStorage(): DataContainerInterface
{
return $this->storage;
}
}

0 comments on commit 0098a16

Please sign in to comment.