[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Tree/ -> ImmutableNodeTrait.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Tree;
  11  
  12  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Defines the trait for an Immutable Node Class.
  18   *
  19   * @since  4.0.0
  20   */
  21  trait ImmutableNodeTrait
  22  {
  23      /**
  24       * Parent node object
  25       *
  26       * @var    NodeInterface
  27       * @since  1.6
  28       */
  29      protected $_parent = null;
  30  
  31      /**
  32       * Array of Children
  33       *
  34       * @var    NodeInterface[]
  35       * @since  1.6
  36       */
  37      protected $_children = array();
  38  
  39      /**
  40       * Node left of this one
  41       *
  42       * @var    NodeInterface
  43       * @since  1.6
  44       */
  45      protected $_leftSibling = null;
  46  
  47      /**
  48       * Node right of this one
  49       *
  50       * @var    NodeInterface
  51       * @since  1.6
  52       */
  53      protected $_rightSibling = null;
  54  
  55      /**
  56       * Get the children of this node
  57       *
  58       * @param   boolean  $recursive  False by default
  59       *
  60       * @return  NodeInterface[]  The children
  61       *
  62       * @since   4.0.0
  63       */
  64      public function &getChildren($recursive = false)
  65      {
  66          if ($recursive) {
  67              $items = array();
  68  
  69              foreach ($this->_children as $child) {
  70                  $items[] = $child;
  71                  $items = array_merge($items, $child->getChildren(true));
  72              }
  73  
  74              return $items;
  75          }
  76  
  77          return $this->_children;
  78      }
  79  
  80      /**
  81       * Get the parent of this node
  82       *
  83       * @return  NodeInterface|null
  84       *
  85       * @since   4.0.0
  86       */
  87      public function getParent()
  88      {
  89          return $this->_parent;
  90      }
  91  
  92      /**
  93       * Get the root of the tree
  94       *
  95       * @return  ImmutableNodeInterface
  96       *
  97       * @since   4.0.0
  98       */
  99      public function getRoot()
 100      {
 101          $root = $this->getParent();
 102  
 103          if (!$root) {
 104              return $this;
 105          }
 106  
 107          while ($root->hasParent()) {
 108              $root = $root->getParent();
 109          }
 110  
 111          return $root;
 112      }
 113  
 114      /**
 115       * Test if this node has children
 116       *
 117       * @return  boolean  True if there is a child
 118       *
 119       * @since   4.0.0
 120       */
 121      public function hasChildren()
 122      {
 123          return (bool) \count($this->_children);
 124      }
 125  
 126      /**
 127       * Test if this node has a parent
 128       *
 129       * @return  boolean  True if there is a parent
 130       *
 131       * @since   4.0.0
 132       */
 133      public function hasParent()
 134      {
 135          return $this->getParent() != null;
 136      }
 137  
 138      /**
 139       * Returns the right or left sibling of a node
 140       *
 141       * @param   boolean  $right  If set to false, returns the left sibling
 142       *
 143       * @return  NodeInterface|null  NodeInterface object of the sibling.
 144       *
 145       * @since   4.0.0
 146       */
 147      public function getSibling($right = true)
 148      {
 149          if ($right) {
 150              return $this->_rightSibling;
 151          } else {
 152              return $this->_leftSibling;
 153          }
 154      }
 155  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer