Callback.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
interface ICallbackNamed {
function hasName();
function getName();
}
/**
* Callback class introduces currying-like pattern.
*
* Example:
* function foo($param1, $param2, $param3) {
* var_dump($param1, $param2, $param3);
* }
* $fooCurried = new Callback('foo',
* 'param1 is now statically set',
* new CallbackParam, new CallbackParam
* );
* phpQuery::callbackRun($fooCurried,
* array('param2 value', 'param3 value'
* );
*
* Callback class is supported in all phpQuery methods which accepts callbacks.
*
* @link http://code.google.com/p/phpquery/wiki/Callbacks#Param_Structures
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
*
* @TODO??? return fake forwarding function created via create_function
* @TODO honor paramStructure
*/
class Callback
implements ICallbackNamed {
public $callback = null;
public $params = null;
protected $name;
public function __construct($callback, $param1 = null, $param2 = null,
$param3 = null) {
$params = func_get_args();
$params = array_slice($params, 1);
if ($callback instanceof Callback) {
// TODO implement recurention
} else {
$this->callback = $callback;
$this->params = $params;
}
}
public function getName() {
return 'Callback: '.$this->name;
}
public function hasName() {
return isset($this->name) && $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
// TODO test me
// public function addParams() {
// $params = func_get_args();
// return new Callback($this->callback, $this->params+$params);
// }
}
/**
* Shorthand for new Callback(create_function(...), ...);
*
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
*/
class CallbackBody extends Callback {
public function __construct($paramList, $code, $param1 = null, $param2 = null,
$param3 = null) {
$params = func_get_args();
$params = array_slice($params, 2);
$this->callback = create_function($paramList, $code);
$this->params = $params;
}
}
/**
* Callback type which on execution returns reference passed during creation.
*
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
*/
class CallbackReturnReference extends Callback
implements ICallbackNamed {
protected $reference;
public function __construct(&$reference, $name = null){
$this->reference =& $reference;
$this->callback = array($this, 'callback');
}
public function callback() {
return $this->reference;
}
public function getName() {
return 'Callback: '.$this->name;
}
public function hasName() {
return isset($this->name) && $this->name;
}
}
/**
* Callback type which on execution returns value passed during creation.
*
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
*/
class CallbackReturnValue extends Callback
implements ICallbackNamed {
protected $value;
protected $name;
public function __construct($value, $name = null){
$this->value =& $value;
$this->name = $name;
$this->callback = array($this, 'callback');
}
public function callback() {
return $this->value;
}
public function __toString() {
return $this->getName();
}
public function getName() {
return 'Callback: '.$this->name;
}
public function hasName() {
return isset($this->name) && $this->name;
}
}
/**
* CallbackParameterToReference can be used when we don't really want a callback,
* only parameter passed to it. CallbackParameterToReference takes first
* parameter's value and passes it to reference.
*
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
*/
class CallbackParameterToReference extends Callback {
/**
* @param $reference
* @TODO implement $paramIndex;
* param index choose which callback param will be passed to reference
*/
public function __construct(&$reference){
$this->callback =& $reference;
}
}
//class CallbackReference extends Callback {
// /**
// *
// * @param $reference
// * @param $paramIndex
// * @todo implement $paramIndex; param index choose which callback param will be passed to reference
// */
// public function __construct(&$reference, $name = null){
// $this->callback =& $reference;
// }
//}
class CallbackParam {}