PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Server/Definition.php

http://github.com/michael-romer/zf-boilerplate
PHP | 267 lines | 117 code | 21 blank | 129 comment | 10 complexity | 46f35d924c25c7c48383483bef1d92b1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Server
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Definition.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Server methods metadata
  23. *
  24. * @todo Implement iterator
  25. * @category Zend
  26. * @package Zend_Server
  27. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Server_Definition implements Countable, Iterator
  31. {
  32. /**
  33. * @var array Array of Zend_Server_Method_Definition objects
  34. */
  35. protected $_methods = array();
  36. /**
  37. * @var bool Whether or not overwriting existing methods is allowed
  38. */
  39. protected $_overwriteExistingMethods = false;
  40. /**
  41. * Constructor
  42. *
  43. * @param null|array $methods
  44. * @return void
  45. */
  46. public function __construct($methods = null)
  47. {
  48. if (is_array($methods)) {
  49. $this->setMethods($methods);
  50. }
  51. }
  52. /**
  53. * Set flag indicating whether or not overwriting existing methods is allowed
  54. *
  55. * @param mixed $flag
  56. * @return void
  57. */
  58. public function setOverwriteExistingMethods($flag)
  59. {
  60. $this->_overwriteExistingMethods = (bool) $flag;
  61. return $this;
  62. }
  63. /**
  64. * Add method to definition
  65. *
  66. * @param array|Zend_Server_Method_Definition $method
  67. * @param null|string $name
  68. * @return Zend_Server_Definition
  69. * @throws Zend_Server_Exception if duplicate or invalid method provided
  70. */
  71. public function addMethod($method, $name = null)
  72. {
  73. if (is_array($method)) {
  74. require_once 'Zend/Server/Method/Definition.php';
  75. $method = new Zend_Server_Method_Definition($method);
  76. } elseif (!$method instanceof Zend_Server_Method_Definition) {
  77. require_once 'Zend/Server/Exception.php';
  78. throw new Zend_Server_Exception('Invalid method provided');
  79. }
  80. if (is_numeric($name)) {
  81. $name = null;
  82. }
  83. if (null !== $name) {
  84. $method->setName($name);
  85. } else {
  86. $name = $method->getName();
  87. }
  88. if (null === $name) {
  89. require_once 'Zend/Server/Exception.php';
  90. throw new Zend_Server_Exception('No method name provided');
  91. }
  92. if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) {
  93. require_once 'Zend/Server/Exception.php';
  94. throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name));
  95. }
  96. $this->_methods[$name] = $method;
  97. return $this;
  98. }
  99. /**
  100. * Add multiple methods
  101. *
  102. * @param array $methods Array of Zend_Server_Method_Definition objects or arrays
  103. * @return Zend_Server_Definition
  104. */
  105. public function addMethods(array $methods)
  106. {
  107. foreach ($methods as $key => $method) {
  108. $this->addMethod($method, $key);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Set all methods at once (overwrite)
  114. *
  115. * @param array $methods Array of Zend_Server_Method_Definition objects or arrays
  116. * @return Zend_Server_Definition
  117. */
  118. public function setMethods(array $methods)
  119. {
  120. $this->clearMethods();
  121. $this->addMethods($methods);
  122. return $this;
  123. }
  124. /**
  125. * Does the definition have the given method?
  126. *
  127. * @param string $method
  128. * @return bool
  129. */
  130. public function hasMethod($method)
  131. {
  132. return array_key_exists($method, $this->_methods);
  133. }
  134. /**
  135. * Get a given method definition
  136. *
  137. * @param string $method
  138. * @return null|Zend_Server_Method_Definition
  139. */
  140. public function getMethod($method)
  141. {
  142. if ($this->hasMethod($method)) {
  143. return $this->_methods[$method];
  144. }
  145. return false;
  146. }
  147. /**
  148. * Get all method definitions
  149. *
  150. * @return array Array of Zend_Server_Method_Definition objects
  151. */
  152. public function getMethods()
  153. {
  154. return $this->_methods;
  155. }
  156. /**
  157. * Remove a method definition
  158. *
  159. * @param string $method
  160. * @return Zend_Server_Definition
  161. */
  162. public function removeMethod($method)
  163. {
  164. if ($this->hasMethod($method)) {
  165. unset($this->_methods[$method]);
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Clear all method definitions
  171. *
  172. * @return Zend_Server_Definition
  173. */
  174. public function clearMethods()
  175. {
  176. $this->_methods = array();
  177. return $this;
  178. }
  179. /**
  180. * Cast definition to an array
  181. *
  182. * @return array
  183. */
  184. public function toArray()
  185. {
  186. $methods = array();
  187. foreach ($this->getMethods() as $key => $method) {
  188. $methods[$key] = $method->toArray();
  189. }
  190. return $methods;
  191. }
  192. /**
  193. * Countable: count of methods
  194. *
  195. * @return int
  196. */
  197. public function count()
  198. {
  199. return count($this->_methods);
  200. }
  201. /**
  202. * Iterator: current item
  203. *
  204. * @return mixed
  205. */
  206. public function current()
  207. {
  208. return current($this->_methods);
  209. }
  210. /**
  211. * Iterator: current item key
  212. *
  213. * @return int|string
  214. */
  215. public function key()
  216. {
  217. return key($this->_methods);
  218. }
  219. /**
  220. * Iterator: advance to next method
  221. *
  222. * @return void
  223. */
  224. public function next()
  225. {
  226. return next($this->_methods);
  227. }
  228. /**
  229. * Iterator: return to first method
  230. *
  231. * @return void
  232. */
  233. public function rewind()
  234. {
  235. return reset($this->_methods);
  236. }
  237. /**
  238. * Iterator: is the current index valid?
  239. *
  240. * @return bool
  241. */
  242. public function valid()
  243. {
  244. return (bool) $this->current();
  245. }
  246. }