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:
<?php
/**
* Callable Object
*
* @author Whizark <contact@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2013 Whizark.
* @license MIT
*/
namespace Ecailles\CallableObject;
/**
* Class CallableObject
*
* @package Ecailles\CallableObject
*/
class CallableObject
{
/**
* @var callable The callable.
*/
private $callable = null;
/**
* The constructor.
*
* @param callable $callable A callable.
*/
public function __construct(callable $callable)
{
if (is_string($callable) && strpos($callable, '::') !== false) {
$callable = explode('::', $callable);
}
$this->callable = $callable;
}
/**
* Invokes the callable with parameter(s).
*
* @return mixed The return value.
*/
public function __invoke()
{
return $this->invokeArgs(func_get_args());
}
/**
* Invokes the callable with parameter(s).
*
* @return mixed The return value.
*/
public function invoke()
{
return $this->invokeArgs(func_get_args());
}
/**
* Invokes the callable with an array of parameter(s).
*
* @param array $parameters The parameter(s) to be passed to the callable.
*
* @return mixed The return value.
*/
public function invokeArgs(array $parameters = [])
{
return call_user_func_array($this->callable, $parameters);
}
/**
* Returns the raw callable.
*
* @return callable The callable.
*/
public function get()
{
return $this->callable;
}
/**
* Returns whether the callable is a function.
*
* @return bool True if it is a function, false otherwise.
*/
public function isFunction()
{
return is_string($this->callable);
}
/**
* Returns whether the callable is a closure.
*
* @return bool True if it is a Closure, false otherwise.
*/
public function isClosure()
{
return is_object($this->callable);
}
/**
* Returns whether the callable is an instance method.
*
* @return bool True if it is an instance method, false otherwise.
*/
public function isInstanceMethod()
{
return is_array($this->callable) && is_object($this->callable[0]);
}
/**
* Returns whether the callable is a class method.
*
* @return bool True if it is a class method, false otherwise.
*/
public function isClassMethod()
{
return is_array($this->callable) && is_string($this->callable[0]);
}
}