Trash /src/Reflection/CallableReflection.php

Language PHP Lines 290
MD5 Hash c7863a4864aac1d7b51674b3027bfaa4
Repository https://github.com/Kakwen/Trash.git View Raw File
  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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php

/**
 * Class CallableReflection
 * Callable Reflection: You can reflect a callback and know its type.
 * PHP >= 5.4
 */
class CallableReflection
{

	/**
	 * type const
	 */
	const ERROR_TYPE = 0;
	const FUNCTION_TYPE = 1;
	const CLOSURE_TYPE = 2;
	const INSTANCE_METHOD_TYPE = 3;
	const STATIC_METHOD_TYPE = 4;
	const INVOKED_OBJECT_TYPE = 5;

	/**
	 * reflected callback
	 *
	 * @var callable
	 */
	private $callable;

	/**
	 * type of the reflected callback (const XXX_TYPE)
	 *
	 * @var int
	 */
	private $type;

	/**
	 * initializes the reflected callback
	 *
	 * @param callable $callable
	 */
	function __construct(callable $callable)
	{
		$this->setCallable($callable);
	}

	/**
	 * getter of $callable
	 *
	 * @return callable
	 */
	public function getCallable()
	{
		return $this->callable;
	}

	/**
	 * invokes the reflected callback
	 *
	 * @return mixed
	 */
	public function call()
	{
		return call_user_func_array($this->getCallable(), func_get_args());
	}

	/**
	 * invokes the reflected callback with static binding
	 *
	 * @return mixed
	 */
	public function callStatic()
	{
		return forward_static_call_array($this->getCallable(), func_get_args());
	}

	/**
	 * getter of $type
	 *
	 * @return int
	 */
	public function getType()
	{
		if (is_null($this->type)) {
			$this->initType();
		}
		return $this->type;
	}

	/**
	 * indicates whether the callback is a function
	 *
	 * @return bool
	 */
	public function isFunction()
	{
		return $this->getType() === self::FUNCTION_TYPE;
	}

	/**
	 * indicates whether the callback is a Closure
	 *
	 * @return bool
	 */
	public function isClosure()
	{
		return $this->getType() === self::CLOSURE_TYPE;
	}

	/**
	 * indicates whether the callback is a method
	 *
	 * @return bool
	 */
	public function isMethod()
	{
		return $this->isInstanceMethod() || $this->isStaticMethod();
	}

	/**
	 * indicates whether the callback is a object method
	 *
	 * @return bool
	 */
	public function isInstanceMethod()
	{
		return $this->getType() === self::INSTANCE_METHOD_TYPE;
	}

	/**
	 * indicates whether the callback is a class method
	 *
	 * @return bool
	 */
	public function isStaticMethod()
	{
		return $this->getType() === self::STATIC_METHOD_TYPE;
	}

	/**
	 * indicates whether the callback is an invoked object
	 *
	 * @return bool
	 */
	public function isInvokedObject()
	{
		return $this->getType() === self::INVOKED_OBJECT_TYPE;
	}

	/**
	 * returns the name of the function, if the reflected callback is a function
	 *
	 * @return string
	 */
	public function getFunctionName()
	{
		if ($this->isFunction()) {
			return $this->getCallable();
		}
		return '';
	}

	/**
	 * returns the Closure object, if the reflected callback is a Closure
	 *
	 * @return Closure|null
	 */
	public function getClosure()
	{
		if ($this->isClosure()) {
			return $this->getCallable();
		}
		return null;
	}

	/**
	 * returns the name of the method, if the reflected callback is a method (except __invoke)
	 *
	 * @return string
	 */
	public function getMethodName()
	{
		if ($this->isMethod()) {
			$callable = $this->explodeCallable();
			return $callable[1];
		}
		return '';
	}

	/**
	 * returns the name of the class, if the reflected callback is an object (except Closure)
	 *
	 * @return string
	 */
	public function getClassName()
	{
		if ($this->isMethod() || $this->isInvokedObject()) {
			$callable = $this->explodeCallable();
			if ($this->isInstanceMethod() || $this->isInvokedObject()) {
				return get_class($callable[0]);
			}
			return $callable[0];
		}
		return '';
	}

	/**
	 * returns the object, if the reflected callback is an object (except Closure)
	 *
	 * @return object|null
	 */
	public function getObject()
	{
		if ($this->isInstanceMethod() || $this->isInvokedObject()) {
			$callable = $this->explodeCallable();
			return $callable[0];
		}
		return null;
	}

	/**
	 * setter of $callable
	 *
	 * @param callable $callable
	 */
	private function setCallable(callable $callable)
	{
		$this->callable = $callable;
	}

	/**
	 * explodes the reflected callback in an array and splits method/function from class/object
	 *
	 * @return array
	 */
	private function explodeCallable()
	{
		if (is_string($this->getCallable())) {
			return explode('::', $this->getCallable());
		} elseif (is_array($this->getCallable())) {
			return $this->getCallable();
		} else {
			return array($this->getCallable());
		}
	}

	/**
	 * setter of $type
	 *
	 * @param int $type
	 */
	private function setType($type)
	{
		$this->type = (int)$type;
	}

	/**
	 * initializes $type
	 */
	private function initType()
	{
		if ($this->getCallable() instanceof \Closure) {
			$this->setType(self::CLOSURE_TYPE);

		} else {
			$callable = $this->explodeCallable();
			if (count($callable) === 1) {
				if (is_object($callable[0])) {
					$this->setType(self::INVOKED_OBJECT_TYPE);

				} else {
					$this->setType(self::FUNCTION_TYPE);
				}

			} elseif (count($callable) === 2) {
				if (is_object($callable[0])) {
					$this->setType(self::INSTANCE_METHOD_TYPE);

				} else {
					$this->setType(self::STATIC_METHOD_TYPE);
				}

			} else {
				$this->setType(self::ERROR_TYPE);
				trigger_error("Type not found for callable", E_USER_ERROR);
			}
		}
	}

}
Back to Top