Completed
Pull Request — master (#126)
by Phil
02:35
created

InflectorAggregate::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Inflector;
4
5
use Generator;
6
use League\Container\ContainerAwareTrait;
7
8
class InflectorAggregate implements InflectorAggregateInterface
9
{
10
    use ContainerAwareTrait;
11
12
    /**
13
     * @var \League\Container\Inflector[]
14
     */
15
    protected $inflectors = [];
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 9
    public function add(string $type, callable $callback = null): Inflector
21
    {
22 9
        $inflector          = new Inflector($type, $callback);
23 9
        $this->inflectors[] = $inflector;
24
25 9
        return $inflector;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 6
    public function getIterator(): Generator
32
    {
33 6
        $count = count($this->inflectors);
34
35 6
        for ($i = 0; $i < $count; $i++) {
36 6
            yield $this->inflectors[$i];
37
        }
38 6
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3
    public function inflect($object)
44
    {
45 3
        foreach ($this->getIterator() as $inflector) {
46 3
            $type = $inflector->getType();
47
48 3
            if (! $object instanceof $type) {
49 3
                continue;
50
            }
51
52 3
            $inflector->setContainer($this->getContainer());
53 3
            $inflector->inflect($object);
54
        }
55
56 3
        return $object;
57
    }
58
}
59