7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Server
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Server methods metadata
25 * @todo Implement iterator
27 * @package Zend_Server
28 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
29 * @license http://framework.zend.com/license/new-bsd New BSD License
31 class Zend_Server_Definition
implements Countable
, Iterator
34 * @var array Array of Zend_Server_Method_Definition objects
36 protected $_methods = array();
39 * @var bool Whether or not overwriting existing methods is allowed
41 protected $_overwriteExistingMethods = false;
46 * @param null|array $methods
49 public function __construct($methods = null)
51 if (is_array($methods)) {
52 $this->setMethods($methods);
57 * Set flag indicating whether or not overwriting existing methods is allowed
62 public function setOverwriteExistingMethods($flag)
64 $this->_overwriteExistingMethods
= (bool) $flag;
69 * Add method to definition
71 * @param array|Zend_Server_Method_Definition $method
72 * @param null|string $name
73 * @return Zend_Server_Definition
74 * @throws Zend_Server_Exception if duplicate or invalid method provided
76 public function addMethod($method, $name = null)
78 if (is_array($method)) {
79 require_once 'Zend/Server/Method/Definition.php';
80 $method = new Zend_Server_Method_Definition($method);
81 } elseif (!$method instanceof Zend_Server_Method_Definition
) {
82 require_once 'Zend/Server/Exception.php';
83 throw new Zend_Server_Exception('Invalid method provided');
86 if (is_numeric($name)) {
90 $method->setName($name);
92 $name = $method->getName();
95 require_once 'Zend/Server/Exception.php';
96 throw new Zend_Server_Exception('No method name provided');
99 if (!$this->_overwriteExistingMethods
&& array_key_exists($name, $this->_methods
)) {
100 require_once 'Zend/Server/Exception.php';
101 throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name));
103 $this->_methods
[$name] = $method;
108 * Add multiple methods
110 * @param array $methods Array of Zend_Server_Method_Definition objects or arrays
111 * @return Zend_Server_Definition
113 public function addMethods(array $methods)
115 foreach ($methods as $key => $method) {
116 $this->addMethod($method, $key);
122 * Set all methods at once (overwrite)
124 * @param array $methods Array of Zend_Server_Method_Definition objects or arrays
125 * @return Zend_Server_Definition
127 public function setMethods(array $methods)
129 $this->clearMethods();
130 $this->addMethods($methods);
135 * Does the definition have the given method?
137 * @param string $method
140 public function hasMethod($method)
142 return array_key_exists($method, $this->_methods
);
146 * Get a given method definition
148 * @param string $method
149 * @return null|Zend_Server_Method_Definition
151 public function getMethod($method)
153 if ($this->hasMethod($method)) {
154 return $this->_methods
[$method];
160 * Get all method definitions
162 * @return array Array of Zend_Server_Method_Definition objects
164 public function getMethods()
166 return $this->_methods
;
170 * Remove a method definition
172 * @param string $method
173 * @return Zend_Server_Definition
175 public function removeMethod($method)
177 if ($this->hasMethod($method)) {
178 unset($this->_methods
[$method]);
184 * Clear all method definitions
186 * @return Zend_Server_Definition
188 public function clearMethods()
190 $this->_methods
= array();
195 * Cast definition to an array
199 public function toArray()
202 foreach ($this->getMethods() as $key => $method) {
203 $methods[$key] = $method->toArray();
209 * Countable: count of methods
213 public function count()
215 return count($this->_methods
);
219 * Iterator: current item
223 public function current()
225 return current($this->_methods
);
229 * Iterator: current item key
233 public function key()
235 return key($this->_methods
);
239 * Iterator: advance to next method
243 public function next()
245 return next($this->_methods
);
249 * Iterator: return to first method
253 public function rewind()
255 return reset($this->_methods
);
259 * Iterator: is the current index valid?
263 public function valid()
265 return (bool) $this->current();