3 * @see https://github.com/zendframework/zend-server for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-server/blob/master/LICENSE.md New BSD License
14 * Server methods metadata
16 class Definition
implements Countable
, Iterator
19 * @var array Array of \Zend\Server\Method\Definition objects
21 protected $methods = [];
24 * @var bool Whether or not overwriting existing methods is allowed
26 protected $overwriteExistingMethods = false;
31 * @param null|array $methods
33 public function __construct($methods = null)
35 if (is_array($methods)) {
36 $this->setMethods($methods);
41 * Set flag indicating whether or not overwriting existing methods is allowed
44 * @return \Zend\Server\Definition
46 public function setOverwriteExistingMethods($flag)
48 $this->overwriteExistingMethods
= (bool) $flag;
53 * Add method to definition
55 * @param array|\Zend\Server\Method\Definition $method
56 * @param null|string $name
57 * @return \Zend\Server\Definition
58 * @throws \Zend\Server\Exception\InvalidArgumentException if duplicate or invalid method provided
60 public function addMethod($method, $name = null)
62 if (is_array($method)) {
63 $method = new Method\
Definition($method);
64 } elseif (! $method instanceof Method\Definition
) {
65 throw new Exception\
InvalidArgumentException('Invalid method provided');
68 if (is_numeric($name)) {
72 $method->setName($name);
74 $name = $method->getName();
77 throw new Exception\
InvalidArgumentException('No method name provided');
80 if (! $this->overwriteExistingMethods
&& array_key_exists($name, $this->methods
)) {
81 throw new Exception\
InvalidArgumentException(sprintf('Method by name of "%s" already exists', $name));
83 $this->methods
[$name] = $method;
88 * Add multiple methods
90 * @param array $methods Array of \Zend\Server\Method\Definition objects or arrays
91 * @return \Zend\Server\Definition
93 public function addMethods(array $methods)
95 foreach ($methods as $key => $method) {
96 $this->addMethod($method, $key);
102 * Set all methods at once (overwrite)
104 * @param array $methods Array of \Zend\Server\Method\Definition objects or arrays
105 * @return \Zend\Server\Definition
107 public function setMethods(array $methods)
109 $this->clearMethods();
110 $this->addMethods($methods);
115 * Does the definition have the given method?
117 * @param string $method
120 public function hasMethod($method)
122 return array_key_exists($method, $this->methods
);
126 * Get a given method definition
128 * @param string $method
129 * @return null|\Zend\Server\Method\Definition
131 public function getMethod($method)
133 if ($this->hasMethod($method)) {
134 return $this->methods
[$method];
140 * Get all method definitions
142 * @return array Array of \Zend\Server\Method\Definition objects
144 public function getMethods()
146 return $this->methods
;
150 * Remove a method definition
152 * @param string $method
153 * @return \Zend\Server\Definition
155 public function removeMethod($method)
157 if ($this->hasMethod($method)) {
158 unset($this->methods
[$method]);
164 * Clear all method definitions
166 * @return \Zend\Server\Definition
168 public function clearMethods()
175 * Cast definition to an array
179 public function toArray()
182 foreach ($this->getMethods() as $key => $method) {
183 $methods[$key] = $method->toArray();
189 * Countable: count of methods
193 public function count()
195 return count($this->methods
);
199 * Iterator: current item
201 * @return Method\Definition
203 public function current()
205 return current($this->methods
);
209 * Iterator: current item key
213 public function key()
215 return key($this->methods
);
219 * Iterator: advance to next method
221 * @return Method\Definition
223 public function next()
225 return next($this->methods
);
229 * Iterator: return to first method
233 public function rewind()
235 reset($this->methods
);
239 * Iterator: is the current index valid?
243 public function valid()
245 return (bool) $this->current();