|
1
|
|
|
<?php namespace nyx\console\output\formatting\styles; |
|
2
|
|
|
|
|
3
|
|
|
// External dependencies |
|
4
|
|
|
use nyx\core\collections; |
|
5
|
|
|
|
|
6
|
|
|
// Internal dependencies |
|
7
|
|
|
use nyx\console\output\formatting\interfaces; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Styles Map |
|
11
|
|
|
* |
|
12
|
|
|
* Items in the collection are identified by names, although those names are not set explicitly within the Style |
|
13
|
|
|
* instances themselves. |
|
14
|
|
|
* |
|
15
|
|
|
* While those names can be used by Output Formatters to determine how to style text within the respective tags, |
|
16
|
|
|
* neither the collection nor the instances run under the assumption of being used only in that context. |
|
17
|
|
|
* As such, naming them is merely a helpful means of accessing them. |
|
18
|
|
|
* |
|
19
|
|
|
* @version 0.1.0 |
|
20
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
21
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
22
|
|
|
* @link https://github.com/unyx/nyx |
|
23
|
|
|
*/ |
|
24
|
|
|
class Map extends collections\Collection |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The traits of a Styles Map. |
|
28
|
|
|
*/ |
|
29
|
|
|
use collections\traits\Collection; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns a Style identified by its name. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name The name of the Style to return. |
|
35
|
|
|
* @param mixed $default The default value to return when the given Style does not exist in the Collection. |
|
36
|
|
|
* @return interfaces\Style The Style or the default value given if the Style couldn't be found. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function get(string $name, $default = null) : ?interfaces\Style |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->items[strtolower($name)] ?? $default; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sets the given Style in the Collection. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name The name the Style should be set as. |
|
47
|
|
|
* @param mixed $value The Style to set. |
|
|
|
|
|
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function set(string $name, interfaces\Style $style) : Map |
|
51
|
|
|
{ |
|
52
|
|
|
$this->items[strtolower($name)] = $style; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Checks whether the given Style identified by its name exists in the Collection. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name The name of the Style to check for. |
|
61
|
|
|
* @return bool True if the Style exists in the Collection, false otherwise. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function has(string $name) : bool |
|
64
|
|
|
{ |
|
65
|
|
|
return isset($this->items[strtolower($name)]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks whether the given Style exists in the Collection. |
|
70
|
|
|
* |
|
71
|
|
|
* @param interfaces\Style $style The Style to search for. |
|
72
|
|
|
* @return bool True if the Style exists in the Collection, false otherwise. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function contains(interfaces\Style $style) : bool |
|
75
|
|
|
{ |
|
76
|
|
|
return empty($this->items) ? false : (null !== $this->name($style)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Removes a Style from the Collection by its name. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name The name of the Style to remove. |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function remove(string $name) : Map |
|
86
|
|
|
{ |
|
87
|
|
|
unset($this->items[strtolower($name)]); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function replace($items) : collections\interfaces\Collection |
|
96
|
|
|
{ |
|
97
|
|
|
$this->items = []; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($this->extractItems($items) as $name => $style) { |
|
100
|
|
|
$this->set($name, $style); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the name a given Style is set as within the Collection. |
|
108
|
|
|
* |
|
109
|
|
|
* @param interfaces\Style $style The Style to search for. |
|
110
|
|
|
* @return string The name of the Style or null if it couldn't be found. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function name(interfaces\Style $style) : ?string |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($this->items as $key => $value) { |
|
115
|
|
|
if ($value === $style) { |
|
116
|
|
|
return $key; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @see Map::get() |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __get(string $name) : ?interfaces\Style |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->get($name); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @see Map::set() |
|
133
|
|
|
*/ |
|
134
|
|
|
public function __set(string $name, interfaces\Style $style) : Map |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->set($name, $style); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @see Map::has() |
|
141
|
|
|
*/ |
|
142
|
|
|
public function __isset(string $name) : bool |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->has($name); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @see Map::remove() |
|
149
|
|
|
*/ |
|
150
|
|
|
public function __unset(string $name) : Map |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->remove($name); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function offsetGet($name) : interfaces\Style |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->get($name); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function offsetSet($name, $item) : Map |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->set($name, $item); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
|
|
public function offsetExists($name) : bool |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->has($name); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function offsetUnset($name) : Map |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->remove($name); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.