1 | <?php |
||
14 | class Style |
||
15 | { |
||
16 | /** |
||
17 | * An array of Decorator objects |
||
18 | * |
||
19 | * @var Component\DecoratorInterface[] $style |
||
20 | */ |
||
21 | protected $style = []; |
||
22 | |||
23 | /** |
||
24 | * An array of the available Decorators |
||
25 | * and their corresponding class names |
||
26 | * |
||
27 | * @var array $available |
||
28 | */ |
||
29 | protected $available = [ |
||
30 | 'format' => 'Format', |
||
31 | 'color' => 'Color', |
||
32 | 'background' => 'BackgroundColor', |
||
33 | 'command' => 'Command', |
||
34 | ]; |
||
35 | |||
36 | protected $parser; |
||
37 | |||
38 | /** |
||
39 | * An array of the current styles applied |
||
40 | * |
||
41 | * @var array $current |
||
42 | */ |
||
43 | protected $current = []; |
||
44 | |||
45 | 852 | public function __construct() |
|
46 | { |
||
47 | 852 | foreach ($this->available as $key => $class) { |
|
48 | 852 | $class = 'League\CLImate\Decorator\Component\\' . $class; |
|
49 | 852 | $this->style[$key] = new $class(); |
|
50 | } |
||
51 | 852 | } |
|
52 | |||
53 | /** |
||
54 | * Get all of the styles available |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 636 | public function all() |
|
59 | { |
||
60 | 636 | $all = []; |
|
61 | |||
62 | 636 | foreach ($this->style as $style) { |
|
63 | 636 | $all = array_merge($all, $this->convertToCodes($style->all())); |
|
64 | } |
||
65 | |||
66 | 636 | return $all; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Attempt to get the corresponding code for the style |
||
71 | * |
||
72 | * @param mixed $key |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | 640 | public function get($key) |
|
77 | { |
||
78 | 640 | foreach ($this->style as $style) { |
|
79 | 640 | if ($code = $style->get($key)) { |
|
80 | 640 | return $code; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 4 | return false; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Attempt to set some aspect of the styling, |
||
89 | * return true if attempt was successful |
||
90 | * |
||
91 | * @param string $key |
||
92 | * |
||
93 | * @return boolean |
||
94 | */ |
||
95 | 112 | public function set($key) |
|
96 | { |
||
97 | 112 | foreach ($this->style as $style) { |
|
98 | 112 | if ($code = $style->set($key)) { |
|
99 | 112 | return $this->validateCode($code); |
|
100 | } |
||
101 | } |
||
102 | |||
103 | return false; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Reset the current styles applied |
||
108 | * |
||
109 | */ |
||
110 | 628 | public function reset() |
|
111 | { |
||
112 | 628 | foreach ($this->style as $style) { |
|
113 | 628 | $style->reset(); |
|
114 | } |
||
115 | 628 | } |
|
116 | |||
117 | /** |
||
118 | * Get a new instance of the Parser class based on the current settings |
||
119 | * |
||
120 | * @param \League\CLImate\Util\System\System $system |
||
121 | * |
||
122 | * @return \League\CLImate\Decorator\Parser\Parser |
||
123 | */ |
||
124 | 628 | public function parser(System $system) |
|
128 | |||
129 | /** |
||
130 | * Compile an array of the current codes |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 636 | public function current() |
|
135 | { |
||
136 | 636 | $full_current = []; |
|
137 | |||
138 | 636 | foreach ($this->style as $style) { |
|
139 | 636 | $full_current = array_merge($full_current, Helper::toArray($style->current())); |
|
140 | } |
||
141 | |||
142 | 636 | $full_current = array_filter($full_current); |
|
143 | |||
144 | 636 | return array_values($full_current); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Make sure that the code is an integer, if not let's try and get it there |
||
149 | * |
||
150 | * @param mixed $code |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | 112 | protected function validateCode($code) |
|
171 | |||
172 | /** |
||
173 | * Validate an array of codes |
||
174 | * |
||
175 | * @param array $codes |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | 4 | protected function validateCodeArray(array $codes) |
|
180 | { |
||
181 | // Loop through it and add each of the properties |
||
182 | 4 | $adds = []; |
|
183 | |||
184 | 4 | foreach ($codes as $code) { |
|
185 | 4 | $adds[] = $this->set($code); |
|
186 | } |
||
187 | |||
188 | // If any of them came back true, we're good to go |
||
189 | 4 | return in_array(true, $adds); |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Convert the array of codes to integers |
||
194 | * |
||
195 | * @param array $codes |
||
196 | * @return array |
||
197 | */ |
||
198 | 636 | protected function convertToCodes(array $codes) |
|
199 | { |
||
200 | 636 | foreach ($codes as $key => $code) { |
|
201 | 636 | if (is_int($code)) { |
|
202 | 636 | continue; |
|
203 | } |
||
204 | |||
205 | 636 | $codes[$key] = $this->getCode($code); |
|
206 | } |
||
207 | |||
208 | 636 | return $codes; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Retrieve the integers from the mixed code input |
||
213 | * |
||
214 | * @param string|array $code |
||
215 | * |
||
216 | * @return integer|array |
||
217 | */ |
||
218 | 636 | protected function getCode($code) |
|
226 | |||
227 | /** |
||
228 | * Retrieve an array of integers from the array of codes |
||
229 | * |
||
230 | * @param array $codes |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | 8 | protected function getCodeArray(array $codes) |
|
235 | { |
||
236 | 8 | foreach ($codes as $key => $code) { |
|
237 | 8 | $codes[$key] = $this->get($code); |
|
238 | } |
||
239 | |||
240 | 8 | return $codes; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Parse the add method for the style they are trying to add |
||
245 | * |
||
246 | * @param string $method |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | 32 | protected function parseAddMethod($method) |
|
254 | |||
255 | /** |
||
256 | * Add a custom style |
||
257 | * |
||
258 | * @param string $style |
||
259 | * @param string $key |
||
260 | * @param string $value |
||
261 | */ |
||
262 | 32 | protected function add($style, $key, $value) |
|
263 | { |
||
272 | |||
273 | /** |
||
274 | * Magic Methods |
||
275 | * |
||
276 | * List of possible magic methods are at the top of this class |
||
277 | * |
||
278 | * @param string $requested_method |
||
279 | * @param array $arguments |
||
280 | */ |
||
281 | 32 | public function __call($requested_method, $arguments) |
|
295 | } |
||
296 |