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