3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Server
;
16 * Server methods metadata
18 class Definition
implements Countable
, Iterator
21 * @var array Array of \Zend\Server\Method\Definition objects
23 protected $methods = array();
26 * @var bool Whether or not overwriting existing methods is allowed
28 protected $overwriteExistingMethods = false;
33 * @param null|array $methods
35 public function __construct($methods = null)
37 if (is_array($methods)) {
38 $this->setMethods($methods);
43 * Set flag indicating whether or not overwriting existing methods is allowed
46 * @return \Zend\Server\Definition
48 public function setOverwriteExistingMethods($flag)
50 $this->overwriteExistingMethods
= (bool) $flag;
55 * Add method to definition
57 * @param array|\Zend\Server\Method\Definition $method
58 * @param null|string $name
59 * @return \Zend\Server\Definition
60 * @throws \Zend\Server\Exception\InvalidArgumentException if duplicate or invalid method provided
62 public function addMethod($method, $name = null)
64 if (is_array($method)) {
65 $method = new Method\
Definition($method);
66 } elseif (!$method instanceof Method\Definition
) {
67 throw new Exception\
InvalidArgumentException('Invalid method provided');
70 if (is_numeric($name)) {
74 $method->setName($name);
76 $name = $method->getName();
79 throw new Exception\
InvalidArgumentException('No method name provided');
82 if (!$this->overwriteExistingMethods
&& array_key_exists($name, $this->methods
)) {
83 throw new Exception\
InvalidArgumentException(sprintf('Method by name of "%s" already exists', $name));
85 $this->methods
[$name] = $method;
90 * Add multiple methods
92 * @param array $methods Array of \Zend\Server\Method\Definition objects or arrays
93 * @return \Zend\Server\Definition
95 public function addMethods(array $methods)
97 foreach ($methods as $key => $method) {
98 $this->addMethod($method, $key);
104 * Set all methods at once (overwrite)
106 * @param array $methods Array of \Zend\Server\Method\Definition objects or arrays
107 * @return \Zend\Server\Definition
109 public function setMethods(array $methods)
111 $this->clearMethods();
112 $this->addMethods($methods);
117 * Does the definition have the given method?
119 * @param string $method
122 public function hasMethod($method)
124 return array_key_exists($method, $this->methods
);
128 * Get a given method definition
130 * @param string $method
131 * @return null|\Zend\Server\Method\Definition
133 public function getMethod($method)
135 if ($this->hasMethod($method)) {
136 return $this->methods
[$method];
142 * Get all method definitions
144 * @return array Array of \Zend\Server\Method\Definition objects
146 public function getMethods()
148 return $this->methods
;
152 * Remove a method definition
154 * @param string $method
155 * @return \Zend\Server\Definition
157 public function removeMethod($method)
159 if ($this->hasMethod($method)) {
160 unset($this->methods
[$method]);
166 * Clear all method definitions
168 * @return \Zend\Server\Definition
170 public function clearMethods()
172 $this->methods
= array();
177 * Cast definition to an array
181 public function toArray()
184 foreach ($this->getMethods() as $key => $method) {
185 $methods[$key] = $method->toArray();
191 * Countable: count of methods
195 public function count()
197 return count($this->methods
);
201 * Iterator: current item
203 * @return Method\Definition
205 public function current()
207 return current($this->methods
);
211 * Iterator: current item key
215 public function key()
217 return key($this->methods
);
221 * Iterator: advance to next method
223 * @return Method\Definition
225 public function next()
227 return next($this->methods
);
231 * Iterator: return to first method
235 public function rewind()
237 reset($this->methods
);
241 * Iterator: is the current index valid?
245 public function valid()
247 return (bool) $this->current();