3 namespace Org\Mxchange\CoreFramework\Helper;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
14 use \BasMethodCallException;
15 use \InvalidArgumentException;
18 * A generic helper class with generic methods
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseHelper extends BaseFrameworkSystem {
41 * Instance to the class which provides field values
43 private $valueInstance = NULL;
46 * Extra instance to the class which provides field values
48 private $extraInstance = NULL;
51 * Rendered content created by the helper class
53 private $content = '';
61 * Array with sub group
63 private $subGroups = [];
66 * Previously opened group
68 private $previousGroupId = '';
71 * Previously opened sub group
73 private $previousSubGroupId = '';
76 * Total counter for groups and sub groups
78 private $totalCounter = 0;
80 // Exception constants
81 const EXCEPTION_GROUP_NOT_OPENED = 0x1e3;
82 const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4;
83 const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5;
84 const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6;
85 const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7;
88 * Protected constructor
90 * @param $className Real name of the class
93 protected function __construct (string $className) {
94 // Call parent constructor
95 parent::__construct($className);
99 * Adds content directly
101 * @param $newContent New content to add
104 protected final function addContent (string $newContent) {
105 $this->content .= trim($newContent) . PHP_EOL;
109 * Add header content to the helper
111 * @param $content Content to to the base
114 protected function addHeaderContent (string $content) {
115 // Add the header content
116 $this->groups['header']['content'] = trim($content);
120 * Add footer content to the helper
122 * @param $content Content to to the base
125 protected function addFooterContent (string $content) {
126 // Add the footer content
127 $this->groups['footer']['content'] = trim($content);
131 * Adds content to the previously opened group or sub group. If a sub group
132 * was found it will be taken. If no group/sub group is opened at the moment
133 * the code will be passed to addContent().
135 * @param $newContent New content to add
137 * @throws InvalidArgumentException If a parameter has an invalid value
139 protected final function addContentToPreviousGroup (string $newContent) {
140 // Check on parameter
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: newContent=%s - CALLED!', $newContent));
142 if (empty($newContent)) {
144 throw new InvalidArgumentException('Parameter "newContent" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
147 // Check for sub/group
148 if ($this->ifSubGroupOpenedPreviously()) {
150 $subGroupId = $this->getPreviousSubGroupId();
153 $this->subGroups[$subGroupId]['content'] .= $newContent;
154 } elseif ($this->ifGroupOpenedPreviously()) {
156 $groupId = $this->getPreviousGroupId();
159 $this->groups[$groupId]['content'] .= $newContent;
162 $this->addContent($newContent);
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!');
172 * @return $content The rendered content by this helper
174 protected final function getContent () {
175 return $this->content;
179 * Public setter for extra instance
181 * @param $extraInstance An extra instance of FrameworkInterface to set
184 public final function setExtraInstance (FrameworkInterface $extraInstance) {
185 $this->extraInstance = $extraInstance;
189 * Assigns a field from the value instance with a template variable
191 * @param $fieldName Name of the field to assign
193 * @throws InvalidArgumentException If a parameter has an invalid value
195 public function assignField (string $fieldName) {
196 // Check on parameter
197 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s - CALLED!', $fieldName));
198 if (empty($fieldName)) {
200 throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
203 // Get the value from value instance
204 $fieldValue = $this->getValueField($fieldName);
206 // Assign it with a template variable
207 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue);
210 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!');
214 * Assigns a field from the value instance with a template variable but
215 * parses its content through a given filter method of the value instance
217 * @param $fieldName Name of the field to assign
218 * @param $filterMethod Method name to call of the value instance
220 * @throws InvalidArgumentException If a parameter has an invalid value
221 * @todo Rewrite this method using a helper class for filtering data
223 public function assignFieldWithFilter (string $fieldName, string $filterMethod) {
224 // Validate parameter
225 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s,filterMethod=%s - CALLED!', $fieldName, $filterMethod));
226 if (empty($fieldName)) {
228 throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
229 } elseif (empty($filterMethod)) {
231 throw new InvalidArgumentException('Parameter "filterMethod" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
235 $fieldValue = $this->getValueField($fieldName);
237 // Now filter it through the value through the filter method
238 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: %s[%s]=%s', $fieldName, gettype($fieldValue), $fieldValue));
239 $filteredValue = call_user_func_array(array($this, 'doFilter' . StringUtils::convertToClassName($filterMethod)), array($fieldValue));
241 // Assign it with a template variable
242 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: filteredValue[%s]=%s', gettype($filteredValue), $filteredValue));
243 $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue);
246 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!');
250 * Pre-fetches field default values from the given registry key instance into this class
252 * @param $registryKey Registry key which holds an object with values
253 * @param $extraKey Extra value instance key used if registryKey is null
255 * @throws InvalidArgumentException If a parameter has an invalid value
257 public function prefetchValueInstance (string $registryKey, string $extraKey = NULL) {
258 // Validate parameter
259 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: registryKey=%s,extraKey[%s]=%s - CALLED!', $registryKey, gettype($extraKey), $extraKey));
260 if (empty($registryKey)) {
262 throw new InvalidArgumentException('Parameter "registryKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
266 // Get the required instance
267 $this->valueInstance = GenericRegistry::getRegistry()->getInstance($registryKey);
268 } catch (NullPointerException $e) {
269 // Not set in registry
270 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: registryKey=%s returned no instance (NPE thrown)', $registryKey));
273 // Shall we get an extra instance?
274 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey[%s]=%s', gettype($extraKey), $extraKey));
275 if (!is_null($extraKey)) {
277 // Get the extra instance.
278 $this->extraInstance = GenericRegistry::getRegistry()->getInstance($extraKey);
279 } catch (NullPointerException $e) {
281 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey=%s returned no instance (NPE thrown), this->valueInstance[]=%s', $extraKey, gettype($this->valueInstance)));
282 $this->extraInstance = ObjectFactory::createObjectByConfiguredName($extraKey . '_class', [$this->valueInstance]);
284 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: extraKey=%s,this->extraInstance[%s]=%s', $extraKey, gettype($this->extraInstance), $this->extraInstance));
287 // Is the value instance valid?
288 //* NOSIY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->valueInstance[]=%s - BEFORE!', gettype($this->valueInstance)));
289 if (is_null($this->valueInstance)) {
290 // Get the requested instance
291 $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($this->extraInstance));
295 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->valueInstance[]=%s - AFTER/EXIT!', gettype($this->valueInstance)));
299 * Opens a helper group with given group id and content or throws an
300 * exception if that group is already found regardless if it is open or
303 * @param $groupId Group id to open
304 * @param $content Initial content to add to the group
305 * @param $tag HTML tag used to open this group
307 * @throws InvalidArgumentException If a parameter has an invalid value
308 * @throws BadMethodCallException If the group was already created before
310 protected function openGroupByIdContent (string $groupId, string $content, string $tag) {
311 // Is the group already there?
312 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: groupdId=%s,content=%s,tag=%s - CALLED!', $groupdId, $content, $tag));
313 if (empty($groupId)) {
315 throw new InvalidArgumentException('Parameter "groupId" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
316 } elseif (isset($this->groups[$groupId])) {
317 // Then throw an exception here
318 throw new BadMethodCallException(sprintf('[%s:%d]: groupId=%s is already opened.', $this->__toString(), $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND);
319 } elseif (empty($content)) {
321 throw new InvalidArgumentException('Parameter "content" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
322 } elseif (empty($tag)) {
324 throw new InvalidArgumentException('Parameter "tag" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
328 $this->totalCounter++;
330 // Add the group to the stack
331 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: this->totalCounter=%d', $this->totalCounter));
332 $this->groups[$this->totalCounter] = $groupId;
333 $this->groups[$groupId]['opened'] = true;
334 $this->groups[$groupId]['content'] = sprintf(
335 '<!-- group %s opened (length: %s, tag: %s) //-->%s' . PHP_EOL,
341 $this->groups[$groupId]['tag'] = $tag;
343 // Mark this group as previously opened
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: Setting this->previousGroupId=%s ...', $groupId));
345 $this->setPreviousGroupId($groupId);
348 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-HELPER: EXIT!');
352 * Closes the previously opened group by added given content to it or
353 * throws an exception if no previous group was opened
355 * @param $content Content for previously opened group, or empty to use tag of opener
357 * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found
359 public function closePreviousGroupByContent (string $content = '') {
360 // Check if any sub group was opened before
361 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: content(%d)=%s - CALLED!', strlen($content), $content));
362 if ($this->ifSubGroupOpenedPreviously()) {
363 // Close it automatically
364 $this->closePreviousSubGroupByContent();
367 // Check if any group was opened before
368 if ($this->ifGroupOpenedPreviously() === false) {
369 // Then throw an exception
370 throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
373 // Get previous group
374 $groupId = $this->getPreviousGroupId();
376 // Is the content empty?
377 if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) {
378 // Get it from opener
380 "<!-- group %s auto-closed //--></%s>",
382 $this->groups[$groupId]['tag']
386 // Add content to it and mark it as closed
387 $this->groups[$groupId]['content'] .= sprintf(
388 "<!-- group %s closed (length: %s, tag: %s) //-->%s\n",
391 $this->groups[$groupId]['tag'],
394 $this->groups[$groupId]['opened'] = false;
396 // Mark previous group as closed
397 $this->setPreviousGroupId('');
398 //* NOISY-DEBUG: */ echo "CLOSE:groupId={$groupId}<br />\n";
402 * Opens a helper sub group with given group id and content or throws an
403 * exception if that sub group is already found regardless if it is open or
406 * @param $subGroupId Sub group id to open
407 * @param $content Initial content to add to the sub group
408 * @param $tag HTML tag used to open this group
410 * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before
412 protected function openSubGroupByIdContent (string $subGroupId, string $content, string $tag) {
413 //* NOISY-DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."<br />\n";
414 // Is the group already there?
415 if (empty($subGroupId)) {
417 throw new InvalidArgumentException('Parameter "subGroupId" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
418 } elseif (isset($this->subGroups[$subGroupId])) {
419 // Then throw an exception here
420 throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND);
424 $this->totalCounter++;
426 // Add the group to the stack
427 $this->subGroups[$this->totalCounter] = $subGroupId;
428 $this->subGroups[$subGroupId]['opened'] = true;
429 $this->subGroups[$subGroupId]['content'] = sprintf("<!-- sub-group %s opened (length: %s, tag: %s) //-->%s\n", $subGroupId, strlen($content), $tag, $content);
430 $this->subGroups[$subGroupId]['tag'] = $tag;
432 // Mark this group as previously opened
433 $this->setPreviousSubGroupId($subGroupId);
437 * Closes the previously opened sub group by added given content to it or
438 * throws an exception if no previous sub group was opened
440 * @param $content Content for previously opened sub group, or leave empty to use div/span of openener
442 * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
444 public function closePreviousSubGroupByContent (string $content = '') {
445 // Check if any sub group was opened before
446 if ($this->ifSubGroupOpenedPreviously() === false) {
447 // Then throw an exception
448 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
451 // Get previous sub group
452 $subGroupId = $this->getPreviousSubGroupId();
454 // Is the content empty?
455 if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) {
456 // Get it from opener
457 $content = sprintf('<!-- sub-group %s auto-closed //--></%s>', $subGroupId, $this->subGroups[$subGroupId]['tag']);
460 // Add content to it and mark it as closed
461 $this->subGroups[$subGroupId]['content'] .= sprintf('<!-- sub-group %s closed (length: %s, tag: %s) //-->%s' . PHP_EOL, $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content);
462 $this->subGroups[$subGroupId]['opened'] = false
465 // Mark previous sub group as closed
466 $this->setPreviousSubGroupId('');
467 //* NOISY-DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}<br />\n";
471 * Renders all group and sub group in their order
473 * @return $content Rendered HTML content
475 public function renderContent () {
476 // Initialize content
479 // Is header content there?
480 if (isset($this->groups['header'])) {
482 $content .= $this->groups['header']['content'] . PHP_EOL;
486 $content .= $this->getContent();
488 // Now "walk" through all groups and sub-groups
489 for ($idx = 1; $idx <= $this->totalCounter; $idx++) {
490 // Is this a sub/group and is it closed?
491 if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) {
492 // Then add it's content
493 $groupContent = trim($this->groups[$this->groups[$idx]]['content']);
494 //* NOISY-DEBUG: */ echo "group={$this->groups[$idx]},content=<pre>".htmlentities($groupContent)."</pre><br />\n";
495 $content .= $groupContent;
496 } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) {
497 // Then add it's content
498 $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content'];
499 //* NOISY-DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=<pre>".htmlentities($subGroupContent)."</pre><br />\n";
500 $content .= trim($subGroupContent);
502 // Something went wrong
503 $this->debugInstance(__METHOD__ . '(): Something unexpected happened here.');
507 // Is footer content there?
508 if (isset($this->groups['footer'])) {
510 $content .= $this->groups['footer']['content'] . PHP_EOL;
514 //* NOISY-DEBUG: */ echo "content=<pre>".htmlentities($content)."</pre> (".strlen($content).")<br />\n";
519 * Checks whether the specified group is opened
521 * @param $groupId Id of group to check
522 * @return $isOpened Whether the specified group is open
523 * @throws InvalidArgumentException If a parameter has an invalid value
525 protected function ifGroupIsOpened (string $groupId) {
526 // Check on parameter
527 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: groupId=%s - CALLED!', $groupId));
528 if (empty($groupId)) {
530 throw new InvalidArgumentException('Parameter "groupId" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
533 // Is the group open?
534 $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true));
537 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: isOpened=%d - EXIT!', intval($isOpened)));
542 * Getter for direct field values
544 * @param $fieldName Name of the field we shall fetch
545 * @return $fieldValue Value from field
546 * @throws InvalidArgumentException If a parameter has an invalid value
547 * @throws NullPointerException Thrown if $valueInstance is null
549 public function getValueField (string $fieldName) {
551 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldName=%s - CALLED!', $fieldName));
552 if (empty($fieldName)) {
554 throw new InvalidArgumentException('Parameter "fieldName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
560 // The $valueInstance attribute should not be null!
561 if (is_null($this->getValueInstance())) {
562 // Throws an exception here
563 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
567 if ($this->getValueInstance()->isFieldSet($fieldName)) {
568 // Get the field value
569 $fieldValue = $this->getValueInstance()->getField($fieldName);
570 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!');
571 } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) {
572 // So try the extra instance
573 $fieldValue = $this->extraInstance->getField($fieldName);
574 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!');
577 $this->debugOutput('BASE-HELPER: fieldName=' . $fieldName . ' is not set! - @TODO');
581 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-HELPER: fieldValue[%s]=%s - EXIT!', gettype($fieldValue), $fieldValue));
586 * Getter for value instance
588 * @return $valueInstance Instance of the class holding our values
590 public final function getValueInstance () {
591 return $this->valueInstance;
595 * Check whether a group was opened previously
597 * @return $groupOpened Whether any group was opened before
599 protected final function ifGroupOpenedPreviously () {
600 $groupOpened = (!empty($this->previousGroupId));
605 * Check whether a group was opened previously
607 * @return $subGroupOpened Whether any group was opened before
609 protected final function ifSubGroupOpenedPreviously () {
610 $subGroupOpened = (!empty($this->previousSubGroupId));
611 return $subGroupOpened;
615 * Getter for previous group id
617 * @return $previousGroupId Id of previously opened group
619 protected final function getPreviousGroupId () {
620 return $this->previousGroupId;
624 * Setter for previous group id
626 * @param $previousGroupId Id of previously opened group
629 protected final function setPreviousGroupId (string $previousGroupId) {
630 $this->previousGroupId = $previousGroupId;
634 * Getter for previous sub group id
636 * @return $previousSubGroupId Id of previously opened sub group
638 protected final function getPreviousSubGroupId () {
639 return $this->previousSubGroupId;
643 * Setter for previous sub group id
645 * @param $previousSubGroupId Id of previously opened sub group
648 protected final function setPreviousSubGroupId (string $previousSubGroupId) {
649 $this->previousSubGroupId = $previousSubGroupId;