1 | <?php |
||
39 | abstract class AbstractComponent implements NestableInterface { |
||
40 | /** |
||
41 | * Holds a reference of the component's attribute manger. |
||
42 | * |
||
43 | * @var AttributeManager $attributeManager |
||
44 | */ |
||
45 | private $attributeManager; |
||
46 | |||
47 | /** |
||
48 | * @var ComponentLibrary $componentLibrary |
||
49 | */ |
||
50 | private $componentLibrary; |
||
51 | |||
52 | /** |
||
53 | * The (html) id of this component. Not available before the component was opened. |
||
54 | * |
||
55 | * @var string $id |
||
56 | */ |
||
57 | private $id; |
||
58 | |||
59 | /** |
||
60 | * Name of the component |
||
61 | * |
||
62 | * @var string $name |
||
63 | */ |
||
64 | private $name; |
||
65 | |||
66 | /** |
||
67 | * @var NestingController $nestingController |
||
68 | */ |
||
69 | private $nestingController; |
||
70 | |||
71 | /** |
||
72 | * @var NestableInterface|false $parentComponent |
||
73 | */ |
||
74 | private $parentComponent; |
||
75 | |||
76 | /** |
||
77 | * @var ParserOutputHelper $parserOutputHelper |
||
78 | */ |
||
79 | private $parserOutputHelper; |
||
80 | |||
81 | /** |
||
82 | * @var ParserRequest $parserRequest |
||
83 | */ |
||
84 | private $parserRequest; |
||
85 | |||
86 | /** |
||
87 | * For every of my registered attributes holds a value. false, if not valid in supplied |
||
88 | * parserRequest. |
||
89 | * |
||
90 | * @var array $sanitizedAttributes |
||
91 | */ |
||
92 | private $sanitizedAttributes; |
||
93 | |||
94 | /** |
||
95 | * Does the actual work in the individual components. |
||
96 | * |
||
97 | * @param string $input |
||
98 | * |
||
99 | * @return string|array |
||
100 | */ |
||
101 | abstract protected function placeMe( $input ); |
||
102 | |||
103 | /** |
||
104 | * Component constructor. |
||
105 | * |
||
106 | * @param ComponentLibrary $componentLibrary |
||
107 | * @param ParserOutputHelper $parserOutputHelper |
||
108 | * @param NestingController $nestingController |
||
109 | * |
||
110 | * @throws MWException cascading {@see \BootstrapComponents\ComponentLibrary::getNameFor} |
||
111 | * or {@see \BootstrapComponents\Component::extractAttribute} |
||
112 | */ |
||
113 | 42 | public function __construct( $componentLibrary, $parserOutputHelper, $nestingController ) { |
|
126 | 42 | ||
127 | /** |
||
128 | * Returns the name of the component. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getComponentName() { |
||
135 | |||
136 | /** |
||
137 | * Note that id is only present after {@see AbstractComponent::parseComponent} starts execution. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getId() { |
||
144 | |||
145 | /** |
||
146 | * @param ParserRequest $parserRequest ; |
||
147 | * |
||
148 | * @throws \MWException self or cascading from {@see \BootstrapComponents\Component::processArguments} |
||
149 | * or {@see \BootstrapComponents\NestingController::close} |
||
150 | * @return string|array |
||
151 | */ |
||
152 | public function parseComponent( $parserRequest ) { |
||
165 | |||
166 | /** |
||
167 | * Converts the input array to a string using glue. Removes invalid (false) entries beforehand. |
||
168 | * |
||
169 | * @param array|false $array |
||
170 | * @param string $glue |
||
171 | * |
||
172 | * @return false|string returns false on empty array, string otherwise |
||
173 | */ |
||
174 | protected function arrayToString( $array, $glue ) { |
||
185 | |||
186 | /** |
||
187 | * @return AttributeManager |
||
188 | */ |
||
189 | protected function getAttributeManager() { |
||
192 | |||
193 | /** |
||
194 | * @return ComponentLibrary |
||
195 | */ |
||
196 | protected function getComponentLibrary() { |
||
199 | |||
200 | /** |
||
201 | * @return NestingController |
||
202 | */ |
||
203 | protected function getNestingController() { |
||
206 | |||
207 | /** |
||
208 | * @return NestableInterface|false |
||
209 | */ |
||
210 | protected function getParentComponent() { |
||
213 | |||
214 | /** |
||
215 | * @return ParserOutputHelper |
||
216 | */ |
||
217 | protected function getParserOutputHelper() { |
||
225 | |||
226 | /** |
||
227 | * Returns the original parser request supplied to this component. |
||
228 | * Note, that none of the attributes nor the input were parsed with |
||
229 | * {@see \Parser::recursiveTagParse}. |
||
230 | * |
||
231 | * @return ParserRequest |
||
232 | */ |
||
233 | protected function getParserRequest() { |
||
236 | |||
237 | /** |
||
238 | * If attribute is registered, this returns the verified and parsed value for it. If not, or the |
||
239 | * verified value is false, this returns the fallback. |
||
240 | * |
||
241 | * @param string $attribute |
||
242 | * @param bool|string $fallback |
||
243 | * |
||
244 | * @return bool|string |
||
245 | */ |
||
246 | protected function getValueFor( $attribute, $fallback = false ) { |
||
252 | |||
253 | /** |
||
254 | * Checks, if component has a value supplied for $attribute. Note, that $value can be empty string or evaluate to false. |
||
255 | * |
||
256 | * @param string $attribute |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | protected function hasValueFor( $attribute ) { |
||
263 | |||
264 | 29 | /** |
|
265 | 29 | * Parses input text from parser request. Does also some fixes to let parser detect paragraphs in content. |
|
266 | 29 | * |
|
267 | 2 | * @param ParserRequest $parserRequest |
|
268 | 2 | * @param bool $fullParse |
|
269 | 2 | * |
|
270 | 2 | * @return string |
|
271 | 2 | * @since 1.1.0 |
|
272 | 29 | * |
|
273 | 29 | */ |
|
274 | 29 | protected function prepareInput( $parserRequest, $fullParse = false ) { |
|
293 | 27 | ||
294 | 27 | /** |
|
295 | 27 | * Takes your class and style string and appends them with corresponding data from user (if present) |
|
296 | 27 | * passed in attributes. |
|
297 | 25 | * |
|
298 | 25 | * @param string|array $class |
|
299 | 27 | * @param string|array $style |
|
300 | 25 | * |
|
301 | 25 | * @return array[] containing (array)$class and (array)$style |
|
302 | 27 | */ |
|
303 | protected function processCss( $class, $style ) { |
||
314 | 29 | ||
315 | 29 | /** |
|
316 | 29 | * Performs all the mandatory actions on the parser output for the component class. |
|
317 | */ |
||
318 | private function augmentParserOutput() { |
||
327 | 29 | ||
328 | 29 | /** |
|
329 | 29 | * Initializes the attributes, id and stores the original parser request. |
|
330 | 29 | * |
|
331 | 29 | * @param ParserRequest $parserRequest |
|
332 | 29 | */ |
|
333 | 29 | private function initComponentData( $parserRequest ) { |
|
344 | |||
345 | 29 | /** |
|
346 | 29 | * For every registered attribute, sanitizes (parses and verifies) the corresponding value in supplied attributes. |
|
347 | 28 | * |
|
348 | 29 | * @param \Parser $parser |
|
349 | 29 | * @param array $attributes |
|
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | private function sanitizeAttributes( $parser, $attributes ) { |
||
369 | } |
||
370 |