|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Parameter.php - A parameter of a Jaxon request |
|
5
|
|
|
* |
|
6
|
|
|
* This class is used to create client side requests to the Jaxon functions and callable objects. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
11
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
12
|
|
|
*/ |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
namespace Jaxon\Request\Factory; |
|
15
|
|
|
|
|
16
|
|
|
class Parameter implements Contracts\Parameter |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/* |
|
19
|
|
|
* Request parameters |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
// Specifies that the parameter will consist of an array of form values. |
|
22
|
|
|
const FORM_VALUES = 'FormValues'; |
|
23
|
|
|
// Specifies that the parameter will contain the value of an input control. |
|
24
|
|
|
const INPUT_VALUE = 'InputValue'; |
|
25
|
|
|
// Specifies that the parameter will consist of a boolean value of a checkbox. |
|
26
|
|
|
const CHECKED_VALUE = 'CheckedValue'; |
|
27
|
|
|
// Specifies that the parameter value will be the innerHTML value of the element. |
|
28
|
|
|
const ELEMENT_INNERHTML = 'ElementInnerHTML'; |
|
29
|
|
|
// Specifies that the parameter will be a quoted value (string). |
|
30
|
|
|
const QUOTED_VALUE = 'QuotedValue'; |
|
31
|
|
|
// Specifies that the parameter will be a boolean value (true or false). |
|
32
|
|
|
const BOOL_VALUE = 'BoolValue'; |
|
33
|
|
|
// Specifies that the parameter will be a numeric, non-quoted value. |
|
34
|
|
|
const NUMERIC_VALUE = 'NumericValue'; |
|
35
|
|
|
// Specifies that the parameter will be a non-quoted value |
|
36
|
|
|
// (evaluated by the browsers javascript engine at run time). |
|
37
|
|
|
const JS_VALUE = 'UnquotedValue'; |
|
38
|
|
|
// Specifies that the parameter will be an integer used to generate pagination links. |
|
39
|
|
|
const PAGE_NUMBER = 'PageNumber'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The parameter type |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $sType; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The parameter value |
|
50
|
|
|
* |
|
51
|
|
|
* @var mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $xValue; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $sType The parameter type |
|
|
|
|
|
|
59
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct($sType, $xValue) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->sType = $sType; |
|
|
|
|
|
|
64
|
|
|
$this->xValue = $xValue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the parameter type |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getType() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->sType; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the parameter value |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getValue() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->xValue; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set the parameter value |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setValue($xValue) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->xValue = $xValue; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create a Parameter instance using the given value |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
103
|
|
|
* |
|
104
|
|
|
* @return Parameter |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function make($xValue) |
|
107
|
|
|
{ |
|
108
|
|
|
if($xValue instanceof Contracts\Parameter) |
|
109
|
|
|
{ |
|
110
|
|
|
return $xValue; |
|
111
|
|
|
} |
|
112
|
|
|
if(is_numeric($xValue)) |
|
113
|
|
|
{ |
|
114
|
|
|
return new Parameter(self::NUMERIC_VALUE, $xValue); |
|
115
|
|
|
} |
|
116
|
|
|
if(is_string($xValue)) |
|
117
|
|
|
{ |
|
118
|
|
|
return new Parameter(self::QUOTED_VALUE, $xValue); |
|
119
|
|
|
} |
|
120
|
|
|
if(is_bool($xValue)) |
|
121
|
|
|
{ |
|
122
|
|
|
return new Parameter(self::BOOL_VALUE, $xValue); |
|
123
|
|
|
} |
|
124
|
|
|
// if(is_array($xValue) || is_object($xValue)) |
|
125
|
|
|
{ |
|
126
|
|
|
return new Parameter(self::JS_VALUE, $xValue); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Add quotes to a given value |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $xValue The value to be quoted |
|
|
|
|
|
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
private function getQuotedValue($xValue) |
|
138
|
|
|
{ |
|
139
|
|
|
$sQuoteCharacter = "'"; |
|
140
|
|
|
return $sQuoteCharacter . $xValue . $sQuoteCharacter; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get a js call to Jaxon with a single parameter |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $sFunction The function name |
|
|
|
|
|
|
147
|
|
|
* @param string $sParameter The function parameter |
|
|
|
|
|
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
private function getJsCall($sFunction, $sParameter) |
|
152
|
|
|
{ |
|
153
|
|
|
return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')'; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get the script for an array of form values. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getFormValuesScript() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->getJsCall('getFormValues', $this->xValue); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the script for an input control. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getInputValueScript() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->getJsCall('$', $this->xValue) . '.value'; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the script for a boolean value of a checkbox. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function getCheckedValueScript() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->getJsCall('$', $this->xValue) . '.checked'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the script for the innerHTML value of the element. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function getElementInnerHTMLScript() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->getJsCall('$', $this->xValue) . '.innerHTML'; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Get the script for a quoted value (string). |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function getQuotedValueScript() |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->getQuotedValue(addslashes($this->xValue)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the script for a boolean value (true or false). |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getBoolValueScript() |
|
212
|
|
|
{ |
|
213
|
|
|
return ($this->xValue) ? 'true' : 'false'; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get the script for a numeric, non-quoted value. |
|
218
|
|
|
* |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function getNumericValueScript() |
|
222
|
|
|
{ |
|
223
|
|
|
return (string)$this->xValue; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time). |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
protected function getUnquotedValueScript() |
|
232
|
|
|
{ |
|
233
|
|
|
if(is_array($this->xValue) || is_object($this->xValue)) |
|
234
|
|
|
{ |
|
235
|
|
|
// Unable to use double quotes here because they cannot be handled on client side. |
|
236
|
|
|
// So we are using simple quotes even if the Json standard recommends double quotes. |
|
237
|
|
|
return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT)); |
|
238
|
|
|
} |
|
239
|
|
|
return (string)$this->xValue; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get the script for an integer used to generate pagination links. |
|
244
|
|
|
* |
|
245
|
|
|
* @return string |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getPageNumberScript() |
|
248
|
|
|
{ |
|
249
|
|
|
return (string)$this->xValue; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Generate the javascript code. |
|
254
|
|
|
* |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getScript() |
|
258
|
|
|
{ |
|
259
|
|
|
$sMethodName = 'get' . $this->sType . 'Script'; |
|
260
|
|
|
if(!\method_exists($this, $sMethodName)) |
|
261
|
|
|
{ |
|
262
|
|
|
return ''; |
|
263
|
|
|
} |
|
264
|
|
|
return $this->$sMethodName(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Magic function to generate the jQuery call. |
|
269
|
|
|
* |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function __toString() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->getScript(); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Generate the jQuery call, when converting the response into json. |
|
279
|
|
|
* |
|
280
|
|
|
* This is a method of the JsonSerializable interface. |
|
281
|
|
|
* |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
|
|
public function jsonSerialize() |
|
285
|
|
|
{ |
|
286
|
|
|
return $this->getScript(); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|