File indexing completed on 2024-06-16 05:30:23

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Server
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 /**
0022  * Node Tree class for Zend_Server reflection operations
0023  *
0024  * @category   Zend
0025  * @package    Zend_Server
0026  * @subpackage Reflection
0027  * @version $Id$
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Server_Reflection_Node
0032 {
0033     /**
0034      * Node value
0035      * @var mixed
0036      */
0037     protected $_value = null;
0038 
0039     /**
0040      * Array of child nodes (if any)
0041      * @var array
0042      */
0043     protected $_children = array();
0044 
0045     /**
0046      * Parent node (if any)
0047      * @var Zend_Server_Reflection_Node
0048      */
0049     protected $_parent = null;
0050 
0051     /**
0052      * Constructor
0053      *
0054      * @param mixed $value
0055      * @param Zend_Server_Reflection_Node $parent Optional
0056      * @return Zend_Server_Reflection_Node
0057      */
0058     public function __construct($value, Zend_Server_Reflection_Node $parent = null)
0059     {
0060         $this->_value = $value;
0061         if (null !== $parent) {
0062             $this->setParent($parent, true);
0063         }
0064 
0065         return $this;
0066     }
0067 
0068     /**
0069      * Set parent node
0070      *
0071      * @param Zend_Server_Reflection_Node $node
0072      * @param boolean $new Whether or not the child node is newly created
0073      * and should always be attached
0074      * @return void
0075      */
0076     public function setParent(Zend_Server_Reflection_Node $node, $new = false)
0077     {
0078         $this->_parent = $node;
0079 
0080         if ($new) {
0081             $node->attachChild($this);
0082             return;
0083         }
0084     }
0085 
0086     /**
0087      * Create and attach a new child node
0088      *
0089      * @param mixed $value
0090      * @access public
0091      * @return Zend_Server_Reflection_Node New child node
0092      */
0093     public function createChild($value)
0094     {
0095         $child = new self($value, $this);
0096 
0097         return $child;
0098     }
0099 
0100     /**
0101      * Attach a child node
0102      *
0103      * @param Zend_Server_Reflection_Node $node
0104      * @return void
0105      */
0106     public function attachChild(Zend_Server_Reflection_Node $node)
0107     {
0108         $this->_children[] = $node;
0109 
0110         if ($node->getParent() !== $this) {
0111             $node->setParent($this);
0112         }
0113     }
0114 
0115     /**
0116      * Return an array of all child nodes
0117      *
0118      * @return array
0119      */
0120     public function getChildren()
0121     {
0122         return $this->_children;
0123     }
0124 
0125     /**
0126      * Does this node have children?
0127      *
0128      * @return boolean
0129      */
0130     public function hasChildren()
0131     {
0132         return count($this->_children) > 0;
0133     }
0134 
0135     /**
0136      * Return the parent node
0137      *
0138      * @return null|Zend_Server_Reflection_Node
0139      */
0140     public function getParent()
0141     {
0142         return $this->_parent;
0143     }
0144 
0145     /**
0146      * Return the node's current value
0147      *
0148      * @return mixed
0149      */
0150     public function getValue()
0151     {
0152         return $this->_value;
0153     }
0154 
0155     /**
0156      * Set the node value
0157      *
0158      * @param mixed $value
0159      * @return void
0160      */
0161     public function setValue($value)
0162     {
0163         $this->_value = $value;
0164     }
0165 
0166     /**
0167      * Retrieve the bottommost nodes of this node's tree
0168      *
0169      * Retrieves the bottommost nodes of the tree by recursively calling
0170      * getEndPoints() on all children. If a child is null, it returns the parent
0171      * as an end point.
0172      *
0173      * @return array
0174      */
0175     public function getEndPoints()
0176     {
0177         $endPoints = array();
0178         if (!$this->hasChildren()) {
0179             return $endPoints;
0180         }
0181 
0182         foreach ($this->_children as $child) {
0183             $value = $child->getValue();
0184 
0185             if (null === $value) {
0186                 $endPoints[] = $this;
0187             } elseif ((null !== $value)
0188                 && $child->hasChildren())
0189             {
0190                 $childEndPoints = $child->getEndPoints();
0191                 if (!empty($childEndPoints)) {
0192                     $endPoints = array_merge($endPoints, $childEndPoints);
0193                 }
0194             } elseif ((null !== $value) && !$child->hasChildren()) {
0195                 $endPoints[] = $child;
0196             }
0197         }
0198 
0199         return $endPoints;
0200     }
0201 }