Source for file Collection.class.php
Documentation is available at Collection.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
* Standard Collection Class
* @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 Standard Collection Class
gumbo_load ("Interface_Collection");
class Gumbo_Collection implements Gumbo_Interface_Collection, IteratorAggregate {
/** @var string $_type type of class|interface supplied objects must be */
/** @var array $_list array list of the objects that are part of the Collection */
/** @var string|array$_callback callback function/method that loads values into the Collection */
/** @var array $_params callback function parameters */
/** @var bool $_loaded tells if the Collection has been loaded */
* Constructor (extra arguments sent to callback as is)
* @param string $func callback function/method name
* @param string|StdClass$cls callback class name or object
// checking for additional parameters on the callback function
if ($key <=
1) { continue; }
/** OVERLOADED METHODS **/
* Returns Data (wrapper to get())
* @param int|string$key for integers, use $list->{$num}
public function __get ($key) {
return $this->get ($key);
* Sets Data (wrapper to add())
public function __set ($key, $data) {
$this->add ($data, $key);
* Returns if key is set (wrapper to exists())
* Removes Data (wrapper to remove())
* Adds an Object to the Collection
* @param string|int$key key reference name
* @throws Gumbo_Exception
public function add ($data, $key=
null) {
if (!($data instanceof
$type)) {
$this->_list [$key] =
$data;
// automatically set a new Object
$e->setFunction (__METHOD__
);
* Removes an Object from the Collection
* @param int|string$key key name
* @throws Gumbo_Exception
public function remove ($key) {
// check if the key exists
unset
($this->_list [$key]);
$e->setFunction (__METHOD__
);
* Removes a value from the List (not used)
* Resets the List (not used)
public function reset () {
* Sets the type of class|interface of the Collection
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the callback function/method (extra arguments will be sent to callback as is)
* The callback function will be called to load the contents of the
* Collection. The value could be a function name or a class method. A
* callback function/method should define a single argument, the
* Collection. When the callback is executed, the Collection will
* pass itself (by reference) to the function. The callback will
* set the values of the Collection.
* <code>$coll->setCallback ("function_name");</code>
* To set a class/object method (3 methods):
* $col->setCallback ("method_name", "class_name");
* // substitute an Object ($obj) for "class_name" if necessary
* @precondition function/method callable
* @param string $name function/method name (array should be (0=>"class",1=>"method"))
* @param string|StdClass$obj class name or Object reference
* @throws Gumbo_Exception
$callback =
array ($obj, $name);
// verify if the function/method is callable
$exc_func =
$name .
"()";
$exc_func .=
"::{$exc_class}";
// set the callback function
// checking for additional parameters on the callback function
if ($key <=
1) { continue; }
$e->setFunction (__METHOD__
);
* Executes the callback function
* This method must be called by all methods inside the Collection. This
* will ensure that the Collection is populated only when the data it
* @precondition getCallback ()
* @precondition !isLoaded ()
* @throws Gumbo_Exception
// execute callback function
call_user_function_array ($this->getCallback (), $params);
$e->setFunction (__METHOD__
);
* Returns the corresponding Object associated with the given key
public function get ($key) {
return $this->_list [$key];
$e->setFunction (__METHOD__
);
* Returns the entire List
* Returns an array of the Collection keys
* @precondition size () > 0
public function keys () {
public function exists ($key) {
return isset
($this->_list [$key]);
$e->setFunction (__METHOD__
);
* Returns the size of the Collection
public function size () {
* Returns the Collection Type
* Returns the callback function/method
* Returns the parameters to pass to the callback
* Returns if the Collection has been loaded
* Returns an Iterator object
// return the CollectionIterator