Total Complexity | 13 |
Total Lines | 98 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
7 | class Sorter |
||
8 | { |
||
9 | protected $special_chars = [ |
||
10 | ' ', |
||
11 | '*', |
||
12 | '-', |
||
13 | '_', |
||
14 | '—', |
||
15 | '=', |
||
16 | '\\', |
||
17 | '/', |
||
18 | '|', |
||
19 | '~', |
||
20 | '`', |
||
21 | '+', |
||
22 | ':', |
||
23 | ';', |
||
24 | '@', |
||
25 | '#', |
||
26 | '$', |
||
27 | '%', |
||
28 | '^', |
||
29 | '&', |
||
30 | '?', |
||
31 | '!', |
||
32 | '(', |
||
33 | ')', |
||
34 | '{', |
||
35 | '}', |
||
36 | '[', |
||
37 | ']', |
||
38 | '§', |
||
39 | '№', |
||
40 | '<', |
||
41 | '>', |
||
42 | '.', |
||
43 | ',', |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Gets an array of special characters. |
||
48 | * |
||
49 | * @return string[] |
||
50 | */ |
||
51 | public function specialChars(): array |
||
52 | { |
||
53 | return $this->special_chars; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Gets a callback function for sorting. |
||
58 | * |
||
59 | * @return callable |
||
60 | */ |
||
61 | public function default(): callable |
||
62 | { |
||
63 | return function ($current, $next) { |
||
64 | $current = $this->lower($current); |
||
65 | $next = $this->lower($next); |
||
66 | |||
67 | if ($current === $next) { |
||
68 | return 0; |
||
69 | } |
||
70 | |||
71 | if (is_string($current) && is_numeric($next)) { |
||
72 | return $this->hasSpecialChar($current) ? -1 : 1; |
||
73 | } |
||
74 | |||
75 | if (is_numeric($current) && is_string($next)) { |
||
76 | return $this->hasSpecialChar($next) ? 1 : -1; |
||
77 | } |
||
78 | |||
79 | return $current < $next ? -1 : 1; |
||
80 | }; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Prepares a value for case-insensitive sorting. |
||
85 | * |
||
86 | * @param $value |
||
87 | * |
||
88 | * @return mixed|string|null |
||
89 | */ |
||
90 | protected function lower($value) |
||
91 | { |
||
92 | return is_string($value) ? Str::lower($value) : $value; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Determine if a value is a special character. |
||
97 | * |
||
98 | * @param $value |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | protected function hasSpecialChar($value): bool |
||
105 | } |
||
106 | } |
||
107 |