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