Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Server / Definition.php
blobf969ce8dda974dfb46d9353901fe9dcbded8aaf2
1 <?php
2 /**
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
8 */
10 namespace Zend\Server;
12 use Countable;
13 use Iterator;
15 /**
16 * Server methods metadata
18 class Definition implements Countable, Iterator
20 /**
21 * @var array Array of \Zend\Server\Method\Definition objects
23 protected $methods = array();
25 /**
26 * @var bool Whether or not overwriting existing methods is allowed
28 protected $overwriteExistingMethods = false;
30 /**
31 * Constructor
33 * @param null|array $methods
35 public function __construct($methods = null)
37 if (is_array($methods)) {
38 $this->setMethods($methods);
42 /**
43 * Set flag indicating whether or not overwriting existing methods is allowed
45 * @param mixed $flag
46 * @return \Zend\Server\Definition
48 public function setOverwriteExistingMethods($flag)
50 $this->overwriteExistingMethods = (bool) $flag;
51 return $this;
54 /**
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)) {
71 $name = null;
73 if (null !== $name) {
74 $method->setName($name);
75 } else {
76 $name = $method->getName();
78 if (null === $name) {
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;
86 return $this;
89 /**
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);
100 return $this;
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);
113 return $this;
117 * Does the definition have the given method?
119 * @param string $method
120 * @return bool
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];
138 return false;
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]);
162 return $this;
166 * Clear all method definitions
168 * @return \Zend\Server\Definition
170 public function clearMethods()
172 $this->methods = array();
173 return $this;
177 * Cast definition to an array
179 * @return array
181 public function toArray()
183 $methods = array();
184 foreach ($this->getMethods() as $key => $method) {
185 $methods[$key] = $method->toArray();
187 return $methods;
191 * Countable: count of methods
193 * @return int
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
213 * @return int|string
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
233 * @return void
235 public function rewind()
237 reset($this->methods);
241 * Iterator: is the current index valid?
243 * @return bool
245 public function valid()
247 return (bool) $this->current();