Skip to content

Commit

Permalink
#3 MapStorage is Traversable
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Dec 15, 2014
1 parent bd3cdd2 commit bf472c5
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/Adapter/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BlackBox\Adapter;

use ArrayIterator;
use BlackBox\MapStorage;
use IteratorAggregate;

/**
* Stores data in memory in an array.
Expand All @@ -11,7 +13,7 @@
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class ArrayStorage implements MapStorage
class ArrayStorage implements IteratorAggregate, MapStorage
{
/**
* @var array
Expand All @@ -37,4 +39,12 @@ public function set($id, $data)
{
$this->storage[$id] = $data;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->storage);
}
}
39 changes: 38 additions & 1 deletion src/Adapter/MultipleFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace BlackBox\Adapter;

use ArrayIterator;
use BlackBox\Exception\StorageException;
use BlackBox\MapStorage;
use IteratorAggregate;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
* Stores data in multiple files.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class MultipleFileStorage implements MapStorage
class MultipleFileStorage implements IteratorAggregate, MapStorage
{
/**
* @var string
Expand Down Expand Up @@ -65,6 +69,14 @@ public function set($id, $data)
$this->getFileStorage($id)->setData($data);
}

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

/**
* @param string $id
*
Expand Down Expand Up @@ -93,4 +105,29 @@ private function getFilename($id)
// TODO escape the ID
return $this->directory . '/' . $id . $extension;
}

/**
* @return array Get all entries as an array.
*/
private function getAll()
{
$files = new Finder();
$files->files()->in($this->directory);
if ($this->fileExtension) {
$files->name('*.' . $this->fileExtension);
}

$data = [];
foreach ($files as $file) {
/** @var SplFileInfo $file */
$id = $file->getFilename();
if ($this->fileExtension) {
$id = substr($id, 0, -strlen('.' . $this->fileExtension));
}

$data[$id] = $this->get($id);
}

return $data;
}
}
3 changes: 2 additions & 1 deletion src/MapStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace BlackBox;

use BlackBox\Exception\StorageException;
use Traversable;

/**
* Stores multiple items identified by ids.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
interface MapStorage
interface MapStorage extends Traversable
{
/**
* Returns the data stored under the given ID.
Expand Down
11 changes: 10 additions & 1 deletion src/Transformer/MapWithTransformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace BlackBox\Transformer;

use BlackBox\MapStorage;
use IteratorAggregate;

/**
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class MapWithTransformers implements MapStorage
class MapWithTransformers implements IteratorAggregate, MapStorage
{
/**
* @var MapStorage
Expand Down Expand Up @@ -49,6 +50,14 @@ public function set($id, $data)
$this->storage->set($id, $data);
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
return $this->storage;
}

public function addTransformer(Transformer $transformer)
{
$this->transformers[] = $transformer;
Expand Down
37 changes: 37 additions & 0 deletions tests/Adapter/ArrayStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\BlackBox\Adapter;

use BlackBox\Adapter\ArrayStorage;

/**
* @covers \BlackBox\Adapter\ArrayStorage
*/
class ArrayStorageTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function it_should_store_data_as_map()
{
$storage = new ArrayStorage();

$storage->set('foo', 'bar');

$this->assertEquals('bar', $storage->get('foo'));
}

/**
* @test
*/
public function it_should_be_traversable()
{
$storage = new ArrayStorage();

$this->assertEquals([], iterator_to_array($storage));

$storage->set('foo', 'bar');

$this->assertEquals([ 'foo' => 'bar' ], iterator_to_array($storage));
}
}
15 changes: 15 additions & 0 deletions tests/Adapter/MultipleFileStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function it_should_store_data_as_map($extension)
$this->assertEquals('bar', $storage->get('foo'));
}

/**
* @test
* @dataProvider extensionProvider
*/
public function it_should_be_traversable($extension)
{
$storage = new MultipleFileStorage($this->directory, $extension);

$this->assertEquals([], iterator_to_array($storage));

$storage->set('foo', 'bar');

$this->assertEquals([ 'foo' => 'bar' ], iterator_to_array($storage));
}

public function extensionProvider()
{
return [
Expand Down

0 comments on commit bf472c5

Please sign in to comment.