Passed
Push — main ( 87655a...3ce0a2 )
by Oscar
02:40
created

HtmlAttributesRuntime::__invoke()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 30
c 1
b 0
f 0
nc 6
nop 5
dl 0
loc 50
ccs 24
cts 24
cp 1
crap 7
rs 8.5066
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-html-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension;
13
14
use Twig\Extension\RuntimeExtensionInterface;
15
16
class HtmlAttributesRuntime implements RuntimeExtensionInterface
17
{
18
    // HTML attributes sorting criteria
19
    public const SORT_NONE = 'none';    // Do not sort
20
    public const SORT_DEFAULT = 'default'; // Use default sorting method
21
    public const SORT_NATURAL = 'natural'; // Use natural case for sorting
22
    public const SORT_SPECIAL = 'special'; // Use natural case and consider no-class as class
23
24
    /**
25
     * Sanitize HTML attributes option.
26
     *
27
     * @param string          $text      The input string to process
28
     * @param string|callable $sort      The criteria used to sort elements
29
     * @param string          $separator The separator between elements
30
     * @param string          $split     The regular expression used to extract elements
31
     * @param string          $prefix    The prefix used to negate the element (used with SORT_SPECIAL)
32
     */
33 5
    public function __invoke(
34
        string $text,
35
               $sort = self::SORT_SPECIAL,
36
        string $separator = ' ',
37
        string $split = '@\s+@s',
38
        string $prefix = 'no-'
39
    ): string {
40 5
        $values = preg_split($split, $text);
41 5
        $values = array_combine($values, $values);
42
43 5
        switch ($sort) {
44
            case self::SORT_NONE:
45
                // Do not sort
46 1
                break;
47
48
            case self::SORT_DEFAULT:
49 1
                uasort($values, 'strcasecmp');
50 1
                break;
51
52
            case self::SORT_NATURAL:
53 1
                uasort($values, 'strnatcasecmp');
54 1
                break;
55
56
            case self::SORT_SPECIAL:
57 1
                $length = mb_strlen($prefix);
58 1
                $values = array_combine(
59 1
                    array_map(
60 1
                        function (string $item) use ($prefix, $length) {
61 1
                            return strncasecmp($prefix, $item, $length)
62 1
                                ? $item
63 1
                                : mb_substr($item, $length).' '.$prefix;
64
                        },
65
                        $values
66
                    ),
67
                    $values
68
                );
69
70 1
                uksort($values, 'strnatcasecmp');
71 1
                break;
72
73
            default:
74 1
                if (\is_callable($sort)) {
75 1
                    uasort($values, $sort);
76
                }
77 1
                break;
78
        }
79
80 5
        return trim(
81 5
            implode($separator, $values),
82 5
            " \t\n\r\0\x0B".$separator
83
        );
84
    }
85
}
86