Completed
Push — master ( c9a923...7591c0 )
by Karsten
03:22
created

SortByOperation::apply()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 24
c 1
b 0
f 1
ccs 13
cts 13
cp 1
rs 8.9713
cc 3
eloc 12
nc 1
nop 1
crap 3
1
<?php
2
/**
3
 * File was created 12.06.2015 12:38
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi\Operation\FullSet;
9
10
11
use PeekAndPoke\Component\Psi\FullSetOperation;
12
use PeekAndPoke\Component\Psi\Operation\AbstractUnaryFunctionOperation;
13
14
/**
15
 * SortByOperation
16
 *
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class SortByOperation extends AbstractUnaryFunctionOperation implements FullSetOperation
20
{
21
    /**
22
     * @param \Iterator $set
23
     *
24
     * @return \Iterator
25
     */
26 2
    public function apply(\Iterator $set)
27
    {
28 2
        $func = $this->function;
29
30 2
        $data = iterator_to_array($set);
31
32 2
        usort(
33 2
            $data,
34 2
            function ($i1, $i2) use ($func) {
35
36 2
                $val1 = $func($i1);
37 2
                $val2 = $func($i2);
38
39 2
                if ($val1 === $val2) {
40 2
                    return null;
41
                }
42
43 2
                return $val1 > $val2 ? 1 : -1;
44
            }
45 2
        );
46
47 2
        return new \ArrayIterator($data);
48
49
    }
50
}
51