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

/vendor/zendframework/zend-server/src/Method/Callback.php

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