1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: final class NCallback extends NObject
25: {
26:
27: private $cb;
28:
29:
30:
31: 32: 33: 34: 35:
36: public function __construct($t, $m = NULL)
37: {
38: if ($m === NULL) {
39: if (is_string($t)) {
40: $t = explode('::', $t, 2);
41: $this->cb = isset($t[1]) ? $t : $t[0];
42: } elseif (is_object($t)) {
43: $this->cb = $t instanceof Closure ? $t : array($t, '__invoke');
44: } else {
45: $this->cb = $t;
46: }
47:
48: } else {
49: $this->cb = array($t, $m);
50: }
51:
52:
53: if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
54: $this->cb = substr($this->cb, $a + 1);
55:
56: } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
57: $this->cb[0] = substr($this->cb[0], $a + 1);
58: }
59:
60: if (!is_callable($this->cb, TRUE)) {
61: throw new InvalidArgumentException("Invalid callback.");
62: }
63: }
64:
65:
66:
67: 68: 69: 70:
71: public function __invoke()
72: {
73: if (!is_callable($this->cb)) {
74: throw new InvalidStateException("Callback '$this' is not callable.");
75: }
76: $args = func_get_args();
77: return call_user_func_array($this->cb, $args);
78: }
79:
80:
81:
82: 83: 84: 85:
86: public function invoke()
87: {
88: if (!is_callable($this->cb)) {
89: throw new InvalidStateException("Callback '$this' is not callable.");
90: }
91: $args = func_get_args();
92: return call_user_func_array($this->cb, $args);
93: }
94:
95:
96:
97: 98: 99: 100: 101:
102: public function invokeArgs(array $args)
103: {
104: if (!is_callable($this->cb)) {
105: throw new InvalidStateException("Callback '$this' is not callable.");
106: }
107: return call_user_func_array($this->cb, $args);
108: }
109:
110:
111:
112: 113: 114: 115:
116: public function isCallable()
117: {
118: return is_callable($this->cb);
119: }
120:
121:
122:
123: 124: 125: 126:
127: public function getNative()
128: {
129: return $this->cb;
130: }
131:
132:
133:
134: 135: 136: 137:
138: public function toReflection()
139: {
140: if (is_array($this->cb)) {
141: return new NMethodReflection($this->cb[0], $this->cb[1]);
142: } else {
143: return new NFunctionReflection($this->cb);
144: }
145: }
146:
147:
148:
149: 150: 151:
152: public function isStatic()
153: {
154: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
155: }
156:
157:
158:
159: 160: 161:
162: public function __toString()
163: {
164: if ($this->cb instanceof Closure) {
165: return '{closure}';
166: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
167: return '{lambda}';
168: } else {
169: is_callable($this->cb, TRUE, $textual);
170: return $textual;
171: }
172: }
173:
174: }
175: