ArraySorter::applySortingByKeys()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
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 15
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Collections\Interactors;
6
7
use CCT\Component\Collections\CollectionInterface;
8
9
class ArraySorter extends AbstractInteractor
10
{
11
    /**
12
     * Sort the elements.
13
     *
14
     * @param int  $order           Possible values: SORT_ASC or SORT_DESC
15
     * @param int  $strategy        @see http://php.net/manual/en/function.sort.php#refsect1-function.sort-parameters
16
     * @param bool $preserveKeys    Preserve or not the keys
17
     *
18
     * @return array|CollectionInterface
19
     */
20 6
    public function sort(int $order = SORT_ASC, int $strategy = SORT_REGULAR, $preserveKeys = false): array
21
    {
22 6
        $this->applySorting($order, $strategy, $preserveKeys);
23
24 5
        return $this->elements;
25
    }
26
27
    /**
28
     * Sort the elements by keys.
29
     *
30
     * @param int $order        Possible values: SORT_ASC or SORT_DESC
31
     * @param int $strategy     @see http://php.net/manual/en/function.sort.php#refsect1-function.sort-parameters
32
     *
33
     * @return array|CollectionInterface
34
     */
35 3
    public function sortByKeys(int $order = SORT_ASC, int $strategy = SORT_REGULAR): array
36
    {
37 3
        $this->applySortingByKeys($order, $strategy);
38
39 2
        return $this->elements;
40
    }
41
42
    /**
43
     * Shuffle the array and return the result.
44
     *
45
     * @return array|CollectionInterface
46
     */
47 2
    public function shuffle(): array
48
    {
49 2
        shuffle($this->elements);
50
51 2
        return $this->elements;
52
    }
53
54 6
    private function applySorting($order = SORT_ASC, $strategy = SORT_REGULAR, $preserveKeys = false): void
55
    {
56 6
        if (SORT_ASC === $order) {
57 3
            $this->sortByAscendingOrder($strategy, $preserveKeys);
58 3
            return;
59
        }
60
61 3
        if (SORT_DESC === $order) {
62 2
            $this->sortByDescendingOrder($strategy, $preserveKeys);
63 2
            return;
64
        }
65
66 1
        throw new \InvalidArgumentException(sprintf(
67 1
            'The order "%s" given is invalid. The accepted orders are: SORT_ASC or SORT_DESC',
68 1
            $order
69
        ));
70
    }
71
72 3
    private function sortByAscendingOrder($strategy, $preserveKeys): void
73
    {
74 3
        if (true === $preserveKeys) {
75 1
            asort($this->elements, $strategy);
76 1
            return;
77
        }
78
79 2
        sort($this->elements, $strategy);
80 2
    }
81
82 2
    private function sortByDescendingOrder($strategy, $preserveKeys): void
83
    {
84 2
        if (true === $preserveKeys) {
85 1
            arsort($this->elements, $strategy);
86 1
            return;
87
        }
88
89 1
        rsort($this->elements, $strategy);
90 1
    }
91
92 3
    private function applySortingByKeys($order = SORT_ASC, $strategy = SORT_REGULAR): void
93
    {
94 3
        if (SORT_ASC === $order) {
95 1
            ksort($this->elements, $strategy);
96 1
            return;
97
        }
98
99 2
        if (SORT_DESC === $order) {
100 1
            krsort($this->elements, $strategy);
101 1
            return;
102
        }
103
104 1
        throw new \InvalidArgumentException(sprintf(
105 1
            'The order "%s" given is invalid. The accepted orders are: SORT_ASC or SORT_DESC',
106 1
            $order
107
        ));
108
    }
109
}
110