openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Component.class.php (2153B)


      1 <?php
      2 
      3 namespace template_engine\components\html;
      4 
      5 use cms\action\RequestParams;
      6 use template_engine\element\Element;
      7 use template_engine\engine\TemplateContext;
      8 
      9 /**
     10  * Base class for all components.
     11  *
     12  * @package template_engine\components\html
     13  */
     14 abstract class Component
     15 {
     16 	/**
     17 	 * Contains child components.
     18 	 * 
     19 	 * @var array
     20 	 */
     21 	private $childComponents = [];
     22 
     23 	private $depth;
     24 
     25 	/**
     26 	 * @param $component Component
     27 	 */
     28 	public function addChildComponent($component ) {
     29 		if   ( $component )
     30 			$this->childComponents[] = $component;
     31 	}
     32 
     33     /**
     34      * @var TemplateContext
     35      */
     36     public $context;
     37 
     38 	/**
     39 	 * @var Element
     40 	 */
     41     private $element;
     42 
     43 	/**
     44 	 * If a component is generating a tree of elements, then this element is the
     45 	 * element, which is getting the elements, which sub-components are creating.
     46 	 * Sublasses should set this element if necessary.
     47 	 *
     48 	 * @var Element
     49 	 */
     50     protected $adoptiveElement;
     51 
     52     public function __construct()
     53 	{
     54 	}
     55 
     56 	final public function getDepth()
     57 	{
     58 		return $this->depth;
     59 	}
     60 
     61 	final public function setDepth($depth)
     62 	{
     63 		$this->depth = $depth;
     64 	}
     65 
     66 	/**
     67 	 * Every component must generate a single element or a tree of elements.
     68 	 * Sublasses must overwrite this method.
     69 	 *
     70 	 * @return Element
     71 	 */
     72 	public abstract function createElement();
     73 
     74 	final public function init()
     75 	{
     76 		$this->element = $this->createElement();
     77 
     78 		// if there is no special adoptive element, lets use the root element.
     79 		if   ( ! $this->adoptiveElement )
     80 			$this->adoptiveElement = $this->element;
     81 	}
     82 
     83 
     84 	/**
     85 	 * Gets the element with all child elements from all child components.
     86 	 *
     87 	 * @return Element
     88 	 */
     89 	final public function getElement()
     90 	{
     91 		/** @var Component $childComponent */
     92 		foreach ($this->childComponents as $childComponent )
     93 			$this->adoptiveElement->addChild( $childComponent->getElement() );
     94 
     95 		return $this->element;
     96 	}
     97 
     98 
     99 
    100 	/**
    101 	 * Splits a text at the comma char and trims the parts.
    102 	 *
    103 	 * @param $text
    104 	 * @return array
    105 	 */
    106 	public static function splitByComma($text)
    107 	{
    108 		$parts = explode(',',$text);
    109 		return array_map( function($text) {
    110 			return trim($text);
    111 		},$parts);
    112 	}
    113 
    114 
    115 }