PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Server/Method/Definition.php

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