SortableTrait::customSortKeys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Arrayzy\Traits;
4
5
/**
6
 * Trait with helpful methods for sorting.
7
 *
8
 * @author Victor Bocharsky <[email protected]>
9
 *
10
 * @property array $elements
11
 */
12
trait SortableTrait
13
{
14
    /**
15
     * {@inheritdoc}
16
     *
17
     * @link http://php.net/manual/en/function.usort.php
18
     */
19 4
    public function customSort(callable $func)
20
    {
21 4
        usort($this->elements, $func);
22
23 4
        return $this;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @link http://php.net/manual/en/function.uksort.php
30
     */
31 4
    public function customSortKeys(callable $func)
32
    {
33 4
        uksort($this->elements, $func);
34
35 4
        return $this;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @link http://php.net/manual/en/function.arsort.php
42
     * @link http://php.net/manual/en/function.sort.php
43
     * @link http://php.net/manual/en/function.asort.php
44
     * @link http://php.net/manual/en/function.rsort.php
45
     */
46 16
    public function sort($order = SORT_ASC, $strategy = SORT_REGULAR, $preserveKeys = false)
47
    {
48 16
        $this->sorting($this->elements, $order, $strategy, $preserveKeys);
49
50 16
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     *
56
     * @link http://php.net/manual/en/function.ksort.php
57
     * @link http://php.net/manual/en/function.krsort.php
58
     */
59 8
    public function sortKeys($order = SORT_ASC, $strategy = SORT_REGULAR)
60
    {
61 8
        $this->sortingKeys($this->elements, $order, $strategy);
62
63 8
        return $this;
64
    }
65
66
    /**
67
     * @param array &$elements
68
     * @param int $order
69
     * @param int $strategy
70
     * @param bool $preserveKeys
71
     */
72 16
    protected function sorting(array &$elements, $order = SORT_ASC, $strategy = SORT_REGULAR, $preserveKeys = false)
73
    {
74
        switch ($order) {
75 16
            case SORT_DESC:
76 8
                if ($preserveKeys) {
77 4
                    arsort($elements, $strategy);
78 4
                } else {
79 4
                    rsort($elements, $strategy);
80
                }
81 8
                break;
82
83 8
            case SORT_ASC:
84 8
            default:
85 8
                if ($preserveKeys) {
86 4
                    asort($elements, $strategy);
87 4
                } else {
88 4
                    sort($elements, $strategy);
89
                }
90 8
        }
91 16
    }
92
93
    /**
94
     * @param array &$elements
95
     * @param int $order
96
     * @param int $strategy
97
     */
98 8
    protected function sortingKeys(array &$elements, $order = SORT_ASC, $strategy = SORT_REGULAR)
99
    {
100
        switch ($order) {
101 8
            case SORT_DESC:
102 4
                krsort($elements, $strategy);
103 4
                break;
104
105 4
            case SORT_ASC:
106 4
            default:
107 4
                ksort($elements, $strategy);
108 4
        }
109 8
    }
110
}
111