Completed
Push — master ( 01762c...256c3c )
by Karsten
02:12
created

SortByOperation::apply()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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