Location: PHPKode > scripts > BowML > BowList.inc.php
<?php

/**
 * Class : BowList
 * Developed by Eric Aili and Jonas Eriksson
 * @copyright 2003 BICOM KB
 **/

 class BowList
 {
	/*Instance variables*/
	var $elements;
	
	/*Constructor*/
	function BowList()
	{
		$this->elementsSet(array());
	}

	/*Getter and Setter for intance variable $elements*/
	function &elements() {return $this->elements;}
	function elementsSet($someElements) {$this->elements = $someElements;}
	
	/*Get the value associated with $key*/
	function &value($key)
	{
		$array =& $this->elements();
		return $array[$key];
	}
	
	/*Get the element reffered to by the array pointer*/
	function &getCurrentElement() {return current($this->elements());}
	
	/*Returns true if the array is empty, otherwhise, false*/
	function isEmpty() {return ($this->size() == 0);}
	
	/*Returns true if the array is not empty, otherwhise, false*/
	function notEmpty() {return ($this->size() > 0);}
	
	/*Return the element after the current one, 
	* if none is detected, return false*/
	function &getNextElement()
	{
		$currentElement =& $this->getCurrentElement();
		if (next($this->elements))
			return $currentElement;
		else
		{
			return false;
		}
	}
	
	/*Return the current key value pair as a BowAssociation*/
	function &currentAssociation()
	{
		$association =& new BowAssociation();
		$association->currentAssociation($this->elements);
		return $association;
	}
	
	/*Return the next key value pair as a BowAssociation*/
	function &nextAssociation()
	{
		$association =& new BowAssociation();
		$association->nextAssociation($this->elements);
		return $association;
	}
	
	/*Return true if the given value matches one of the elements values*/
	function includes($value)
	{
		return in_array($value, $this->elements);
	}

	/*Return the number of elements occupied in the array, 
	* not counting the last element*/
	function size()
	{
		$list = $this->elements();
		$size = sizeof($list);
		return $size;
	}
	/*Perform $aMethod, represented with a string name on 
	* the $receiver, on each and everyone of my elements*/
	function doWithEach($aMethod, &$receiver, $extra = null)
	{
		foreach($this->elements() as $iElement)
		{
			$performMethod = $aMethod;
			$receiver->$performMethod($iElement, $extra);
		}
	}
	
	/*Return the first element of the array*/
	function &firstElement()
	{
		$myArray = $this->elements();
		reset($myArray);
		return current($myArray);
	}
	
	/*Return the last element/value pair of the array*/
	function &lastElement()
	{
		end($this->elements);
		$lastRelevant =& each($this->elements);
		reset($this->elements);
		return $lastRelevant;
	}
	
	function searchValueFromLast($aValue)
	{
		end($this->elements);
		$i = null;
		while($i!=$aValue)
		{
			$j = each($this->elements);
			$i = $j[0];
			prev($this->elements);
		}
		return $i;
	}

	function searchValueFromFirst($aValue)
	{
		reset($this->elements);
		$i = null;
		while($i!=$aValue)
		{
			$j = each($this->elements);
			$i = $j[0];
			next($this->elements);
		}
		return $i;
	}
	
	/*Adds value to list, with optional keyname.*/
	function &addElement($value)
	{
		$elements = $this->elements();
		if(func_num_args() == 2)
			$elements[func_get_arg(1)] = $value;
		else
		{
			$elements[] = $value;
		}
		$this->elementsSet($elements);
	}
		
	/*Remove an association from the array with $key as the identifyer */
	function &removeElementByKey($key)
	{
		$elements =& $this->elements();
		unset ($elements[$key]);
		$this->elementsSet($elements);
	}
	
	function &removeElementByValue($value)
	{
		$elements =& $this->elements();
		$key = array_keys($elements, $value);
		if(is_array($key))
			$key = array_pop($key);
		unset ($elements[$key]);
		$this->elementsSet($elements);
	}
 }
?>
Return current item: BowML