composer package updates
[openemr.git] / vendor / zendframework / zend-server / src / Definition.php
blob05dd69cc13958e55e00db3d9e03a01f2b3939048
1 <?php
2 /**
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
6 */
8 namespace Zend\Server;
10 use Countable;
11 use Iterator;
13 /**
14 * Server methods metadata
16 class Definition implements Countable, Iterator
18 /**
19 * @var array Array of \Zend\Server\Method\Definition objects
21 protected $methods = [];
23 /**
24 * @var bool Whether or not overwriting existing methods is allowed
26 protected $overwriteExistingMethods = false;
28 /**
29 * Constructor
31 * @param null|array $methods
33 public function __construct($methods = null)
35 if (is_array($methods)) {
36 $this->setMethods($methods);
40 /**
41 * Set flag indicating whether or not overwriting existing methods is allowed
43 * @param mixed $flag
44 * @return \Zend\Server\Definition
46 public function setOverwriteExistingMethods($flag)
48 $this->overwriteExistingMethods = (bool) $flag;
49 return $this;
52 /**
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)) {
69 $name = null;
71 if (null !== $name) {
72 $method->setName($name);
73 } else {
74 $name = $method->getName();
76 if (null === $name) {
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;
84 return $this;
87 /**
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);
98 return $this;
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);
111 return $this;
115 * Does the definition have the given method?
117 * @param string $method
118 * @return bool
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];
136 return false;
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]);
160 return $this;
164 * Clear all method definitions
166 * @return \Zend\Server\Definition
168 public function clearMethods()
170 $this->methods = [];
171 return $this;
175 * Cast definition to an array
177 * @return array
179 public function toArray()
181 $methods = [];
182 foreach ($this->getMethods() as $key => $method) {
183 $methods[$key] = $method->toArray();
185 return $methods;
189 * Countable: count of methods
191 * @return int
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
211 * @return int|string
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
231 * @return void
233 public function rewind()
235 reset($this->methods);
239 * Iterator: is the current index valid?
241 * @return bool
243 public function valid()
245 return (bool) $this->current();