[ZF-10089] Zend_Log
[zend.git] / library / Zend / Server / Definition.php
blob7ec4735d4fce6987970018b89d8ee15728aa540b
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
19 * @version $Id$
22 /**
23 * Server methods metadata
25 * @todo Implement iterator
26 * @category Zend
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
33 /**
34 * @var array Array of Zend_Server_Method_Definition objects
36 protected $_methods = array();
38 /**
39 * @var bool Whether or not overwriting existing methods is allowed
41 protected $_overwriteExistingMethods = false;
43 /**
44 * Constructor
46 * @param null|array $methods
47 * @return void
49 public function __construct($methods = null)
51 if (is_array($methods)) {
52 $this->setMethods($methods);
56 /**
57 * Set flag indicating whether or not overwriting existing methods is allowed
59 * @param mixed $flag
60 * @return void
62 public function setOverwriteExistingMethods($flag)
64 $this->_overwriteExistingMethods = (bool) $flag;
65 return $this;
68 /**
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)) {
87 $name = null;
89 if (null !== $name) {
90 $method->setName($name);
91 } else {
92 $name = $method->getName();
94 if (null === $name) {
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;
104 return $this;
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);
118 return $this;
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);
131 return $this;
135 * Does the definition have the given method?
137 * @param string $method
138 * @return bool
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];
156 return false;
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]);
180 return $this;
184 * Clear all method definitions
186 * @return Zend_Server_Definition
188 public function clearMethods()
190 $this->_methods = array();
191 return $this;
195 * Cast definition to an array
197 * @return array
199 public function toArray()
201 $methods = array();
202 foreach ($this->getMethods() as $key => $method) {
203 $methods[$key] = $method->toArray();
205 return $methods;
209 * Countable: count of methods
211 * @return int
213 public function count()
215 return count($this->_methods);
219 * Iterator: current item
221 * @return mixed
223 public function current()
225 return current($this->_methods);
229 * Iterator: current item key
231 * @return int|string
233 public function key()
235 return key($this->_methods);
239 * Iterator: advance to next method
241 * @return void
243 public function next()
245 return next($this->_methods);
249 * Iterator: return to first method
251 * @return void
253 public function rewind()
255 return reset($this->_methods);
259 * Iterator: is the current index valid?
261 * @return bool
263 public function valid()
265 return (bool) $this->current();