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