Gumbo_Branch

Gumbo_Tree
   |
   --Gumbo_Branch

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.

To add Composites:

 $branch->add (new Gumbo_Leaf ("USA", "country"));
 $branch->country = new Gumbo_Leaf ("USA");

To remove a Composite:

 $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 referenced elements.

 foreach ($branch as $obj) {
 		// do something
 		...
 }

 for ($x = 0;$x < $branch->getCount ();$x++) {
 		if (!isset ($branch->get ($x)) { continue; } // checks if element still exists
 		$obj = $branch->get ($x);
 		// do something
 		...
 }

Author(s): Michael Luster <mluster79@yahoo.com>
License:New BSD License
Copyright:Copyright (c) 2007, iBayou, Michael Luster
Link:http://sourceforge.net/projects/phpgumbo
Version:0.0.1

Implements interfaces:

  • IteratorAggregate (internal interface)

Inherited Methods

Class Variables

int $_count =  0 [line 98]
array $_list = array () [line 96]

Class Methods

public Gumbo_Branch __construct ( [int|string $name] ) [line 106]

Constructor

Parameter(s):

  • (int|string) $name :: name of the Branch

[ Top ]
public void add ( Gumbo_Interface_Composite $tree, [bool $replace] ) [line 252]

Add a Composite object to the Branch

Parameter(s):

  • throws:  Gumbo_Exception

[ Top ]
public Gumbo_Interface_Composite get ( [int|string $name] ) [line 322]

Returns the Composite object referenced by the supplied key

Parameter(s):

  • (int|string) $name
  • throws:  Gumbo_Exception

[ Top ]
public int getCount ( ) [line 345]

Returns the number of numerical Composite references


[ Top ]
public Iterator getIterator ( ) [line 354]

Returns an Iterator

Implementation of:
IteratorAggregate::getIterator

[ Top ]
public void remove ( int|string $name ) [line 292]

Removes a Tree object from the Branch based on the name value

Parameter(s):

  • (int|string) $name :: Tree name
  • throws:  Gumbo_Exception

[ Top ]
public mixed __get ( int|string $name ) [line 128]

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.

Parameter(s):

  • (int|string) $name
  • throws:  Gumbo_Exception

[ Top ]
public bool __isset ( int|string $name ) [line 155]

Checks if the given key if referenced inside the list

 isset ($this->{test});

Parameter(s):

  • (int|string) $name
  • throws:  Gumbo_Exception

[ Top ]
public void __set ( int|string $name, mixed $val ) [line 199]

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");
 // similar to calling
 $branch->add (new Gumbo_Leaf ("USA", "country"));

If the value argument is 'null', it will remove the Composite.

 $branch->country = null;
 // similar to calling
 $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";
 // similar to calling:
 $branch->add (new Gumbo_Leaf ("country", "USA"));

Parameter(s):

  • (int|string) $name
  • (mixed) $val

[ Top ]
public void __unset ( int|string $name ) [line 227]

Removes the Tree object references by the supplied key

 unset ($this->{test});

Parameter(s):

  • (int|string) $name
  • throws:  Gumbo_Exception

[ Top ]