You are here

class TreeItem in Views tree 8.2

Defines a tree item class.

Hierarchy

  • class \Drupal\views_tree\TreeItem implements \Drupal\views_tree\IteratorAggregate

Expanded class hierarchy of TreeItem

2 files declare their use of TreeItem
TreeHelperTest.php in tests/src/Unit/TreeHelperTest.php
TreeItemTest.php in tests/src/Unit/TreeItemTest.php

File

src/TreeItem.php, line 8

Namespace

Drupal\views_tree
View source
class TreeItem implements \IteratorAggregate {

  /**
   * The main node in the tree.
   *
   * @var mixed
   */
  protected $node;

  /**
   * Leaves of the main node.
   *
   * @var \Drupal\views_tree\TreeItem[]
   */
  protected $leaves = [];

  /**
   * Creates a new TreeItem instance.
   *
   * @param mixed $node
   *   The main tree node to set.
   * @param array $leaves
   *   An optional array of leaves to set.
   */
  public function __construct($node, array $leaves = []) {
    $this
      ->setNode($node);
    $this
      ->setLeaves($leaves);
  }

  /**
   * Get the tree node.
   *
   * @return mixed
   *   The tree node.
   */
  public function getNode() {
    return $this->node;
  }

  /**
   * Sets the node.
   *
   * @param mixed $node
   *   The node to set.
   */
  public function setNode($node) {
    $this->node = $node;
  }

  /**
   * Get the leaves.
   *
   * @return \Drupal\views_tree\TreeItem[]
   *   An array of tree item leaves.
   */
  public function getLeaves() {
    return $this->leaves;
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    return new \ArrayIterator($this->leaves);
  }

  /**
   * Sets the leaves.
   *
   * @param array $leaves
   *   An array of leaves. If they are not already an instance of
   *   \Drupal\views_tree\TreeItem, each one will be converted.
   *
   * @return $this
   *   The instance of the TreeItem.
   */
  public function setLeaves(array $leaves) {
    foreach ($leaves as &$leave) {
      if (!$leave instanceof static) {
        $leave = new TreeItem($leave);
      }
    }
    $this->leaves = $leaves;
    return $this;
  }

  /**
   * Adds a leaf.
   *
   * @param mixed $item
   *   An item to add. If not an instance of \Drupal\views_tree\TreeItem it will
   *   be converted.
   *
   * @return $this
   */
  public function addLeave($item) {
    if (!$item instanceof static) {
      $item = new TreeItem($item);
    }
    $this->leaves[] = $item;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TreeItem::$leaves protected property Leaves of the main node.
TreeItem::$node protected property The main node in the tree.
TreeItem::addLeave public function Adds a leaf.
TreeItem::getIterator public function
TreeItem::getLeaves public function Get the leaves.
TreeItem::getNode public function Get the tree node.
TreeItem::setLeaves public function Sets the leaves.
TreeItem::setNode public function Sets the node.
TreeItem::__construct public function Creates a new TreeItem instance.