1*e6380ba3SAndreas Gohr<?php 2*e6380ba3SAndreas Gohr 3*e6380ba3SAndreas Gohrnamespace LesserPHP\Functions; 4*e6380ba3SAndreas Gohr 5*e6380ba3SAndreas Gohruse Exception; 6*e6380ba3SAndreas Gohruse LesserPHP\Utils\Color; 7*e6380ba3SAndreas Gohruse LesserPHP\Utils\Util; 8*e6380ba3SAndreas Gohr 9*e6380ba3SAndreas Gohr/** 10*e6380ba3SAndreas Gohr * Implements the string functions for LESS 11*e6380ba3SAndreas Gohr * 12*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#string-functions 13*e6380ba3SAndreas Gohr */ 14*e6380ba3SAndreas Gohrclass Strings extends AbstractFunctionCollection 15*e6380ba3SAndreas Gohr{ 16*e6380ba3SAndreas Gohr /** @inheritdoc */ 17*e6380ba3SAndreas Gohr public function getFunctions(): array 18*e6380ba3SAndreas Gohr { 19*e6380ba3SAndreas Gohr return [ 20*e6380ba3SAndreas Gohr //'escape' => [$this, 'escape'], 21*e6380ba3SAndreas Gohr 'e' => [$this, 'e'], 22*e6380ba3SAndreas Gohr '%' => [$this, 'format'], 23*e6380ba3SAndreas Gohr //'replace' => [$this, 'replace'], 24*e6380ba3SAndreas Gohr ]; 25*e6380ba3SAndreas Gohr } 26*e6380ba3SAndreas Gohr 27*e6380ba3SAndreas Gohr 28*e6380ba3SAndreas Gohr // escape is missing 29*e6380ba3SAndreas Gohr 30*e6380ba3SAndreas Gohr /** 31*e6380ba3SAndreas Gohr * String escaping. 32*e6380ba3SAndreas Gohr * 33*e6380ba3SAndreas Gohr * It expects string as a parameter and return its content as is, but without quotes. It can be used 34*e6380ba3SAndreas Gohr * to output CSS value which is either not valid CSS syntax, or uses proprietary syntax which 35*e6380ba3SAndreas Gohr * Less doesn't recognize. 36*e6380ba3SAndreas Gohr * 37*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#string-functions-e 38*e6380ba3SAndreas Gohr * @throws Exception 39*e6380ba3SAndreas Gohr */ 40*e6380ba3SAndreas Gohr public function e(array $arg): array 41*e6380ba3SAndreas Gohr { 42*e6380ba3SAndreas Gohr return $this->lessc->unwrap($arg); 43*e6380ba3SAndreas Gohr } 44*e6380ba3SAndreas Gohr 45*e6380ba3SAndreas Gohr /** 46*e6380ba3SAndreas Gohr * Formats a string 47*e6380ba3SAndreas Gohr * 48*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#string-functions--format 49*e6380ba3SAndreas Gohr * @throws Exception 50*e6380ba3SAndreas Gohr */ 51*e6380ba3SAndreas Gohr public function format(array $args) : array 52*e6380ba3SAndreas Gohr { 53*e6380ba3SAndreas Gohr if ($args[0] != 'list') return $args; 54*e6380ba3SAndreas Gohr $values = $args[2]; 55*e6380ba3SAndreas Gohr $string = array_shift($values); 56*e6380ba3SAndreas Gohr $template = $this->lessc->compileValue($this->lessc->unwrap($string)); 57*e6380ba3SAndreas Gohr 58*e6380ba3SAndreas Gohr $i = 0; 59*e6380ba3SAndreas Gohr if (preg_match_all('/%[dsa]/', $template, $m)) { 60*e6380ba3SAndreas Gohr foreach ($m[0] as $match) { 61*e6380ba3SAndreas Gohr $val = isset($values[$i]) ? 62*e6380ba3SAndreas Gohr $this->lessc->reduce($values[$i]) : ['keyword', '']; 63*e6380ba3SAndreas Gohr 64*e6380ba3SAndreas Gohr // lessjs compat, renders fully expanded color, not raw color 65*e6380ba3SAndreas Gohr if ($color = Color::coerceColor($val)) { 66*e6380ba3SAndreas Gohr $val = $color; 67*e6380ba3SAndreas Gohr } 68*e6380ba3SAndreas Gohr 69*e6380ba3SAndreas Gohr $i++; 70*e6380ba3SAndreas Gohr $rep = $this->lessc->compileValue($this->lessc->unwrap($val)); 71*e6380ba3SAndreas Gohr $template = preg_replace( 72*e6380ba3SAndreas Gohr '/' . Util::pregQuote($match) . '/', 73*e6380ba3SAndreas Gohr $rep, 74*e6380ba3SAndreas Gohr $template, 75*e6380ba3SAndreas Gohr 1 76*e6380ba3SAndreas Gohr ); 77*e6380ba3SAndreas Gohr } 78*e6380ba3SAndreas Gohr } 79*e6380ba3SAndreas Gohr 80*e6380ba3SAndreas Gohr $d = $string[0] == 'string' ? $string[1] : '"'; 81*e6380ba3SAndreas Gohr return ['string', $d, [$template]]; 82*e6380ba3SAndreas Gohr } 83*e6380ba3SAndreas Gohr 84*e6380ba3SAndreas Gohr // replace is missing 85*e6380ba3SAndreas Gohr} 86