Example #1
0
 /**
  * Creates the variable. Detects the type.
  *
  * @param string $name The name of the variable
  * @param mixed $value The value of the variable
  *
  * @return Variable
  */
 public static function create($name, $value)
 {
     $important = false;
     // name is marked as !name
     if (strpos($name, '!') === 0) {
         $important = true;
         $name = substr($name, 1);
     }
     // Color
     if (Color::isNamedColor($value) || strtolower($value) === 'transparent' || strpos($value, '#') === 0) {
         $value = new ColorNode(new Color($value));
     } elseif (preg_match(self::RGBA_COLOR_REGEXP, $value, $matches)) {
         // RGB(A) colors
         $value = new ColorNode(new Color([$matches[1], $matches[2], $matches[3]], isset($matches[4]) ? $matches[4] : 1));
     } elseif (preg_match(self::QUOTED_REGEXP, $value, $matches)) {
         $value = new QuotedNode($matches[0], $matches[0][0] == '"' ? $matches[1] : $matches[2]);
     } elseif (strpos($value, 'http://') === 0 || strpos($value, 'https://') === 0) {
         $value = new AnonymousNode($value);
     } elseif (preg_match(self::DIMENSION_REGEXP, $value, $matches)) {
         $value = new DimensionNode($matches[1], isset($matches[2]) ? $matches[2] : null);
     } else {
         $value = new AnonymousNode($value);
     }
     return new self($name, $value, $important);
 }
Example #2
0
 /**
  * Convert the given node to color node
  *
  * @param Node $node The node
  * @param null $exceptionMessage ILess\Exception\Exception message if the node could not be converted to color node
  * @return ColorNode
  * @throws InvalidArgumentException If the node could not be converted to color
  */
 protected function getColorNode(Node $node, $exceptionMessage = null)
 {
     if ($node instanceof ColorNode) {
         return $node;
     }
     // this is a keyword
     if ($node instanceof KeywordNode && Color::isNamedColor($node->value)) {
         $node = new ColorNode(Color::color($node->value));
     } elseif ($node instanceof ToColorConvertibleInterface) {
         $node = $node->toColor();
     }
     if (!$node instanceof ColorNode) {
         throw new InvalidArgumentException($exceptionMessage ? $exceptionMessage : 'Cannot convert node to color');
     }
     return $node;
 }