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:
37: public static function create($callback, $m = NULL)
38: {
39: return new self($callback, $m);
40: }
41:
42:
43:
44: 45: 46: 47:
48: public function __construct($cb, $m = NULL)
49: {
50: if ($m !== NULL) {
51: $cb = array($cb, $m);
52:
53: } elseif ($cb instanceof self) {
54: $this->cb = $cb->cb;
55: return;
56: }
57:
58: if (PHP_VERSION_ID < 50202 && is_string($cb) && strpos($cb, '::')) {
59: $cb = explode('::', $cb, 2);
60: } elseif (is_object($cb) && !$cb instanceof NClosure) {
61: $cb = array($cb, '__invoke');
62: }
63:
64:
65: if (is_string($this->cb) && $a = strrpos($this->cb, '\\')) {
66: $this->cb = substr($this->cb, $a + 1);
67:
68: } elseif (is_array($this->cb) && is_string($this->cb[0]) && $a = strrpos($this->cb[0], '\\')) {
69: $this->cb[0] = substr($this->cb[0], $a + 1);
70: }
71: if (!is_callable($cb, TRUE)) {
72: throw new InvalidArgumentException("Invalid callback.");
73: }
74: $this->cb = $cb;
75: }
76:
77:
78:
79: 80: 81: 82:
83: public function __invoke()
84: {
85: if (!is_callable($this->cb)) {
86: throw new InvalidStateException("Callback '$this' is not callable.");
87: }
88: $args = func_get_args();
89: return call_user_func_array($this->cb, $args);
90: }
91:
92:
93:
94: 95: 96: 97:
98: public function invoke()
99: {
100: if (!is_callable($this->cb)) {
101: throw new InvalidStateException("Callback '$this' is not callable.");
102: }
103: $args = func_get_args();
104: return call_user_func_array($this->cb, $args);
105: }
106:
107:
108:
109: 110: 111: 112: 113:
114: public function invokeArgs(array $args)
115: {
116: if (!is_callable($this->cb)) {
117: throw new InvalidStateException("Callback '$this' is not callable.");
118: }
119: return call_user_func_array($this->cb, $args);
120: }
121:
122:
123:
124: 125: 126: 127:
128: public function isCallable()
129: {
130: return is_callable($this->cb);
131: }
132:
133:
134:
135: 136: 137: 138:
139: public function getNative()
140: {
141: return $this->cb;
142: }
143:
144:
145:
146: 147: 148: 149:
150: public function toReflection()
151: {
152: if (is_string($this->cb) && strpos($this->cb, '::')) {
153: return new NMethodReflection($this->cb);
154: } elseif (is_array($this->cb)) {
155: return new NMethodReflection($this->cb[0], $this->cb[1]);
156: } elseif (is_object($this->cb) && !$this->cb instanceof Closure) {
157: return new NMethodReflection($this->cb, '__invoke');
158: } else {
159: return new NFunctionReflection($this->cb);
160: }
161: }
162:
163:
164:
165: 166: 167:
168: public function isStatic()
169: {
170: return is_array($this->cb) ? is_string($this->cb[0]) : is_string($this->cb);
171: }
172:
173:
174:
175: 176: 177:
178: public function __toString()
179: {
180: if ($this->cb instanceof Closure) {
181: return '{closure}';
182: } elseif (is_string($this->cb) && $this->cb[0] === "\0") {
183: return '{lambda}';
184: } else {
185: is_callable($this->cb, TRUE, $textual);
186: return $textual;
187: }
188: }
189:
190: }
191: