/Zend/Server/Method/Definition.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 299 lines · 129 code · 25 blank · 145 comment · 7 complexity · c7be35a7409e7bd3dd25dbd1b9535d3f MD5 · raw file

  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. * @subpackage Method
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Definition.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Server\Method;
  26. use Zend\Server;
  27. /**
  28. * Method definition metadata
  29. *
  30. * @category Zend
  31. * @package Zend_Server
  32. * @subpackage Method
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Definition
  37. {
  38. /**
  39. * @var Zend_Server_Method_Callback
  40. */
  41. protected $_callback;
  42. /**
  43. * @var array
  44. */
  45. protected $_invokeArguments = array();
  46. /**
  47. * @var string
  48. */
  49. protected $_methodHelp = '';
  50. /**
  51. * @var string
  52. */
  53. protected $_name;
  54. /**
  55. * @var null|object
  56. */
  57. protected $_object;
  58. /**
  59. * @var array Array of Zend_Server_Method_Prototype objects
  60. */
  61. protected $_prototypes = array();
  62. /**
  63. * Constructor
  64. *
  65. * @param null|array $options
  66. * @return void
  67. */
  68. public function __construct($options = null)
  69. {
  70. if ((null !== $options) && is_array($options)) {
  71. $this->setOptions($options);
  72. }
  73. }
  74. /**
  75. * Set object state from options
  76. *
  77. * @param array $options
  78. * @return Zend_Server_Method_Definition
  79. */
  80. public function setOptions(array $options)
  81. {
  82. foreach ($options as $key => $value) {
  83. $method = 'set' . ucfirst($key);
  84. if (method_exists($this, $method)) {
  85. $this->$method($value);
  86. }
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Set method name
  92. *
  93. * @param string $name
  94. * @return Zend_Server_Method_Definition
  95. */
  96. public function setName($name)
  97. {
  98. $this->_name = (string) $name;
  99. return $this;
  100. }
  101. /**
  102. * Get method name
  103. *
  104. * @return string
  105. */
  106. public function getName()
  107. {
  108. return $this->_name;
  109. }
  110. /**
  111. * Set method callback
  112. *
  113. * @param array|Zend_Server_Method_Callback $callback
  114. * @return Zend_Server_Method_Definition
  115. */
  116. public function setCallback($callback)
  117. {
  118. if (is_array($callback)) {
  119. require_once 'Zend/Server/Method/Callback.php';
  120. $callback = new Callback($callback);
  121. } elseif (!$callback instanceof Callback) {
  122. require_once 'Zend/Server/Exception.php';
  123. throw new Server\Exception('Invalid method callback provided');
  124. }
  125. $this->_callback = $callback;
  126. return $this;
  127. }
  128. /**
  129. * Get method callback
  130. *
  131. * @return Zend_Server_Method_Callback
  132. */
  133. public function getCallback()
  134. {
  135. return $this->_callback;
  136. }
  137. /**
  138. * Add prototype to method definition
  139. *
  140. * @param array|Zend_Server_Method_Prototype $prototype
  141. * @return Zend_Server_Method_Definition
  142. */
  143. public function addPrototype($prototype)
  144. {
  145. if (is_array($prototype)) {
  146. require_once 'Zend/Server/Method/Prototype.php';
  147. $prototype = new Prototype($prototype);
  148. } elseif (!$prototype instanceof Prototype) {
  149. require_once 'Zend/Server/Exception.php';
  150. throw new Server\Exception('Invalid method prototype provided');
  151. }
  152. $this->_prototypes[] = $prototype;
  153. return $this;
  154. }
  155. /**
  156. * Add multiple prototypes at once
  157. *
  158. * @param array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
  159. * @return Zend_Server_Method_Definition
  160. */
  161. public function addPrototypes(array $prototypes)
  162. {
  163. foreach ($prototypes as $prototype) {
  164. $this->addPrototype($prototype);
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Set all prototypes at once (overwrites)
  170. *
  171. * @param array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
  172. * @return Zend_Server_Method_Definition
  173. */
  174. public function setPrototypes(array $prototypes)
  175. {
  176. $this->_prototypes = array();
  177. $this->addPrototypes($prototypes);
  178. return $this;
  179. }
  180. /**
  181. * Get all prototypes
  182. *
  183. * @return array $prototypes Array of Zend_Server_Method_Prototype objects or arrays
  184. */
  185. public function getPrototypes()
  186. {
  187. return $this->_prototypes;
  188. }
  189. /**
  190. * Set method help
  191. *
  192. * @param string $methodHelp
  193. * @return Zend_Server_Method_Definition
  194. */
  195. public function setMethodHelp($methodHelp)
  196. {
  197. $this->_methodHelp = (string) $methodHelp;
  198. return $this;
  199. }
  200. /**
  201. * Get method help
  202. *
  203. * @return string
  204. */
  205. public function getMethodHelp()
  206. {
  207. return $this->_methodHelp;
  208. }
  209. /**
  210. * Set object to use with method calls
  211. *
  212. * @param object $object
  213. * @return Zend_Server_Method_Definition
  214. */
  215. public function setObject($object)
  216. {
  217. if (!is_object($object) && (null !== $object)) {
  218. require_once 'Zend/Server/Exception.php';
  219. throw new Server\Exception('Invalid object passed to ' . __CLASS__ . '::' . __METHOD__);
  220. }
  221. $this->_object = $object;
  222. return $this;
  223. }
  224. /**
  225. * Get object to use with method calls
  226. *
  227. * @return null|object
  228. */
  229. public function getObject()
  230. {
  231. return $this->_object;
  232. }
  233. /**
  234. * Set invoke arguments
  235. *
  236. * @param array $invokeArguments
  237. * @return Zend_Server_Method_Definition
  238. */
  239. public function setInvokeArguments(array $invokeArguments)
  240. {
  241. $this->_invokeArguments = $invokeArguments;
  242. return $this;
  243. }
  244. /**
  245. * Retrieve invoke arguments
  246. *
  247. * @return array
  248. */
  249. public function getInvokeArguments()
  250. {
  251. return $this->_invokeArguments;
  252. }
  253. /**
  254. * Serialize to array
  255. *
  256. * @return array
  257. */
  258. public function toArray()
  259. {
  260. $prototypes = $this->getPrototypes();
  261. $signatures = array();
  262. foreach ($prototypes as $prototype) {
  263. $signatures[] = $prototype->toArray();
  264. }
  265. return array(
  266. 'name' => $this->getName(),
  267. 'callback' => $this->getCallback()->toArray(),
  268. 'prototypes' => $signatures,
  269. 'methodHelp' => $this->getMethodHelp(),
  270. 'invokeArguments' => $this->getInvokeArguments(),
  271. 'object' => $this->getObject(),
  272. );
  273. }
  274. }