UniqueByOperation::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * File was created 06.05.2015 22:14
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi\Operation\FullSet;
9
10
use PeekAndPoke\Component\Psi\BinaryFunction;
11
use PeekAndPoke\Component\Psi\FullSetOperation;
12
use PeekAndPoke\Component\Psi\UnaryFunction;
13
14
/**
15
 * @author Karsten J. Gerber <[email protected]>
16
 */
17
class UniqueByOperation implements FullSetOperation
18
{
19
    /** @var callable|UnaryFunction|BinaryFunction */
20
    protected $mapper;
21
    /** @var bool */
22
    private $strict;
23
24
    /**
25
     * @param callable|UnaryFunction|BinaryFunction $mapper Mapper function
26
     * @param bool                                  $strict Do strict comparison
27
     */
28 18
    public function __construct($mapper, $strict)
29
    {
30 18
        $this->mapper = $mapper;
31 18
        $this->strict = $strict;
32 18
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 18
    public function apply(\Iterator $set)
38
    {
39 18
        $map    = $this->mapper;
40 18
        $result = [];
41 18
        $known  = [];
42
43 18
        foreach ($set as $item) {
44
45 16
            $mapped = $map($item);
46
47 16
            if (! in_array($mapped, $known, $this->strict)) {
48 16
                $known[]  = $mapped;
49 16
                $result[] = $item;
50
            }
51
        }
52
53 18
        return new \ArrayIterator($result);
54
    }
55
}
56