1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * PHP callback encapsulation.
20: *
21: * @author David Grudl
22: */
23: final class Callback extends Object
24: {
25: /** @var callback */
26: private $cb;
27:
28:
29:
30: /**
31: * Do not call directly, use callback() function.
32: * @param mixed class, object, function, callback
33: * @param string method
34: */
35: public function __construct($t, $m = NULL)
36: {
37: if ($m === NULL) {
38: $this->cb = $t;
39: } else {
40: $this->cb = array($t, $m);
41: }
42:
43: if (!is_callable($this->cb, TRUE)) {
44: throw new \InvalidArgumentException("Invalid callback.");
45: }
46: }
47:
48:
49:
50: /**
51: * Invokes callback. Do not call directly.
52: * @return mixed
53: */
54: public function __invoke()
55: {
56: if (!is_callable($this->cb)) {
57: throw new \InvalidStateException("Callback '$this' is not callable.");
58: }
59: $args = func_get_args();
60: return call_user_func_array($this->cb, $args);
61: }
62:
63:
64:
65: /**
66: * Invokes callback.
67: * @return mixed
68: */
69: public function invoke()
70: {
71: if (!is_callable($this->cb)) {
72: throw new \InvalidStateException("Callback '$this' is not callable.");
73: }
74: $args = func_get_args();
75: return call_user_func_array($this->cb, $args);
76: }
77:
78:
79:
80: /**
81: * Invokes callback with an array of parameters.
82: * @param array
83: * @return mixed
84: */
85: public function invokeArgs(array $args)
86: {
87: if (!is_callable($this->cb)) {
88: throw new \InvalidStateException("Callback '$this' is not callable.");
89: }
90: return call_user_func_array($this->cb, $args);
91: }
92:
93:
94:
95: /**
96: * Verifies that callback can be called.
97: * @return bool
98: */
99: public function isCallable()
100: {
101: return is_callable($this->cb);
102: }
103:
104:
105:
106: /**
107: * Returns PHP callback pseudotype.
108: * @return callback
109: */
110: public function getNative()
111: {
112: return $this->cb;
113: }
114:
115:
116:
117: /**
118: * @return string
119: */
120: public function __toString()
121: {
122: is_callable($this->cb, TRUE, $textual);
123: return $textual;
124: }
125:
126: }
127: