UniqueIterator::rewind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Itertools\lib;
7
8
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
9
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
10
11
class UniqueIterator extends \FilterIterator implements FiniteIterableInterface
12
{
13
    use FiniteIterableTrait;
14
15
    /** @var \Closure */
16
    private $func;
17
18
    /** @var array */
19
    private $seen;
20
21
    /**
22
     * @param \Closure $func
23
     * @param \Iterator $iterable
24
     */
25 15
    public function __construct(\Closure $func, \Iterator $iterable)
26
    {
27 15
        $this->func = $func;
28 15
        $this->seen = [];
29 15
        parent::__construct($iterable);
30 15
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 11
    public function accept()
36
    {
37 11
        $checkValue = call_user_func($this->func, $this->current(), $this->key());
38 11
        if (in_array($checkValue, $this->seen)) {
39 6
            return false;
40
        } else {
41 11
            $this->seen [] = $checkValue;
42 11
            return true;
43
        }
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 13
    public function rewind()
50
    {
51 13
        $this->seen = [];
52 13
        parent::rewind();
53 13
    }
54
}
55