1 | <?php |
||
36 | class AttributeManager { |
||
37 | |||
38 | /** |
||
39 | * This introduces aliases for attributes. |
||
40 | * |
||
41 | * For instance, if someone adds "header" to its component, it is treated like "heading" if this is not present itself. |
||
42 | */ |
||
43 | const ALIASES = [ |
||
44 | 'heading' => 'header', |
||
45 | 'footer' => 'footing' |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * For attributes that take any value. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | const ANY_VALUE = 1; |
||
54 | |||
55 | /** |
||
56 | * For attributes that can be set to false by supplying one of certain values. |
||
57 | * Usually uses for flag-attributes like "active", "collapsible", etc. |
||
58 | * |
||
59 | * @see \BootstrapComponents\AttributeManager::$noValues |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | const NO_FALSE_VALUE = 0; |
||
64 | |||
65 | /** |
||
66 | * Holds the register for allowed attributes per component |
||
67 | * |
||
68 | * @var array $allowedValuesForAttribute |
||
69 | */ |
||
70 | private $allowedValuesForAttribute; |
||
71 | |||
72 | /** |
||
73 | * Holds all values indicating a "no". Can be used to ignore "enable"-fields. |
||
74 | * |
||
75 | * @var array $noValues |
||
76 | */ |
||
77 | private $noValues; |
||
78 | |||
79 | /** |
||
80 | * The list of attributes that are considered valid |
||
81 | * |
||
82 | * @var string[] $validAttributes |
||
83 | */ |
||
84 | private $validAttributes; |
||
85 | |||
86 | /** |
||
87 | * AttributeManager constructor. |
||
88 | * |
||
89 | * Do not instantiate directly, but use {@see ApplicationFactory::getAttributeManager} |
||
90 | * instead. |
||
91 | * |
||
92 | * @param string[] $validAttributes the list of attributes, this manager deems valid. |
||
93 | * |
||
94 | * @see ApplicationFactory::getAttributeManager |
||
95 | */ |
||
96 | 49 | public function __construct( $validAttributes ) { |
|
101 | |||
102 | /** |
||
103 | * Returns the list of all available attributes |
||
104 | * |
||
105 | * @return string[] |
||
106 | */ |
||
107 | 1 | public function getAllKnownAttributes() { |
|
110 | |||
111 | /** |
||
112 | * Returns the allowed values for a given attribute or NULL if invalid attribute. |
||
113 | * |
||
114 | * Note that allowed values can be an array, {@see AttributeManager::NO_FALSE_VALUE}, |
||
115 | * or {@see AttributeManager::ANY_VALUE}. |
||
116 | * |
||
117 | * @param string $attribute |
||
118 | * |
||
119 | * @return null|array|bool |
||
120 | */ |
||
121 | 44 | public function getAllowedValuesFor( $attribute ) { |
|
127 | |||
128 | /** |
||
129 | * @return string[] |
||
130 | */ |
||
131 | 27 | public function getValidAttributes() { |
|
134 | |||
135 | /** |
||
136 | * Checks if given $attribute is registered with the manager. |
||
137 | * |
||
138 | * @param string $attribute |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 44 | public function isRegistered( $attribute ) { |
|
145 | |||
146 | /** |
||
147 | * Registers $attribute with and its allowed values. |
||
148 | * |
||
149 | * Notes on attribute registering: |
||
150 | * * {@see AttributeManager::ANY_VALUE}: every non empty string is allowed |
||
151 | * * {@see AttributeManager::NO_FALSE_VALUE}: as along as the attribute is present and NOT set to a value contained in {@see AttributeManager::$noValues}, |
||
152 | * the attribute is considered valid. Note that flag attributes will be set to the empty string by the parser, e.g. having <tag active></tag> will have |
||
153 | * active set to "". {@see AttributeManager::verifyValueForAttribute} returns those as true. |
||
154 | * * array: attribute must be present and contain a value in the array to be valid |
||
155 | * |
||
156 | * Note also, that values will be converted to lower case before checking, you therefore should only put lower case values in your |
||
157 | * allowed-values array. |
||
158 | * |
||
159 | * @param string $attribute |
||
160 | * @param array|int $allowedValues |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | 4 | public function register( $attribute, $allowedValues ) { |
|
174 | |||
175 | /** |
||
176 | * Takes the attributes/options supplied by parser, removes the ones not registered for this component and |
||
177 | * verifies the rest. Note that the result array has an entry for every valid attribute, false if not supplied via parser. |
||
178 | * |
||
179 | * Valid here means: the attribute is registered with the manager and with the component |
||
180 | * Verified: The attributes value has been checked and deemed ok. |
||
181 | * |
||
182 | * Note that attributes not registered with the manager return with a false value. |
||
183 | * |
||
184 | * @param string[] $attributes |
||
185 | * |
||
186 | * @see AttributeManager::verifyValueForAttribute |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | 27 | public function verifyAttributes( $attributes ) { |
|
202 | |||
203 | /** |
||
204 | * For each supplied valid attribute this registers the attribute together with its valid values. |
||
205 | * |
||
206 | * Note: Registers only known attributes. |
||
207 | * |
||
208 | * @param string[] $validAttributes |
||
209 | * |
||
210 | * @see AttributeManager::getInitialAttributeRegister |
||
211 | * |
||
212 | * @return array ($filteredValidAttributes, $attributesRegister) |
||
213 | */ |
||
214 | 49 | protected function registerValidAttributes( $validAttributes ) { |
|
227 | |||
228 | /** |
||
229 | * For a given attribute, this verifies, if value is allowed. If verification succeeds, the value will be returned, false otherwise. |
||
230 | * If an attribute is registered as NO_FALSE_VALUE and value is the empty string, it gets converted to true. |
||
231 | * |
||
232 | * Note: an ANY_VALUE attribute can still be the empty string. |
||
233 | * Note: that every value for an unregistered attribute fails verification automatically. |
||
234 | * |
||
235 | * @param string $attribute |
||
236 | * @param string $value |
||
237 | * |
||
238 | * @return bool|string |
||
239 | */ |
||
240 | 25 | protected function verifyValueForAttribute( $attribute, $value ) { |
|
252 | |||
253 | /** |
||
254 | * @return array |
||
255 | */ |
||
256 | 49 | private function getInitialAttributeRegister() { |
|
276 | |||
277 | /** |
||
278 | * Extracts the value for attribute from passed attributes. If attribute itself |
||
279 | * is not set, it also looks for aliases. |
||
280 | * |
||
281 | * @param $attribute |
||
282 | * @param $passedAttributes |
||
283 | * |
||
284 | * @see AttributeManager::ALIASES |
||
285 | * |
||
286 | * @return null|string |
||
287 | */ |
||
288 | 26 | private function getValueForAttribute( $attribute, $passedAttributes ) { |
|
303 | |||
304 | /** |
||
305 | * @param string $value |
||
306 | * |
||
307 | * @return bool|string |
||
308 | */ |
||
309 | 10 | private function verifyValueForNoValueAttribute( $value ) { |
|
315 | } |