Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
20.00% |
6 / 30 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TwigExtensionsAndFilter | |
20.00% |
6 / 30 |
|
20.00% |
1 / 5 |
114.35 | |
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 |
72 | |||
| getObjectName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | public function getFilters() |
| 17 | { |
| 18 | return array( |
| 19 | new \Twig\TwigFilter('msort', array($this, 'msort')), |
| 20 | new \Twig\TwigFilter('getObjectName', array($this, 'getObjectName')), |
| 21 | new \Twig\TwigFilter('decodeEntities', array($this, 'decodeEntities')), |
| 22 | new \Twig\TwigFilter('sanitize_html', array($this, 'sanitizeHtml'), ['is_safe' => ['html']]) |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | public function decodeEntities($string) |
| 27 | { |
| 28 | return $string === null ? '' : trim(nl2br(html_entity_decode($string))); |
| 29 | } |
| 30 | |
| 31 | public function msort($array, $key, $sort_flags = SORT_REGULAR) |
| 32 | { |
| 33 | if (is_array($array) && count($array) > 0) { |
| 34 | if (!empty($key)) { |
| 35 | $mapping = array(); |
| 36 | foreach ($array as $k => $v) { |
| 37 | $sort_key = ''; |
| 38 | if (!is_array($key)) { |
| 39 | $sort_key = $v[$key]; |
| 40 | } else { |
| 41 | // @TODO This should be fixed, now it will be sorted as string |
| 42 | foreach ($key as $key_key) { |
| 43 | $sort_key .= $v[$key_key]; |
| 44 | } |
| 45 | $sort_flags = SORT_STRING; |
| 46 | } |
| 47 | $mapping[$k] = $sort_key; |
| 48 | } |
| 49 | asort($mapping, $sort_flags); |
| 50 | $sorted = array(); |
| 51 | foreach ($mapping as $k => $v) { |
| 52 | $sorted[] = $array[$k]; |
| 53 | } |
| 54 | return $sorted; |
| 55 | } |
| 56 | } |
| 57 | return $array; |
| 58 | } |
| 59 | |
| 60 | public function getObjectName($object) |
| 61 | { |
| 62 | return (new \ReflectionClass($object))->getShortName(); |
| 63 | } |
| 64 | |
| 65 | public function sanitizeHtml($html) |
| 66 | { |
| 67 | static $purifier = null; |
| 68 | if ($purifier === null) { |
| 69 | $config = \HTMLPurifier_Config::createDefault(); |
| 70 | $purifier = new \HTMLPurifier($config); |
| 71 | } |
| 72 | return $purifier->purify($html); |
| 73 | } |
| 74 | } |