Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CssHandler | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
306 | |
0.00% |
0 / 1 |
| cssToArray | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
72 | |||
| arrayToCss | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\helpers; |
| 24 | |
| 25 | class CssHandler |
| 26 | { |
| 27 | /** |
| 28 | * Convert incoming CSS to CSS array |
| 29 | * This CSS must have the format generated by the online editor |
| 30 | * |
| 31 | * @param $css |
| 32 | * @return mixed |
| 33 | */ |
| 34 | public static function cssToArray($css) |
| 35 | { |
| 36 | if (!$css) { |
| 37 | return []; |
| 38 | } |
| 39 | $css = str_replace(' /* Do not edit */', '', $css); |
| 40 | $oldCssArr = explode("\n", $css); |
| 41 | $newCssArr = []; |
| 42 | foreach ($oldCssArr as $line) { |
| 43 | if (false === strpos($line, '{')) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | preg_match('~(?P<selector>[^{]+)(\{)(?P<rules>[^}]+)\}~', $line, $matches); |
| 48 | |
| 49 | foreach ($matches as $key => &$match) { |
| 50 | if (is_numeric($key)) { |
| 51 | continue; |
| 52 | } |
| 53 | $match = trim($match); |
| 54 | if ($key === 'rules') { |
| 55 | $ruleSet = array_filter(array_map('trim', explode(';', $match))); |
| 56 | $match = []; |
| 57 | foreach ($ruleSet as $rule) { |
| 58 | $rule = array_map('trim', explode(':', $rule)); |
| 59 | $match[$rule[0]] = $rule[1]; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $newCssArr[$matches['selector']] = $matches['rules']; |
| 65 | } |
| 66 | return $newCssArr; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Convert incoming CSS array to proper CSS |
| 71 | * |
| 72 | * @param $array |
| 73 | * @param $compressed boolean add break lines or not |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function arrayToCss($array, $compressed = true) |
| 77 | { |
| 78 | $css = ''; |
| 79 | |
| 80 | $break = ''; |
| 81 | $space = ''; |
| 82 | if (!$compressed) { |
| 83 | $break = "\n\t"; |
| 84 | $space = " "; |
| 85 | } |
| 86 | // rebuild CSS |
| 87 | foreach ($array as $key1 => $value1) { |
| 88 | $css .= $key1 . $space . '{'; |
| 89 | |
| 90 | foreach ($value1 as $key2 => $value2) { |
| 91 | // in the case that the code is embedded in a media query |
| 92 | if (is_array($value2)) { |
| 93 | foreach ($value2 as $value3) { |
| 94 | $css .= $break . $key2 . $space . '{'; |
| 95 | foreach ($value3 as $mProp) { |
| 96 | $css .= $break . $mProp . ':' . $value3 . ';'; |
| 97 | } |
| 98 | $css .= ($compressed) ? '}' : "\n}\n"; |
| 99 | } |
| 100 | } else { // regular selectors |
| 101 | $css .= $break . $key2 . ':' . $value2 . ';'; |
| 102 | } |
| 103 | } |
| 104 | $css .= ($compressed) ? '}' : "\n}\n"; |
| 105 | } |
| 106 | return $css; |
| 107 | } |
| 108 | } |