Source for file Branch.class.php
Documentation is available at Branch.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* Composite Pattern Branch Class
* The Branch class can hold any number of Composite objects. It will provide a
* reference to each Composite based on the name. Simply add or remove Composite
* objects to the Branch to populate.
* If a Branch holds Composite elements
* with the same name, each individual Composite can be accessed by a numerical
* reference. This feature will automatically be initiated when it finds another
* Composite with the same name. However, this only works on one level. This means
* if Composites of name 'last' are added, the numerical reference will work only
* with the 'last' Composites. If multiple Composites of another name 'first' are
* added, the numerical references will be reset, and will reference the 'first'
* Composites. Use additional Branches to hold numerical references, separating the
* responsibility from the parent.
* $branch->add (new Gumbo_Leaf ("USA", "country"));
* $branch->country = new Gumbo_Leaf ("USA");
* $branch->remove ("country");
* $branch->country = null;
* To access Composite elements inside the Branch:
* $branch->get ("country")->value;
* $branch->country->value;
* To access Composite elements using a numerical reference:
* $branch->get (0)->value; // for first element
* $branch->{0}->value; // for first element
* The Branch implements the IteratorAggregate interface, which allows easy looping
* through the elements inside the Branch. The first method will loop through
* every element inside the Branch. The second will only loop through the numerical
* foreach ($branch as $obj) {
* for ($x = 0;$x < $branch->getCount ();$x++) {
* if (!isset ($branch->get ($x)) { continue; } // checks if element still exists
* $obj = $branch->get ($x);
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* @desc Composite Pattern Branch Class
/** @var array $_list array of Composite objects */
/** @var int $_count number of numerical referenced Composites */
* @param int|string$name name of the Branch
* Returns the value of the given element
* This method will be responsible for returning the element
* object located inside the list. It will check for the key
* inside the array, and return the object. If the key is not
* found, it will return null.
* If the parameter is equal to "name", then it will return the
* name of the object. Be cautious, as the list could contain a
* key reference as "name". If this occurs, the Composite object inside
* the array element will be returned. Use the getName () method to
* ensure the Composite name is returned.
* @throws Gumbo_Exception
public function __get ($name) {
if (!isset
($this->{$name})) {
return $this->get ($name);
$e->setFunction (__METHOD__
);
* Checks if the given key if referenced inside the list
* @throws Gumbo_Exception
return isset
($this->_list [$name]);
$e->setFunction (__METHOD__
);
* Sets the value of the given element (only with a string key)
* If the value is a Composite object, then it will set the key reference
* to the name of the Composite.
* $branch->any_string = new Gumbo_Leaf ("USA", "country");
* $branch->add (new Gumbo_Leaf ("USA", "country"));
* If the value argument is 'null', it will remove the Composite.
* $branch->country = null;
* $branch->remove ("country");
* The last option will create a new Leaf object with the 'name' and
* 'val' arguments as the Leaf 'name' and 'value'
* $branch->country = "USA";
* $branch->add (new Gumbo_Leaf ("country", "USA"));
public function __set ($name, $val) {
// sets a Composite object
// creates a new Leaf object based on the value except
* Removes the Tree object references by the supplied key
* @throws Gumbo_Exception
if (!isset
($this->{$name})) {
unset
($this->_list [$name]);
$e->setFunction (__METHOD__
);
* Add a Composite object to the Branch
* @param Gumbo_Interface_Composite $tree
* @param bool $replace replaces the Tree object
* @throws Gumbo_Exception
public function add (Gumbo_Interface_Composite $tree, $replace=
false) {
$e->setFunction (__METHOD__
);
$this->_list [$name] =
$tree;
// if two Composites have the same name, new Composite appended to integer list
if (isset
($this->_list [$name])) {
if (!isset
($this->_list [0])) {
$this->_list [$name] =
$tree;
* Removes a Tree object from the Branch based on the name value
* @param int|string$name Tree name
* @throws Gumbo_Exception
public function remove ($name) {
if (!isset
($this->{$name})) {
unset
($this->_list [$name]);
$e->setFunction (__METHOD__
);
* Returns the Composite object referenced by the supplied key
* @return Gumbo_Interface_Composite
* @throws Gumbo_Exception
public function get ($name=
null) {
if (is_null ($name)) { return null; }
if (!isset
($this->_list [$name])) {
return $this->_list [$name];
$e->setFunction (__METHOD__
);
* Returns the number of numerical Composite references