DefaultPsiFactory::createSingleIterator()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * File was created 11.05.2015 07:05
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi;
9
10
use PeekAndPoke\Component\Psi\Exception\PsiException;
11
use PeekAndPoke\Component\Psi\Iterator\KeylessAppendIterator;
12
13
/**
14
 * PsiFactory
15
 *
16
 * @author Karsten J. Gerber <[email protected]>
17
 */
18
class DefaultPsiFactory implements PsiFactory
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 129
    public function createSolver()
24
    {
25 129
        return new DefaultSolver();
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 142
    public function createIterator(array $iteratables, PsiOptions $options)
32
    {
33 142
        $count = count($iteratables);
34
35 142
        if ($count === 1) {
36 127
            return $this->createSingleIterator($iteratables[0]);
37
        }
38
39 15
        if ($count > 1) {
40 14
            return $this->createIteratorForMultipleInputs($iteratables, $options);
41
        }
42
43 1
        return new \ArrayIterator();
44
    }
45
46
    /**
47
     * @param array      $iteratables
48
     * @param PsiOptions $options
49
     *
50
     * @return \AppendIterator|KeylessAppendIterator
51
     *
52
     * @throws PsiException
53
     */
54 14
    protected function createIteratorForMultipleInputs(array $iteratables, PsiOptions $options)
55
    {
56
        // when we have multiple inputs we need an option to decide on how to deal with colliding keys
57 14
        if ($options->isPreserveKeysOfMultipleInputs()) {
58
            // this iterator return the original keys of each child iterator
59
            // -> this leads to conflicts OR means we preserve keys
60 3
            $iterator = new \AppendIterator();
61
        } else {
62
            // this iterator will create numeric keys from 0 .. n
63 11
            $iterator = new KeylessAppendIterator();
64
        }
65
66 14
        foreach ($iteratables as $iteratable) {
67 14
            $iterator->append($this->createSingleIterator($iteratable));
68
        }
69
70 8
        return $iterator;
71
    }
72
73
    /**
74
     * @param mixed $iteratable
75
     *
76
     * @return \Iterator|\Traversable
77
     *
78
     * @throws PsiException
79
     */
80 141
    protected function createSingleIterator($iteratable)
81
    {
82 141
        if ($iteratable === null) {
83 3
            return new \ArrayIterator();
84
        }
85
86 140
        if ($iteratable instanceof \Iterator) {
87 6
            return $iteratable;
88
        }
89
90 138
        if ($iteratable instanceof \Traversable) {
91 2
            return new \IteratorIterator($iteratable);
92
        }
93
94 138
        if (is_array($iteratable)) {
95 133
            return new \ArrayIterator($iteratable);
96
        }
97
98 7
        throw new PsiException('Invalid input, not an array or an Iterator or a Traversable');
99
    }
100
}
101