[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Tree/ -> NodeTrait.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 a Node Interface Trait Class.
  18   *
  19   * @since  4.0.0
  20   */
  21  trait NodeTrait
  22  {
  23      use ImmutableNodeTrait;
  24  
  25      /**
  26       * Set the parent of this node
  27       *
  28       * If the node already has a parent, the link is unset
  29       *
  30       * @param   NodeInterface|null  $parent  NodeInterface for the parent to be set or null
  31       *
  32       * @return  void
  33       *
  34       * @since   4.0.0
  35       */
  36      public function setParent(NodeInterface $parent)
  37      {
  38          if (!\is_null($this->_parent)) {
  39              $key = array_search($this, $this->_parent->_children);
  40              unset($this->_parent->_children[$key]);
  41          }
  42  
  43          $this->_parent = $parent;
  44  
  45          $this->_parent->_children[] = &$this;
  46  
  47          if (\count($this->_parent->_children) > 1) {
  48              end($this->_parent->_children);
  49              $this->_leftSibling = prev($this->_parent->_children);
  50              $this->_leftSibling->_rightSibling = $this;
  51          }
  52      }
  53  
  54      /**
  55       * Add child to this node
  56       *
  57       * If the child already has a parent, the link is unset
  58       *
  59       * @param   NodeInterface  $child  The child to be added.
  60       *
  61       * @return  void
  62       *
  63       * @since   4.0.0
  64       */
  65      public function addChild(NodeInterface $child)
  66      {
  67          $child->setParent($this);
  68      }
  69  
  70      /**
  71       * Remove a specific child
  72       *
  73       * @param   NodeInterface  $child  Child to remove
  74       *
  75       * @return  void
  76       *
  77       * @since   4.0.0
  78       */
  79      public function removeChild(NodeInterface $child)
  80      {
  81          $key = array_search($child, $this->_children);
  82          unset($this->_children[$key]);
  83      }
  84  
  85      /**
  86       * Function to set the left or right sibling of a node
  87       *
  88       * @param   NodeInterface  $sibling  NodeInterface object for the sibling
  89       * @param   boolean        $right    If set to false, the sibling is the left one
  90       *
  91       * @return  void
  92       *
  93       * @since   4.0.0
  94       */
  95      public function setSibling(NodeInterface $sibling, $right = true)
  96      {
  97          if ($right) {
  98              $this->_rightSibling = $sibling;
  99          } else {
 100              $this->_leftSibling = $sibling;
 101          }
 102      }
 103  }


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