1+ <?php
2+
3+ namespace Irmmr \RTLCss ;
4+
5+ use Irmmr \RTLCss \Encoder \ColorFuncsEncoder ;
6+ use Irmmr \RTLCss \Encoder \Encoder ;
7+
8+ /**
9+ * class Encode
10+ *
11+ * PHP-CSS-Parser have many problems with using css4 or other
12+ * parsing values.
13+ * This class is an unreliable class that tries to keep away items
14+ * that the CSS parser cannot parse or make some changes to the original
15+ * code so that it can be parsed.
16+ * ! It is better not to use this class at all.
17+ *
18+ * @package Irmmr\RTLCss
19+ */
20+ class Encode
21+ {
22+ /**
23+ * code holder
24+ * @var string
25+ */
26+ protected string $ code = '' ;
27+
28+ /**
29+ * encoded code
30+ * @var string
31+ */
32+ protected string $ encoded = '' ;
33+
34+ /**
35+ * all encoder classes name
36+ *
37+ * @var array
38+ */
39+ protected array $ encoders = [
40+ 'color-funcs ' => ColorFuncsEncoder::class
41+ ];
42+
43+ /**
44+ * all encoder classes (created)
45+ *
46+ * @var array
47+ */
48+ protected array $ classes = [];
49+
50+ /**
51+ * class constructor
52+ */
53+ public function __construct (string $ code )
54+ {
55+ $ this ->code = $ code ;
56+ }
57+
58+ /**
59+ * set encoded code
60+ */
61+ public function setEncoded (string $ encoded ): void
62+ {
63+ foreach ($ this ->classes as $ id => $ cls ) {
64+ if (!$ cls instanceof Encoder) {
65+ continue ;
66+ }
67+
68+ $ cls ->setEncoded ($ encoded );
69+ }
70+ }
71+
72+ /**
73+ * encode whole code using encode
74+ *
75+ * @return string
76+ */
77+ public function encode (): string
78+ {
79+ if (empty ($ this ->encoded )) {
80+ $ this ->encoded = $ this ->code ;
81+ }
82+
83+ foreach ($ this ->encoders as $ id => $ encoder ) {
84+ $ enc = $ this ->classes [$ id ] = new $ encoder ($ this ->encoded );
85+
86+ $ this ->encoded = $ enc ->encode ();
87+ }
88+
89+ return $ this ->encoded ;
90+ }
91+
92+ /**
93+ * decode whole code using decode
94+ *
95+ * @return string
96+ */
97+ public function decode (): string
98+ {
99+ $ decoded = $ this ->encoded ;
100+
101+ foreach ($ this ->classes as $ id => $ cls ) {
102+ if (!$ cls instanceof Encoder) {
103+ continue ;
104+ }
105+
106+ $ decoded = $ cls ->decode ();
107+ }
108+
109+ return $ decoded ;
110+ }
111+ }
0 commit comments