<?php
class Zend_Server_Definition implements Countable, Iterator
{
protected $_methods = array();
protected $_overwriteExistingMethods = false;
public function __construct($methods = null)
{
if (is_array($methods)) {
$this->setMethods($methods);
}
}
public function setOverwriteExistingMethods($flag)
{
$this->_overwriteExistingMethods = (bool) $flag;
return $this;
}
public function addMethod($method, $name = null)
{
if (is_array($method)) {
require_once 'Zend/Server/Method/Definition.php';
$method = new Zend_Server_Method_Definition($method);
} elseif (!$method instanceof Zend_Server_Method_Definition) {
require_once 'Zend/Server/Exception.php';
throw new Zend_Server_Exception('Invalid method provided');
}
if (is_numeric($name)) {
$name = null;
}
if (null !== $name) {
$method->setName($name);
} else {
$name = $method->getName();
}
if (null === $name) {
require_once 'Zend/Server/Exception.php';
throw new Zend_Server_Exception('No method name provided');
}
if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) {
require_once 'Zend/Server/Exception.php';
throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name));
}
$this->_methods[$name] = $method;
return $this;
}
public function addMethods(array $methods)
{
foreach ($methods as $key => $method) {
$this->addMethod($method, $key);
}
return $this;
}
public function setMethods(array $methods)
{
$this->clearMethods();
$this->addMethods($methods);
return $this;
}
public function hasMethod($method)
{
return array_key_exists($method, $this->_methods);
}
public function getMethod($method)
{
if ($this->hasMethod($method)) {
return $this->_methods[$method];
}
return false;
}
public function getMethods()
{
return $this->_methods;
}
public function removeMethod($method)
{
if ($this->hasMethod($method)) {
unset($this->_methods[$method]);
}
return $this;
}
public function clearMethods()
{
$this->_methods = array();
return $this;
}
public function toArray()
{
$methods = array();
foreach ($this->getMethods() as $key => $method) {
$methods[$key] = $method->toArray();
}
return $methods;
}
public function count()
{
return count($this->_methods);
}
public function current()
{
return current($this->_methods);
}
public function key()
{
return key($this->_methods);
}
public function next()
{
return next($this->_methods);
}
public function rewind()
{
return reset($this->_methods);
}
public function valid()
{
return (bool) $this->current();
}
}