1 | <?php |
||
34 | class AttributeManager { |
||
35 | |||
36 | /** |
||
37 | * For attributes that take any value. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | const ANY_VALUE = 1; |
||
42 | |||
43 | /** |
||
44 | * For attributes that can be set to false by supplying one of certain values. |
||
45 | * Usually uses for flag-attributes like "active", "collapsible", etc. |
||
46 | * |
||
47 | * @see \BootstrapComponents\AttributeManager::$noValues |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | const NO_FALSE_VALUE = 0; |
||
52 | |||
53 | /** |
||
54 | * Holds all values indicating a "no". Can be used to ignore "enable"-fields. |
||
55 | * |
||
56 | * @var array $noValues |
||
57 | */ |
||
58 | private $noValues; |
||
59 | |||
60 | /** |
||
61 | * The list of attributes that are considered valid; holds alias relation 'name|alias' => 'real name' |
||
62 | * |
||
63 | * @var string[] $validAttributeNameMapping |
||
64 | */ |
||
65 | private $validAttributeNameMapping; |
||
66 | |||
67 | #@todo need a method: attributeIsSupplied(). for components must be able to check, if "header" was supplied and don't know about aliases |
||
68 | |||
69 | /** |
||
70 | * AttributeManager constructor. |
||
71 | * |
||
72 | * Do not instantiate directly, but use {@see ApplicationFactory::getNewAttributeManager} |
||
73 | * instead. |
||
74 | * |
||
75 | * @param string[] $componentAttributes the list of attributes, this manager deems valid. |
||
76 | * @param string[] $aliases the list of aliases and their corresponding attribute |
||
77 | * |
||
78 | * @see ApplicationFactory::getNewAttributeManager |
||
79 | */ |
||
80 | public function __construct( $componentAttributes, $aliases ) { |
||
85 | |||
86 | /** |
||
87 | * Returns the list of all defined attributes, including those that are invalid for the current component. |
||
88 | * |
||
89 | * @return string[] |
||
90 | */ |
||
91 | public function getAllKnownAttributes() { |
||
94 | 49 | ||
95 | 49 | /** |
|
96 | 49 | * Checks if given $attribute is registered with the manager. Note that an alias is deemed valid. |
|
97 | 49 | * |
|
98 | 49 | * @param string $attribute |
|
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | public function isValid( $attribute ) { |
||
105 | 1 | ||
106 | 1 | /** |
|
107 | * Checks, if the attribute $neededAttribute or its alias is found in $suppliedAttributes. |
||
108 | * |
||
109 | * @param string $neededAttribute |
||
110 | * @param string[] $suppliedAttributes |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isSuppliedInRequest( $neededAttribute, $suppliedAttributes ) { |
||
128 | |||
129 | 27 | /** |
|
130 | 27 | * Extracts the _verified_ value for attribute from passed attributes. When attribute is an alias, it returns the real attribute name |
|
131 | * together with the aliases value. |
||
132 | * |
||
133 | * Note: |
||
134 | * * To make certain, that $attribute is valid, use {@see \BootstrapComponents\AttributeManager::isValid} beforehand. |
||
135 | * * If there is no data for $attribute in $passedValue, or the value does not pass verification, this returns null as $passedValue. |
||
136 | * |
||
137 | * @param string $attribute the attribute|alias, data will be extracted for |
||
138 | * @param string $passedValue must be already parsed |
||
139 | * |
||
140 | 44 | * @return array ( (string) $attributeName, (null|string) $passedValue ); |
|
141 | 44 | */ |
|
142 | public function validateAttributeAndValue( $attribute, $passedValue ) { |
||
147 | |||
148 | /** |
||
149 | * Registers all valid attribute names and all aliases. |
||
150 | * |
||
151 | * @param string[] $validAttributes |
||
152 | * @param string[] $aliases |
||
153 | * |
||
154 | * @return array $filteredValidAttributeNameMapping |
||
155 | * @see AttributeManager::getAttributeRegister |
||
156 | * |
||
157 | */ |
||
158 | protected function calculateValidAttributeNames( $validAttributes, $aliases ) { |
||
180 | |||
181 | /** |
||
182 | * Returns the allowed values for a given attribute or NULL if invalid attribute. Note that an alias is deemed "valid". |
||
183 | * |
||
184 | * Note that allowed values can be an array, {@see AttributeManager::NO_FALSE_VALUE}, |
||
185 | * or {@see AttributeManager::ANY_VALUE}. |
||
186 | * |
||
187 | * @param string $attribute |
||
188 | 27 | * |
|
189 | 27 | * @return null|array|bool |
|
190 | 27 | */ |
|
191 | 26 | protected function getAllowedValuesFor( $attribute ) { |
|
197 | 27 | ||
198 | 27 | /** |
|
199 | * @param string $attribute |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | protected function isInRegister( $attribute ) { |
||
206 | |||
207 | /** |
||
208 | * Resolves aliases to their mapped attribute. If $attributeName is unknown, it is returned. |
||
209 | * |
||
210 | * @param string $attributeName |
||
211 | * |
||
212 | 49 | * @return string |
|
213 | 49 | */ |
|
214 | 49 | protected function resolveAttributeName( $attributeName ) { |
|
219 | 41 | ||
220 | 41 | /** |
|
221 | 41 | * For a given attribute, this verifies, if value is allowed. If verification succeeds, the value will be returned, null otherwise. |
|
222 | 49 | * If an attribute is registered as NO_FALSE_VALUE and value is the empty string, it gets converted to true. |
|
223 | 49 | * |
|
224 | * Note: an ANY_VALUE attribute can still be the empty string. |
||
225 | * Note: that every value for an unregistered attribute fails verification automatically. |
||
226 | * |
||
227 | * @param string $attribute |
||
228 | * @param string $value |
||
229 | * |
||
230 | * @return null|bool|string |
||
231 | */ |
||
232 | protected function validateValueForAttribute( $attribute, $value ) { |
||
244 | 17 | ||
245 | 13 | /** |
|
246 | 12 | * @return array |
|
247 | */ |
||
248 | 10 | private function getAttributeRegister() { |
|
272 | |||
273 | /** |
||
274 | * If $value is a no-value, this returns false. if then $value is empty(), this returns true, $value otherwise. |
||
275 | * |
||
276 | * @param null|string $value |
||
277 | * |
||
278 | * @return bool|string |
||
279 | */ |
||
280 | private function verifyValueForNoValueAttribute( $value ) { |
||
289 | } |
||
290 |