Lines
17.64%
6 / 34
Functions and Methods
20.00%
1 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| TwigExtensionsAndFilter | 17.64% | 6 / 34 | 20.00% | 1 / 5 | 267.31 | 0.00% | 0 / 1 | |||
| getFilters | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | |||||
| decodeEntities | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 6 | |||||
| msort | 0.00% | 0 / 17 | 0.00% | 0 / 1 | 110 | |||||
| getObjectName | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 42 | |||||
| sanitizeHtml | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | /** | |
| 4 | * @package BO Slim | |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG | |
| 6 | **/ | |
| 7 | ||
| 8 | namespace BO\Slim; | |
| 9 | ||
| 10 | /** | |
| 11 | * Extension for Twig and Slim | |
| 12 | * | |
| 13 | */ | |
| 14 | class TwigExtensionsAndFilter extends TwigExtension | |
| 15 | { | |
| 16 | #[\Override] | |
| 17 | public function getFilters(): array | |
| 18 | { | |
| 19 | return array( | |
| 20 | new \Twig\TwigFilter('msort', array($this, 'msort')), | |
| 21 | new \Twig\TwigFilter('getObjectName', array($this, 'getObjectName')), | |
| 22 | new \Twig\TwigFilter('decodeEntities', array($this, 'decodeEntities')), | |
| 23 | new \Twig\TwigFilter('sanitize_html', array($this, 'sanitizeHtml'), ['is_safe' => ['html']]) | |
| 24 | ); | |
| 25 | } | |
| 26 | ||
| 27 | public function decodeEntities(mixed $string): string | |
| 28 | { | |
| 29 | return $string === null ? '' : trim(nl2br(html_entity_decode((string) $string))); | |
| 30 | } | |
| 31 | ||
| 32 | public function msort(mixed $array, mixed $key, int $sort_flags = SORT_REGULAR): mixed | |
| 33 | { | |
| 34 | if (is_array($array) && count($array) > 0) { | |
| 35 | if ($key !== null && $key !== '' && $key !== []) { | |
| 36 | $mapping = array(); | |
| 37 | foreach ($array as $k => $v) { | |
| 38 | $sort_key = ''; | |
| 39 | if (!is_array($key)) { | |
| 40 | $sort_key = $v[$key]; | |
| 41 | } else { | |
| 42 | // @TODO This should be fixed, now it will be sorted as string | |
| 43 | foreach ($key as $key_key) { | |
| 44 | $sort_key .= $v[$key_key]; | |
| 45 | } | |
| 46 | $sort_flags = SORT_STRING; | |
| 47 | } | |
| 48 | $mapping[$k] = $sort_key; | |
| 49 | } | |
| 50 | asort($mapping, $sort_flags); | |
| 51 | $sorted = array(); | |
| 52 | foreach (array_keys($mapping) as $k) { | |
| 53 | $sorted[] = $array[$k]; | |
| 54 | } | |
| 55 | return $sorted; | |
| 56 | } | |
| 57 | } | |
| 58 | return $array; | |
| 59 | } | |
| 60 | ||
| 61 | public function getObjectName(mixed $object): string | |
| 62 | { | |
| 63 | if (is_object($object)) { | |
| 64 | return (new \ReflectionClass($object))->getShortName(); | |
| 65 | } | |
| 66 | if (is_string($object) && (class_exists($object) || interface_exists($object) || trait_exists($object))) { | |
| 67 | return (new \ReflectionClass($object))->getShortName(); | |
| 68 | } | |
| 69 | return ''; | |
| 70 | } | |
| 71 | ||
| 72 | public function sanitizeHtml(mixed $html): string | |
| 73 | { | |
| 74 | static $purifier = null; | |
| 75 | if ($purifier === null) { | |
| 76 | $config = \HTMLPurifier_Config::createDefault(); | |
| 77 | $purifier = new \HTMLPurifier($config); | |
| 78 | } | |
| 79 | return $purifier->purify((string) $html); | |
| 80 | } | |
| 81 | } |