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

/vendor/zendframework/zendframework/library/Zend/Server/Method/Callback.php

https://bitbucket.org/pcelta/zf2
PHP | 193 lines | 84 code | 18 blank | 91 comment | 8 complexity | 53909f36bfc819b7341243710cae64fd MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Server
  9. */
  10. namespace Zend\Server\Method;
  11. use Zend\Server;
  12. /**
  13. * Method callback metadata
  14. *
  15. * @category Zend
  16. * @package Zend_Server
  17. * @subpackage Zend_Server_Method
  18. */
  19. class Callback
  20. {
  21. /**
  22. * @var string Class name for class method callback
  23. */
  24. protected $class;
  25. /**
  26. * @var string Function name for function callback
  27. */
  28. protected $function;
  29. /**
  30. * @var string Method name for class method callback
  31. */
  32. protected $method;
  33. /**
  34. * @var string Callback type
  35. */
  36. protected $type;
  37. /**
  38. * @var array Valid callback types
  39. */
  40. protected $types = array('function', 'static', 'instance');
  41. /**
  42. * Constructor
  43. *
  44. * @param null|array $options
  45. */
  46. public function __construct($options = null)
  47. {
  48. if ((null !== $options) && is_array($options)) {
  49. $this->setOptions($options);
  50. }
  51. }
  52. /**
  53. * Set object state from array of options
  54. *
  55. * @param array $options
  56. * @return \Zend\Server\Method\Callback
  57. */
  58. public function setOptions(array $options)
  59. {
  60. foreach ($options as $key => $value) {
  61. $method = 'set' . ucfirst($key);
  62. if (method_exists($this, $method)) {
  63. $this->$method($value);
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Set callback class
  70. *
  71. * @param string $class
  72. * @return \Zend\Server\Method\Callback
  73. */
  74. public function setClass($class)
  75. {
  76. if (is_object($class)) {
  77. $class = get_class($class);
  78. }
  79. $this->class = $class;
  80. return $this;
  81. }
  82. /**
  83. * Get callback class
  84. *
  85. * @return string|null
  86. */
  87. public function getClass()
  88. {
  89. return $this->class;
  90. }
  91. /**
  92. * Set callback function
  93. *
  94. * @param string $function
  95. * @return \Zend\Server\Method\Callback
  96. */
  97. public function setFunction($function)
  98. {
  99. $this->function = (string) $function;
  100. $this->setType('function');
  101. return $this;
  102. }
  103. /**
  104. * Get callback function
  105. *
  106. * @return null|string
  107. */
  108. public function getFunction()
  109. {
  110. return $this->function;
  111. }
  112. /**
  113. * Set callback class method
  114. *
  115. * @param string $method
  116. * @return \Zend\Server\Method\Callback
  117. */
  118. public function setMethod($method)
  119. {
  120. $this->method = $method;
  121. return $this;
  122. }
  123. /**
  124. * Get callback class method
  125. *
  126. * @return null|string
  127. */
  128. public function getMethod()
  129. {
  130. return $this->method;
  131. }
  132. /**
  133. * Set callback type
  134. *
  135. * @param string $type
  136. * @return Callback
  137. * @throws Server\Exception\InvalidArgumentException
  138. */
  139. public function setType($type)
  140. {
  141. if (!in_array($type, $this->types)) {
  142. throw new Server\Exception\InvalidArgumentException('Invalid method callback type "' . $type . '" passed to ' . __CLASS__ . '::' . __METHOD__);
  143. }
  144. $this->type = $type;
  145. return $this;
  146. }
  147. /**
  148. * Get callback type
  149. *
  150. * @return string
  151. */
  152. public function getType()
  153. {
  154. return $this->type;
  155. }
  156. /**
  157. * Cast callback to array
  158. *
  159. * @return array
  160. */
  161. public function toArray()
  162. {
  163. $type = $this->getType();
  164. $array = array(
  165. 'type' => $type,
  166. );
  167. if ('function' == $type) {
  168. $array['function'] = $this->getFunction();
  169. } else {
  170. $array['class'] = $this->getClass();
  171. $array['method'] = $this->getMethod();
  172. }
  173. return $array;
  174. }
  175. }