composer package updates
[openemr.git] / vendor / zendframework / zend-server / src / Method / Callback.php
blob7ec7b258ae6cb04d6201be9a9bb620c7283222cd
1 <?php
2 /**
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
6 */
8 namespace Zend\Server\Method;
10 use Zend\Server;
12 /**
13 * Method callback metadata
15 class Callback
17 /**
18 * @var string Class name for class method callback
20 protected $class;
22 /**
23 * @var string|callable Function name or callable for function callback
25 protected $function;
27 /**
28 * @var string Method name for class method callback
30 protected $method;
32 /**
33 * @var string Callback type
35 protected $type;
37 /**
38 * @var array Valid callback types
40 protected $types = ['function', 'static', 'instance'];
42 /**
43 * Constructor
45 * @param null|array $options
47 public function __construct($options = null)
49 if ((null !== $options) && is_array($options)) {
50 $this->setOptions($options);
54 /**
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);
68 return $this;
71 /**
72 * Set callback class
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;
83 return $this;
86 /**
87 * Get callback class
89 * @return string|null
91 public function getClass()
93 return $this->class;
96 /**
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');
106 return $this;
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;
128 return $this;
132 * Get callback class method
134 * @return null|string
136 public function getMethod()
138 return $this->method;
142 * Set callback type
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',
153 $type,
154 __METHOD__
157 $this->type = $type;
158 return $this;
162 * Get callback type
164 * @return string
166 public function getType()
168 return $this->type;
172 * Cast callback to array
174 * @return array
176 public function toArray()
178 $type = $this->getType();
179 $array = [
180 'type' => $type,
182 if ('function' == $type) {
183 $array['function'] = $this->getFunction();
184 } else {
185 $array['class'] = $this->getClass();
186 $array['method'] = $this->getMethod();
188 return $array;