File indexing completed on 2025-01-19 05:21:28
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Server 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Server methods metadata 0024 * 0025 * @todo Implement iterator 0026 * @category Zend 0027 * @package Zend_Server 0028 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0029 * @license http://framework.zend.com/license/new-bsd New BSD License 0030 */ 0031 class Zend_Server_Definition implements Countable, Iterator 0032 { 0033 /** 0034 * @var array Array of Zend_Server_Method_Definition objects 0035 */ 0036 protected $_methods = array(); 0037 0038 /** 0039 * @var bool Whether or not overwriting existing methods is allowed 0040 */ 0041 protected $_overwriteExistingMethods = false; 0042 0043 /** 0044 * Constructor 0045 * 0046 * @param null|array $methods 0047 * @return void 0048 */ 0049 public function __construct($methods = null) 0050 { 0051 if (is_array($methods)) { 0052 $this->setMethods($methods); 0053 } 0054 } 0055 0056 /** 0057 * Set flag indicating whether or not overwriting existing methods is allowed 0058 * 0059 * @param mixed $flag 0060 * @return void 0061 */ 0062 public function setOverwriteExistingMethods($flag) 0063 { 0064 $this->_overwriteExistingMethods = (bool) $flag; 0065 return $this; 0066 } 0067 0068 /** 0069 * Add method to definition 0070 * 0071 * @param array|Zend_Server_Method_Definition $method 0072 * @param null|string $name 0073 * @return Zend_Server_Definition 0074 * @throws Zend_Server_Exception if duplicate or invalid method provided 0075 */ 0076 public function addMethod($method, $name = null) 0077 { 0078 if (is_array($method)) { 0079 // require_once 'Zend/Server/Method/Definition.php'; 0080 $method = new Zend_Server_Method_Definition($method); 0081 } elseif (!$method instanceof Zend_Server_Method_Definition) { 0082 // require_once 'Zend/Server/Exception.php'; 0083 throw new Zend_Server_Exception('Invalid method provided'); 0084 } 0085 0086 if (is_numeric($name)) { 0087 $name = null; 0088 } 0089 if (null !== $name) { 0090 $method->setName($name); 0091 } else { 0092 $name = $method->getName(); 0093 } 0094 if (null === $name) { 0095 // require_once 'Zend/Server/Exception.php'; 0096 throw new Zend_Server_Exception('No method name provided'); 0097 } 0098 0099 if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) { 0100 // require_once 'Zend/Server/Exception.php'; 0101 throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name)); 0102 } 0103 $this->_methods[$name] = $method; 0104 return $this; 0105 } 0106 0107 /** 0108 * Add multiple methods 0109 * 0110 * @param array $methods Array of Zend_Server_Method_Definition objects or arrays 0111 * @return Zend_Server_Definition 0112 */ 0113 public function addMethods(array $methods) 0114 { 0115 foreach ($methods as $key => $method) { 0116 $this->addMethod($method, $key); 0117 } 0118 return $this; 0119 } 0120 0121 /** 0122 * Set all methods at once (overwrite) 0123 * 0124 * @param array $methods Array of Zend_Server_Method_Definition objects or arrays 0125 * @return Zend_Server_Definition 0126 */ 0127 public function setMethods(array $methods) 0128 { 0129 $this->clearMethods(); 0130 $this->addMethods($methods); 0131 return $this; 0132 } 0133 0134 /** 0135 * Does the definition have the given method? 0136 * 0137 * @param string $method 0138 * @return bool 0139 */ 0140 public function hasMethod($method) 0141 { 0142 return array_key_exists($method, $this->_methods); 0143 } 0144 0145 /** 0146 * Get a given method definition 0147 * 0148 * @param string $method 0149 * @return null|Zend_Server_Method_Definition 0150 */ 0151 public function getMethod($method) 0152 { 0153 if ($this->hasMethod($method)) { 0154 return $this->_methods[$method]; 0155 } 0156 return false; 0157 } 0158 0159 /** 0160 * Get all method definitions 0161 * 0162 * @return array Array of Zend_Server_Method_Definition objects 0163 */ 0164 public function getMethods() 0165 { 0166 return $this->_methods; 0167 } 0168 0169 /** 0170 * Remove a method definition 0171 * 0172 * @param string $method 0173 * @return Zend_Server_Definition 0174 */ 0175 public function removeMethod($method) 0176 { 0177 if ($this->hasMethod($method)) { 0178 unset($this->_methods[$method]); 0179 } 0180 return $this; 0181 } 0182 0183 /** 0184 * Clear all method definitions 0185 * 0186 * @return Zend_Server_Definition 0187 */ 0188 public function clearMethods() 0189 { 0190 $this->_methods = array(); 0191 return $this; 0192 } 0193 0194 /** 0195 * Cast definition to an array 0196 * 0197 * @return array 0198 */ 0199 public function toArray() 0200 { 0201 $methods = array(); 0202 foreach ($this->getMethods() as $key => $method) { 0203 $methods[$key] = $method->toArray(); 0204 } 0205 return $methods; 0206 } 0207 0208 /** 0209 * Countable: count of methods 0210 * 0211 * @return int 0212 */ 0213 public function count() 0214 { 0215 return count($this->_methods); 0216 } 0217 0218 /** 0219 * Iterator: current item 0220 * 0221 * @return mixed 0222 */ 0223 public function current() 0224 { 0225 return current($this->_methods); 0226 } 0227 0228 /** 0229 * Iterator: current item key 0230 * 0231 * @return int|string 0232 */ 0233 public function key() 0234 { 0235 return key($this->_methods); 0236 } 0237 0238 /** 0239 * Iterator: advance to next method 0240 * 0241 * @return void 0242 */ 0243 public function next() 0244 { 0245 return next($this->_methods); 0246 } 0247 0248 /** 0249 * Iterator: return to first method 0250 * 0251 * @return void 0252 */ 0253 public function rewind() 0254 { 0255 return reset($this->_methods); 0256 } 0257 0258 /** 0259 * Iterator: is the current index valid? 0260 * 0261 * @return bool 0262 */ 0263 public function valid() 0264 { 0265 return (bool) $this->current(); 0266 } 0267 }