/** * Search and replace the css variables with the real value * * @param string $value The property value where the variable is placed * @param array $variables The defined variables */ public static function replaceVariables($value, array $variables) { if (strpos($value, '$') !== false) { $value = preg_replace_callback('/\\$([\\w-]+)/', function ($matches) use($variables) { return isset($variables[$matches[1]]) ? $variables[$matches[1]] : $matches[0]; }, $value); } $value = Parser::executeFunctions($value, 'var', function ($params) use($variables) { if (!isset($params[0])) { return 'var()'; } if (isset($variables[$params[0]])) { return $variables[$params[0]]; } return isset($params[1]) ? $params[1] : 'var(' . $params[0] . ')'; }); return $value; }
/** * Fix the different syntaxis for the linear-gradient * * @param string $value The value of the property * * @return array The linear-gradient code */ public static function webkitLinearGradient($value) { return Parser::executeFunctions($value, 'linear-gradient', function ($params) { $point = 'top'; if (preg_match('/(top|bottom|left|right|deg)/', $params[0])) { $point = array_shift($params); } switch ($point) { case 'center top': case 'top': $start = 'left top'; $end = 'left bottom'; break; case 'center bottom': case 'bottom': $start = 'left bottom'; $end = 'left top'; break; case 'left top': case 'left': $start = 'left top'; $end = 'right top'; break; case 'right top': case 'right': $start = 'right top'; $end = 'left top'; break; default: $radius = intval($point); } $color_stops = array(); $tk = count($params) - 1; foreach ($params as $k => $param) { $param = Parser::explode(' ', trim($param)); $color = $param[0]; $stop = isset($param[1]) ? $param[1] : null; if ($k === 0) { $text = 'from'; } else { if ($k === $tk) { $text = 'to'; } else { $text = 'color-stop'; } } if ($stop) { $color_stops[] = $text . '(' . $stop . ', ' . $color . ')'; } else { $color_stops[] = $text . '(' . $color . ')'; } } if (isset($radius)) { return '-webkit-gradient(linear, ' . $radius . 'deg, ' . implode(', ', $color_stops) . ')'; } else { return '-webkit-gradient(linear, ' . $start . ', ' . $end . ', ' . implode(', ', $color_stops) . ')'; } }); }
/** * Execute all css functions found in the css value * * @param callable $callback The function to execute with the css functions. If the functions returns a string, it replaces the css code. It's passed two parameters to the function with all arguments of css function and the function name. */ public function executeAllFunctions($callback) { $this->value = Parser::executeFunctions($this->value, null, $callback, $this); }