1*e6380ba3SAndreas Gohr<?php 2*e6380ba3SAndreas Gohr 3*e6380ba3SAndreas Gohrnamespace LesserPHP\Functions; 4*e6380ba3SAndreas Gohr 5*e6380ba3SAndreas Gohruse Exception; 6*e6380ba3SAndreas Gohruse LesserPHP\Utils\Asserts; 7*e6380ba3SAndreas Gohruse LesserPHP\Utils\Util; 8*e6380ba3SAndreas Gohr 9*e6380ba3SAndreas Gohr/** 10*e6380ba3SAndreas Gohr * Implements the miscellaneous functions for LESS 11*e6380ba3SAndreas Gohr * 12*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#misc-functions 13*e6380ba3SAndreas Gohr */ 14*e6380ba3SAndreas Gohrclass Misc 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 //'color' => [$this, 'color'], 21*e6380ba3SAndreas Gohr //'image-size' => [$this, 'imageSize'], 22*e6380ba3SAndreas Gohr //'image-width' => [$this, 'imageWidth'], 23*e6380ba3SAndreas Gohr //'image-height' => [$this, 'imageHeight'], 24*e6380ba3SAndreas Gohr 'convert' => [$this, 'convert'], 25*e6380ba3SAndreas Gohr 'data-uri' => [$this, 'dataUri'], 26*e6380ba3SAndreas Gohr //'default' => [$this, 'default'], 27*e6380ba3SAndreas Gohr 'unit' => [$this, 'unit'], 28*e6380ba3SAndreas Gohr //'get-unit' => [$this, 'getUnit'], 29*e6380ba3SAndreas Gohr //'svg-gradient' => [$this, 'svgGradient'], 30*e6380ba3SAndreas Gohr ]; 31*e6380ba3SAndreas Gohr } 32*e6380ba3SAndreas Gohr 33*e6380ba3SAndreas Gohr // color is missing 34*e6380ba3SAndreas Gohr // image-size is missing 35*e6380ba3SAndreas Gohr // image-width is missing 36*e6380ba3SAndreas Gohr // image-height is missing 37*e6380ba3SAndreas Gohr 38*e6380ba3SAndreas Gohr /** 39*e6380ba3SAndreas Gohr * Convert a number from one unit into another 40*e6380ba3SAndreas Gohr * 41*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#misc-functions-convert 42*e6380ba3SAndreas Gohr * @throws Exception 43*e6380ba3SAndreas Gohr */ 44*e6380ba3SAndreas Gohr public function convert(array $args): array 45*e6380ba3SAndreas Gohr { 46*e6380ba3SAndreas Gohr [$value, $to] = Asserts::assertArgs($args, 2, 'convert'); 47*e6380ba3SAndreas Gohr 48*e6380ba3SAndreas Gohr // If it's a keyword, grab the string version instead 49*e6380ba3SAndreas Gohr if (is_array($to) && $to[0] == 'keyword') { 50*e6380ba3SAndreas Gohr $to = $to[1]; 51*e6380ba3SAndreas Gohr } 52*e6380ba3SAndreas Gohr 53*e6380ba3SAndreas Gohr return Util::convert($value, $to); 54*e6380ba3SAndreas Gohr } 55*e6380ba3SAndreas Gohr 56*e6380ba3SAndreas Gohr /** 57*e6380ba3SAndreas Gohr * Given an url, decide whether to output a regular link or the base64-encoded contents of the file 58*e6380ba3SAndreas Gohr * 59*e6380ba3SAndreas Gohr * @param array $value either an argument list (two strings) or a single string 60*e6380ba3SAndreas Gohr * @return string formatted url(), either as a link or base64-encoded 61*e6380ba3SAndreas Gohr */ 62*e6380ba3SAndreas Gohr public function dataUri(array $value): string 63*e6380ba3SAndreas Gohr { 64*e6380ba3SAndreas Gohr $mime = ($value[0] === 'list') ? $value[2][0][2] : null; 65*e6380ba3SAndreas Gohr $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; 66*e6380ba3SAndreas Gohr 67*e6380ba3SAndreas Gohr $fullpath = $this->lessc->findImport($url); 68*e6380ba3SAndreas Gohr 69*e6380ba3SAndreas Gohr if ($fullpath && ($fsize = filesize($fullpath)) !== false) { 70*e6380ba3SAndreas Gohr // IE8 can't handle data uris larger than 32KB 71*e6380ba3SAndreas Gohr if ($fsize / 1024 < 32) { 72*e6380ba3SAndreas Gohr if (is_null($mime)) { 73*e6380ba3SAndreas Gohr if (class_exists('finfo')) { // php 5.3+ 74*e6380ba3SAndreas Gohr $finfo = new \finfo(FILEINFO_MIME); 75*e6380ba3SAndreas Gohr $mime = explode('; ', $finfo->file($fullpath)); 76*e6380ba3SAndreas Gohr $mime = $mime[0]; 77*e6380ba3SAndreas Gohr } elseif (function_exists('mime_content_type')) { // PHP 5.2 78*e6380ba3SAndreas Gohr $mime = mime_content_type($fullpath); 79*e6380ba3SAndreas Gohr } 80*e6380ba3SAndreas Gohr } 81*e6380ba3SAndreas Gohr 82*e6380ba3SAndreas Gohr if (!is_null($mime)) // fallback if the mime type is still unknown 83*e6380ba3SAndreas Gohr $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); 84*e6380ba3SAndreas Gohr } 85*e6380ba3SAndreas Gohr } 86*e6380ba3SAndreas Gohr 87*e6380ba3SAndreas Gohr return 'url("' . $url . '")'; 88*e6380ba3SAndreas Gohr } 89*e6380ba3SAndreas Gohr 90*e6380ba3SAndreas Gohr // default is missing 91*e6380ba3SAndreas Gohr 92*e6380ba3SAndreas Gohr 93*e6380ba3SAndreas Gohr /** 94*e6380ba3SAndreas Gohr * Remove or change the unit of a dimension 95*e6380ba3SAndreas Gohr * 96*e6380ba3SAndreas Gohr * @link https://lesscss.org/functions/#misc-functions-unit 97*e6380ba3SAndreas Gohr * @throws Exception 98*e6380ba3SAndreas Gohr */ 99*e6380ba3SAndreas Gohr public function unit(array $arg): array 100*e6380ba3SAndreas Gohr { 101*e6380ba3SAndreas Gohr if ($arg[0] == 'list') { 102*e6380ba3SAndreas Gohr [$number, $newUnit] = $arg[2]; 103*e6380ba3SAndreas Gohr return [ 104*e6380ba3SAndreas Gohr 'number', 105*e6380ba3SAndreas Gohr Asserts::assertNumber($number), 106*e6380ba3SAndreas Gohr $this->lessc->compileValue($this->lessc->unwrap($newUnit)) 107*e6380ba3SAndreas Gohr ]; 108*e6380ba3SAndreas Gohr } else { 109*e6380ba3SAndreas Gohr return ['number', Asserts::assertNumber($arg), '']; 110*e6380ba3SAndreas Gohr } 111*e6380ba3SAndreas Gohr } 112*e6380ba3SAndreas Gohr 113*e6380ba3SAndreas Gohr // get-unit is missing 114*e6380ba3SAndreas Gohr // svg-gradient is missing 115*e6380ba3SAndreas Gohr} 116