PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Server/Method/Prototype.php

https://gitlab.com/grayhamster/open-social-media-monitoring
PHP | 208 lines | 94 code | 14 blank | 100 comment | 10 complexity | 8633c518477ddcfe07ded4fa196b3e16 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Prototype.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. /**
  23. * Method prototype metadata
  24. *
  25. * @category Zend
  26. * @package Zend_Server
  27. * @subpackage Method
  28. * @copyright Copyright (c) 2005-2012 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_Prototype
  32. {
  33. /**
  34. * @var string Return type
  35. */
  36. protected $_returnType = 'void';
  37. /**
  38. * @var array Map parameter names to parameter index
  39. */
  40. protected $_parameterNameMap = array();
  41. /**
  42. * @var array Method parameters
  43. */
  44. protected $_parameters = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param null|array $options
  49. * @return void
  50. */
  51. public function __construct($options = null)
  52. {
  53. if ((null !== $options) && is_array($options)) {
  54. $this->setOptions($options);
  55. }
  56. }
  57. /**
  58. * Set return value
  59. *
  60. * @param string $returnType
  61. * @return Zend_Server_Method_Prototype
  62. */
  63. public function setReturnType($returnType)
  64. {
  65. $this->_returnType = $returnType;
  66. return $this;
  67. }
  68. /**
  69. * Retrieve return type
  70. *
  71. * @return string
  72. */
  73. public function getReturnType()
  74. {
  75. return $this->_returnType;
  76. }
  77. /**
  78. * Add a parameter
  79. *
  80. * @param string $parameter
  81. * @return Zend_Server_Method_Prototype
  82. */
  83. public function addParameter($parameter)
  84. {
  85. if ($parameter instanceof Zend_Server_Method_Parameter) {
  86. $this->_parameters[] = $parameter;
  87. if (null !== ($name = $parameter->getName())) {
  88. $this->_parameterNameMap[$name] = count($this->_parameters) - 1;
  89. }
  90. } else {
  91. require_once 'Zend/Server/Method/Parameter.php';
  92. $parameter = new Zend_Server_Method_Parameter(array(
  93. 'type' => (string) $parameter,
  94. ));
  95. $this->_parameters[] = $parameter;
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Add parameters
  101. *
  102. * @param array $parameter
  103. * @return Zend_Server_Method_Prototype
  104. */
  105. public function addParameters(array $parameters)
  106. {
  107. foreach ($parameters as $parameter) {
  108. $this->addParameter($parameter);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Set parameters
  114. *
  115. * @param array $parameters
  116. * @return Zend_Server_Method_Prototype
  117. */
  118. public function setParameters(array $parameters)
  119. {
  120. $this->_parameters = array();
  121. $this->_parameterNameMap = array();
  122. $this->addParameters($parameters);
  123. return $this;
  124. }
  125. /**
  126. * Retrieve parameters as list of types
  127. *
  128. * @return array
  129. */
  130. public function getParameters()
  131. {
  132. $types = array();
  133. foreach ($this->_parameters as $parameter) {
  134. $types[] = $parameter->getType();
  135. }
  136. return $types;
  137. }
  138. /**
  139. * Get parameter objects
  140. *
  141. * @return array
  142. */
  143. public function getParameterObjects()
  144. {
  145. return $this->_parameters;
  146. }
  147. /**
  148. * Retrieve a single parameter by name or index
  149. *
  150. * @param string|int $index
  151. * @return null|Zend_Server_Method_Parameter
  152. */
  153. public function getParameter($index)
  154. {
  155. if (!is_string($index) && !is_numeric($index)) {
  156. return null;
  157. }
  158. if (array_key_exists($index, $this->_parameterNameMap)) {
  159. $index = $this->_parameterNameMap[$index];
  160. }
  161. if (array_key_exists($index, $this->_parameters)) {
  162. return $this->_parameters[$index];
  163. }
  164. return null;
  165. }
  166. /**
  167. * Set object state from array
  168. *
  169. * @param array $options
  170. * @return Zend_Server_Method_Prototype
  171. */
  172. public function setOptions(array $options)
  173. {
  174. foreach ($options as $key => $value) {
  175. $method = 'set' . ucfirst($key);
  176. if (method_exists($this, $method)) {
  177. $this->$method($value);
  178. }
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Serialize to array
  184. *
  185. * @return array
  186. */
  187. public function toArray()
  188. {
  189. return array(
  190. 'returnType' => $this->getReturnType(),
  191. 'parameters' => $this->getParameters(),
  192. );
  193. }
  194. }