<?php
class Class_Method_Collection implements Iterator
{
/**
* For:
* $this->_items[0]['name'] = '<method name>'; | myMethod
* $this->_items[0]['return'] = '<variable type>'; | integer / string / etc.
* $this->_items[0]['params'][0]['name'] = '<param name>'; | my_variable
* $this->_items[0]['params'][0]['type'] = '<param type>'; | integer / string / etc.
* or
* $this->_items[0] = Class_Methods_Method
* @var array
*/
protected
$_items = array(); /**
* Iterator index
* @var integer
*/
protected $_key = 0;
/**
* Identifiers stored in array
* @var array
*/
protected $_keys;
/**
* Converts data from array to Class_Methods_Method object
* For:
* $method['name'] = '<method name>'; | myMethod
* $method['return'] = '<variable type>'; | integer / string / etc.
* $method['params'][0]['name'] = '<param name>'; | my_variable
* $method['params'][0]['type'] = '<param type>'; | integer / string / etc.
* @param array $method
*/
public function array2method($method)
{
$method['return'] = isset($method['return']) ?
$method['return'] 'void';
$ob = InstanceGet('library.class.method', '', InstanceRETURN_INSTANCE);
$ob->setName($method['name']);
$ob->setReturn($method['return']);
foreach($method['params'] as $param)
{
$ob->setParam($param['name'], $param['type']);
}
return $ob;
}
/**
* Loads methods collection in array format as class items for further operations
* $this->_items[0]['name'] = '<method name>'; | myMethod
* $this->_items[0]['return'] = '<variable type>'; | integer / string / etc.
* $this->_items[0]['params'][0]['name'] = '<param name>'; | my_variable
* $this->_items[0]['params'][0]['type'] = '<param type>'; | integer / string / etc.
* or
* $this->_items[0] = Class_Methods_Method
* @param array $methods
*/
public function load($methods)
{
if($this->validate($methods)) throw new Exception('Incorrect $methods format.');
$this->_items = $methods;
}
/**
* Compares format of $methods array with required by load() method
* @param array $methods
*/
public function validate($methods)
{
$out = true;
foreach($methods as $method)
{
if(!isset($method['name'])) {
$out = false; break;
} else
if(isset($method['params'])) {
foreach($method['params'] as $param)
{
if(!isset($param['name']) || !isset($param['type'])) {
$out = false; break;
}
}
}
if(!$out) break;
}
return $out;
}
// ----------------- //
// SETTERS & GETTERS //
// ----------------- //
/**
* Returns item
* @param integer $key
* @return misc
*/
public function getItem($key)
{
return isset($this->_keys
[$key]) && isset($this->_items
[$this->_keys
[$key]]) ?
$this->_items
[$this->_keys
[$key]] false; }
/**
* Sets item
* @param integer $key
* @param misc $item
*/
public function setItem($key, $item)
{
if(isset($this->_items
[$key])) {
$this->_items[$key] = $item;
}
else
{
$this->_keys[] = $key;
$this->_items[$key] = $item;
}
}
// ---------------- //
// ITERATOR METHODS //
// ---------------- //
/**
* Returns current item
* @return misc
*/
{
return $this->valid() ? $this->array2method($this->_items[$this->_keys[$this->_key]]) false;
}
/**
* Return key of current item or boolean false if item does not exists
*/
{
return $this->valid() ? $this->_keys[$this->_key] false;
}
/**
* Moves forward to next item
*/
{
$this->_key += 1;
}
/**
* Checks does current item exists
* @return boolean
*/
public function valid()
{
return (isset($this->_keys
[$this->_key
]) && isset($this->_items
[$this->_keys
[$this->_key
]])); }
/**
* Rewinds Iterator to the first element
*/
{
$this->_key = 0;
}
}
?>