Skip to content

Commit

Permalink
Dictionaries are now countable
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Oct 29, 2018
1 parent 042759f commit 157d3b7
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 75 deletions.
5 changes: 3 additions & 2 deletions .symfony_checker
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use Pedrotroller\Symfony\IntegrationChecker\ConfigurableKernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\KernelInterface;
use Webmozart\Assert\Assert;
use Knp\DictionaryBundle\Dictionary\Collection;

$test = function (KernelInterface $kernel) {
if (false === $kernel->getContainer()->get('knp_dictionary.registry') instanceof DictionaryRegistry) {
throw new Exception('Service "knp_dictionary.registry" unavailable.');
if (false === $kernel->getContainer()->get(Collection::class) instanceof Collection) {
throw new Exception('Service "Knp\DictionaryBundle\Dictionary\Collection" unavailable.');
}
};

Expand Down
49 changes: 49 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/CombinedSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace spec\Knp\DictionaryBundle\Dictionary;

use ArrayIterator;
use Assert\Assert;
use IteratorAggregate;
use Knp\DictionaryBundle\Dictionary;
use PhpSpec\ObjectBehavior;

Expand Down Expand Up @@ -73,8 +75,55 @@ function it_getvalues_should_return_dictionaries_values($dictionary1, $dictionar
]);
}

function it_can_iterate_over_dictionaries($dictionary1, $dictionary2, $dictionary3)
{
$dictionary1->getIterator()->willReturn(new ArrayIterator([
'foo1' => 'foo10',
'foo2' => 'foo20',
]));

$dictionary2->getIterator()->willReturn(new ArrayIterator([
'bar1' => 'bar10',
'bar2' => 'bar20',
]));

$dictionary3->getIterator()->willReturn(new ArrayIterator([
'foo2' => 'baz20',
'bar2' => 'baz20',
]));

$this->shouldIterateOver([
'foo1' => 'foo10',
'foo2' => 'baz20',
'bar1' => 'bar10',
'bar2' => 'baz20',
]);
}

function its_getname_should_return_dictionary_name()
{
$this->getName()->shouldReturn('combined_dictionary');
}

function it_sums_the_count_of_elements($dictionary1, $dictionary2, $dictionary3)
{
$dictionary1->count()->willReturn(1);

$dictionary2->count()->willReturn(2);

$dictionary3->count()->willReturn(4);

$this->count()->shouldReturn(7);
}

public function getMatchers(): array
{
return [
'iterateOver' => function (IteratorAggregate $iterator, array $array): bool {
Assert::that(iterator_to_array($iterator))->eq($array);

return true;
},
];
}
}
5 changes: 5 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/InvokableSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ function its_getname_should_return_dictionary_name()
$this->getName()->shouldReturn('foo');
}

function it_returns_the_count_of_elements()
{
$this->count()->shouldReturn(3);
}

public function execution()
{
if (false === $this->executed) {
Expand Down
5 changes: 5 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/SimpleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ function its_getname_should_return_dictionary_name()
{
$this->getName()->shouldReturn('foo');
}

function it_is_countable()
{
$this->count()->shouldReturn(3);
}
}
5 changes: 5 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/TraceableSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ function it_trace_iteration($collector)

Assert::that(iterator_to_array($this->getIterator()->getWrappedObject()))->eq(['foo' => 'bar', 'baz' => null]);
}

function it_delegates_the_count_to_the_other_dictionary()
{
$this->count()->shouldReturn(2);
}
}
3 changes: 2 additions & 1 deletion src/Knp/DictionaryBundle/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace Knp\DictionaryBundle;

use ArrayAccess;
use Countable;
use IteratorAggregate;

interface Dictionary extends ArrayAccess, IteratorAggregate
interface Dictionary extends ArrayAccess, Countable, IteratorAggregate
{
const VALUE = 'value';
const VALUE_AS_KEY = 'value_as_key';
Expand Down
9 changes: 5 additions & 4 deletions src/Knp/DictionaryBundle/Dictionary/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use ArrayAccess;
use ArrayIterator;
use Countable;
use Iterator;
use IteratorAggregate;
use Knp\DictionaryBundle\Dictionary;
use Knp\DictionaryBundle\Exception\DictionaryNotFoundException;
use RuntimeException;

final class Collection implements ArrayAccess, IteratorAggregate, Countable
final class Collection implements ArrayAccess, Countable, IteratorAggregate
{
/**
* @var Dictionary[]
Expand All @@ -31,7 +32,7 @@ public function add(Dictionary $dictionary)
$this->dictionaries[$dictionary->getName()] = $dictionary;
}

public function offsetExists($offset)
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->dictionaries);
}
Expand Down Expand Up @@ -62,12 +63,12 @@ public function offsetUnset($offset)
throw new RuntimeException('It is not possible to remove a dictionary from the collection.');
}

public function getIterator()
public function getIterator(): Iterator
{
return new ArrayIterator($this->dictionaries);
}

public function count()
public function count(): int
{
return \count($this->dictionaries);
}
Expand Down
17 changes: 10 additions & 7 deletions src/Knp/DictionaryBundle/Dictionary/Combined.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Knp\DictionaryBundle\Dictionary;

use AppendIterator;
use InvalidArgumentException;
use Iterator;
use Knp\DictionaryBundle\Dictionary;

class Combined implements Dictionary
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getKeys(): array
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return \in_array($offset, $this->getKeys());
}
Expand Down Expand Up @@ -115,14 +115,17 @@ public function offsetUnset($offset)
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): Iterator
{
$iterator = new AppendIterator();

foreach ($this->dictionaries as $dictionary) {
$iterator->append($dictionary->getIterator());
foreach ($dictionary as $key => $value) {
yield $key => $value;
}
}
}

return $iterator;
public function count(): int
{
return (int) (array_sum(array_map('count', $this->dictionaries)));
}
}
12 changes: 10 additions & 2 deletions src/Knp/DictionaryBundle/Dictionary/Invokable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayIterator;
use InvalidArgumentException;
use Iterator;
use Knp\DictionaryBundle\Dictionary;

final class Invokable implements Dictionary
Expand Down Expand Up @@ -68,7 +69,7 @@ public function getKeys(): array
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
$this->hydrate();

Expand Down Expand Up @@ -108,7 +109,7 @@ public function offsetUnset($offset)
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): Iterator
{
$this->hydrate();

Expand All @@ -134,4 +135,11 @@ private function hydrate()

$this->values = $values;
}

public function count(): int
{
$this->hydrate();

return \count($this->values);
}
}
10 changes: 8 additions & 2 deletions src/Knp/DictionaryBundle/Dictionary/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Knp\DictionaryBundle\Dictionary;

use ArrayIterator;
use Iterator;
use Knp\DictionaryBundle\Dictionary;

final class Simple implements Dictionary
Expand Down Expand Up @@ -52,7 +53,7 @@ public function getKeys(): array
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->values);
}
Expand Down Expand Up @@ -84,8 +85,13 @@ public function offsetUnset($offset)
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): Iterator
{
return new ArrayIterator($this->values);
}

public function count(): int
{
return \count($this->values);
}
}
16 changes: 12 additions & 4 deletions src/Knp/DictionaryBundle/Dictionary/Traceable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Knp\DictionaryBundle\Dictionary;

use IteratorAggregate;
use Knp\DictionaryBundle\DataCollector\DictionaryDataCollector;
use Knp\DictionaryBundle\Dictionary;

Expand Down Expand Up @@ -56,7 +57,7 @@ public function getKeys(): array
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
$this->trace();

Expand Down Expand Up @@ -96,15 +97,15 @@ public function offsetUnset($offset)
/**
* {@inheritdoc}
*/
public function getIterator()
public function getIterator(): IteratorAggregate
{
$this->trace();

return $this->dictionary->getIterator();
return $this->dictionary;
}

/**
* Register this dictioanry as used.
* Register this dictionary as used.
*/
private function trace()
{
Expand All @@ -114,4 +115,11 @@ private function trace()
array_values($this->dictionary->getValues())
);
}

public function count(): int
{
$this->trace();

return $this->dictionary->count();
}
}
53 changes: 0 additions & 53 deletions src/Knp/DictionaryBundle/Dictionary/Traits/Legacy.php

This file was deleted.

0 comments on commit 157d3b7

Please sign in to comment.