|
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_INSENSITIVE = 'insensitive'; // Use case-insensitive sorting |
|
21
|
|
|
public const SORT_NATURAL = 'natural'; // Use natural sorting |
|
22
|
|
|
public const SORT_SPECIAL = 'special'; // Use natural sorting 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
|
|
|
switch ($sort) { |
|
44
|
|
|
case self::SORT_NONE: |
|
45
|
|
|
// Do not sort |
|
46
|
1 |
|
break; |
|
47
|
|
|
|
|
48
|
|
|
case self::SORT_INSENSITIVE: |
|
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
|
1 |
|
}, |
|
65
|
1 |
|
$values |
|
66
|
1 |
|
), |
|
67
|
1 |
|
$values |
|
68
|
1 |
|
); |
|
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
|
5 |
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|