Sorter::default()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 11
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 9
rs 8.0555
1
<?php
2
/*
3
 * This file is part of the "andrey-helldar/support" project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Andrey Helldar <[email protected]>
9
 *
10
 * @copyright 2021 Andrey Helldar
11
 *
12
 * @license MIT
13
 *
14
 * @see https://github.com/andrey-helldar/support
15
 */
16
17
namespace Helldar\Support\Callbacks;
18
19
use Helldar\Support\Facades\Helpers\Str;
20
21
class Sorter
22
{
23
    protected $special_chars = [
24
        ' ',
25
        '*',
26
        '-',
27
        '_',
28
        '—',
29
        '=',
30
        '\\',
31
        '/',
32
        '|',
33
        '~',
34
        '`',
35
        '+',
36
        ':',
37
        ';',
38
        '@',
39
        '#',
40
        '$',
41
        '%',
42
        '^',
43
        '&',
44
        '?',
45
        '!',
46
        '(',
47
        ')',
48
        '{',
49
        '}',
50
        '[',
51
        ']',
52
        '§',
53
        '№',
54
        '<',
55
        '>',
56
        '.',
57
        ',',
58
    ];
59
60
    /**
61
     * Gets an array of special characters.
62
     *
63
     * @return string[]
64
     */
65 16
    public function specialChars(): array
66
    {
67 16
        return $this->special_chars;
68
    }
69
70
    /**
71
     * Gets a callback function for sorting.
72
     *
73
     * @return callable
74
     */
75 18
    public function default(): callable
76
    {
77 18
        return function ($current, $next) {
78 18
            $current = $this->lower($current);
79 18
            $next    = $this->lower($next);
80
81 18
            if ($current === $next) {
82 2
                return 0;
83
            }
84
85 18
            if (is_string($current) && is_numeric($next)) {
86 12
                return $this->hasSpecialChar($current) ? -1 : 1;
87
            }
88
89 18
            if (is_numeric($current) && is_string($next)) {
90 12
                return $this->hasSpecialChar($next) ? 1 : -1;
91
            }
92
93 18
            return $current < $next ? -1 : 1;
94 18
        };
95
    }
96
97
    /**
98
     * Prepares a value for case-insensitive sorting.
99
     *
100
     * @param $value
101
     *
102
     * @return mixed|string|null
103
     */
104 18
    protected function lower($value)
105
    {
106 18
        return is_string($value) ? Str::lower($value) : $value;
107
    }
108
109
    /**
110
     * Determine if a value is a special character.
111
     *
112
     * @param $value
113
     *
114
     * @return bool
115
     */
116 12
    protected function hasSpecialChar($value): bool
117
    {
118 12
        return in_array($value, $this->specialChars());
119
    }
120
}
121