1<?php
2
3namespace LesserPHP\Functions;
4
5use LesserPHP\Utils\Asserts;
6use LesserPHP\Utils\Color;
7use LesserPHP\Utils\Util;
8
9/**
10 * Implements the type functions for LESS
11 *
12 * @link https://lesscss.org/functions/#type-functions
13 */
14class Type extends AbstractFunctionCollection
15{
16    /** @inheritdoc  */
17    public function getFunctions(): array
18    {
19        return [
20            'isnumber' => [$this, 'isnumber'],
21            'isstring' => [$this, 'isstring'],
22            'iscolor' => [$this, 'iscolor'],
23            'iskeyword' => [$this, 'iskeyword'],
24            'isurl' => [$this, 'isurl'],
25            'ispixel' => [$this, 'ispixel'],
26            'isem' => [$this, 'isem'],
27            'isrem' => [$this, 'isrem'],
28            'ispercentage' => [$this, 'ispercentage'],
29            'isunit' => [$this, 'isunit'],
30            //'isruleset' => [$this, 'isruleset'],
31            //'isdefined' => [$this, 'isdefined'],
32        ];
33    }
34
35
36    /**
37     * Returns true if a value is a number, false otherwise
38     *
39     * @link https://lesscss.org/functions/#type-functions-isnumber
40     */
41    public function isnumber(array $value): array
42    {
43        return Util::toBool($value[0] == 'number');
44    }
45
46    /**
47     * Returns true if a value is a string, false otherwise
48     *
49     * @link https://lesscss.org/functions/#type-functions-isstring
50     */
51    public function isstring(array $value): array
52    {
53        return Util::toBool($value[0] == 'string');
54    }
55
56    /**
57     * Returns true if a value is a color, false otherwise
58     *
59     * @link https://lesscss.org/functions/#type-functions-iscolor
60     */
61    public function iscolor(array $value): array
62    {
63        return Util::toBool(Color::coerceColor($value));
64    }
65
66    /**
67     * Returns true if a value is a keyword, false otherwise
68     *
69     * @link https://lesscss.org/functions/#type-functions-iskeyword
70     */
71    public function iskeyword(array $value): array
72    {
73        return Util::toBool($value[0] == 'keyword');
74    }
75
76    /**
77     * Returns true if a value is a url, false otherwise
78     *
79     * @link https://lesscss.org/functions/#type-functions-isurl
80     */
81    public function isurl(array $value): array
82    {
83        return Util::toBool($value[0] == 'function' && $value[1] == 'url');
84    }
85
86    /**
87     * Returns true if a value is a number in pixels, false otherwise
88     *
89     * @link https://lesscss.org/functions/#type-functions-ispixel
90     */
91    public function ispixel(array $value): array
92    {
93        return Util::toBool($value[0] == 'number' && $value[2] == 'px');
94    }
95
96    /**
97     * Returns true if a value is an em value, false otherwise
98     *
99     * @link https://lesscss.org/functions/#type-functions-isem
100     */
101    public function isem(array $value): array
102    {
103        return Util::toBool($value[0] == 'number' && $value[2] == 'em');
104    }
105
106    /**
107     * Returns true if a value is an rem value, false otherwise
108     *
109     * This method does not exist in the official less.js implementation
110     */
111    public function isrem(array $value): array
112    {
113        return Util::toBool($value[0] == 'number' && $value[2] == 'rem');
114    }
115
116    /**
117     * Returns true if a value is a percentage, false otherwise
118     *
119     * @link https://lesscss.org/functions/#type-functions-ispercentage
120     */
121    public function ispercentage(array $value): array
122    {
123        return Util::toBool($value[0] == 'number' && $value[2] == '%');
124    }
125
126    /**
127     * Returns true if a value is a number with a given unit, false otherwise
128     *
129     * @link https://lesscss.org/functions/#type-functions-isunit
130     */
131    public function isunit(array $args): array
132    {
133        [$input, $unit] = Asserts::assertArgs($args, 2, 'isunit');
134        $unit = $this->lessc->compileValue($this->lessc->unwrap($unit));
135
136        return Util::toBool(
137            $input[0] == 'number' &&
138            $input[2] == $unit
139        );
140    }
141
142    // isruleset is missing
143    // isdefined is missing
144}
145