summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/LessRuleset.php
blob: 2e30a4b493bc6452213a0abc3f96957fbc48ab5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php

namespace ipl\Web;

use ArrayObject;
use Less_Parser;

/**
 * @extends ArrayObject<string, string>
 */
class LessRuleset extends ArrayObject
{
    /** @var ?string */
    protected $selector;

    /** @var array<LessRuleset> */
    protected $children = [];

    /**
     * Create a new LessRuleset
     *
     * @param string $selector Selector to use
     * @param array<string, string> $properties CSS properties
     *
     * @return self
     */
    public static function create(string $selector, array $properties): self
    {
        $ruleset = new static();
        $ruleset->selector = $selector;
        $ruleset->exchangeArray($properties);

        return $ruleset;
    }

    /**
     * Get the selector
     *
     * @return ?string
     */
    public function getSelector(): ?string
    {
        return $this->selector;
    }

    /**
     * Set the selector
     *
     * @param string $selector
     *
     * @return $this
     */
    public function setSelector(string $selector): self
    {
        $this->selector = $selector;

        return $this;
    }

    /**
     * Get a property value
     *
     * @param string $property Name of the property
     *
     * @return string
     */
    public function getProperty(string $property): string
    {
        return (string) $this[$property];
    }

    /**
     * Set a property
     *
     * @param string $property Name to use
     * @param string $value Value to set
     *
     * @return $this
     */
    public function setProperty(string $property, string $value): self
    {
        $this[$property] = $value;

        return $this;
    }

    /**
     * Get all properties
     *
     * @return array<string, string>
     */
    public function getProperties(): array
    {
        return $this->getArrayCopy();
    }

    /**
     * Set properties
     *
     * @param array<string, string> $properties
     *
     * @return $this
     */
    public function setProperties(array $properties): self
    {
        $this->exchangeArray($properties);

        return $this;
    }

    /**
     * Create and add a ruleset
     *
     * @param string $selector Selector to use
     * @param array<string, string> $properties CSS properties
     *
     * @return $this
     */
    public function add(string $selector, array $properties): self
    {
        $this->children[] = static::create($selector, $properties);

        return $this;
    }

    /**
     * Add a ruleset
     *
     * @param LessRuleset $ruleset
     *
     * @return $this
     */
    public function addRuleset(LessRuleset $ruleset): self
    {
        $this->children[] = $ruleset;

        return $this;
    }

    /**
     * Compile the ruleset to CSS
     *
     * @return string
     */
    public function renderCss(): string
    {
        $parser = new Less_Parser(['compress' => true]);
        $parser->parse($this->renderLess());

        return $parser->getCss();
    }

    /**
     * Render the ruleset to LESS
     *
     * @return string
     */
    protected function renderLess(): string
    {
        $less = [];

        foreach ($this as $property => $value) {
            $less[] = "$property: $value;";
        }

        foreach ($this->children as $ruleset) {
            $less[] = $ruleset->renderLess();
        }

        if ($this->selector !== null) {
            array_unshift($less, "$this->selector {");
            $less[] = '}';
        }

        return implode("\n", $less);
    }
}