3 * @see https://github.com/zendframework/zend-server for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-server/blob/master/LICENSE.md New BSD License
8 namespace Zend\Server\Method
;
13 * Method callback metadata
18 * @var string Class name for class method callback
23 * @var string|callable Function name or callable for function callback
28 * @var string Method name for class method callback
33 * @var string Callback type
38 * @var array Valid callback types
40 protected $types = ['function', 'static', 'instance'];
45 * @param null|array $options
47 public function __construct($options = null)
49 if ((null !== $options) && is_array($options)) {
50 $this->setOptions($options);
55 * Set object state from array of options
57 * @param array $options
58 * @return \Zend\Server\Method\Callback
60 public function setOptions(array $options)
62 foreach ($options as $key => $value) {
63 $method = 'set' . ucfirst($key);
64 if (method_exists($this, $method)) {
65 $this->$method($value);
74 * @param string $class
75 * @return \Zend\Server\Method\Callback
77 public function setClass($class)
79 if (is_object($class)) {
80 $class = get_class($class);
82 $this->class = $class;
91 public function getClass()
97 * Set callback function
99 * @param string|callable $function
100 * @return \Zend\Server\Method\Callback
102 public function setFunction($function)
104 $this->function = is_callable($function) ?
$function : (string) $function;
105 $this->setType('function');
110 * Get callback function
112 * @return null|string|callable
114 public function getFunction()
116 return $this->function;
120 * Set callback class method
122 * @param string $method
123 * @return \Zend\Server\Method\Callback
125 public function setMethod($method)
127 $this->method
= $method;
132 * Get callback class method
134 * @return null|string
136 public function getMethod()
138 return $this->method
;
144 * @param string $type
145 * @return \Zend\Server\Method\Callback
146 * @throws Server\Exception\InvalidArgumentException
148 public function setType($type)
150 if (! in_array($type, $this->types
)) {
151 throw new Server\Exception\
InvalidArgumentException(sprintf(
152 'Invalid method callback type "%s" passed to %s',
166 public function getType()
172 * Cast callback to array
176 public function toArray()
178 $type = $this->getType();
182 if ('function' == $type) {
183 $array['function'] = $this->getFunction();
185 $array['class'] = $this->getClass();
186 $array['method'] = $this->getMethod();