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\Call;
|
15
|
|
|
|
16
|
|
|
use function is_array;
|
17
|
|
|
use function is_bool;
|
18
|
|
|
use function is_numeric;
|
19
|
|
|
use function is_object;
|
20
|
|
|
use function is_string;
|
21
|
|
|
use function json_encode;
|
22
|
|
|
use function method_exists;
|
23
|
|
|
use function str_replace;
|
24
|
|
|
|
25
|
|
|
class Parameter implements ParameterInterface
|
|
|
|
|
26
|
|
|
{
|
27
|
|
|
/*
|
28
|
|
|
* Request parameters
|
29
|
|
|
*/
|
|
|
|
|
30
|
|
|
// Specifies that the parameter will consist of an array of form values.
|
31
|
|
|
const FORM_VALUES = 'FormValues';
|
32
|
|
|
// Specifies that the parameter will contain the value of an input control.
|
33
|
|
|
const INPUT_VALUE = 'InputValue';
|
34
|
|
|
// Specifies that the parameter will consist of a bool value of a checkbox.
|
35
|
|
|
const CHECKED_VALUE = 'CheckedValue';
|
36
|
|
|
// Specifies that the parameter value will be the innerHTML value of the element.
|
37
|
|
|
const ELEMENT_INNERHTML = 'ElementInnerHTML';
|
38
|
|
|
// Specifies that the parameter will be a quoted value (string).
|
39
|
|
|
const QUOTED_VALUE = 'QuotedValue';
|
40
|
|
|
// Specifies that the parameter will be a bool value (true or false).
|
41
|
|
|
const BOOL_VALUE = 'BoolValue';
|
42
|
|
|
// Specifies that the parameter will be a numeric, non-quoted value.
|
43
|
|
|
const NUMERIC_VALUE = 'NumericValue';
|
44
|
|
|
// Specifies that the parameter will be a non-quoted value
|
45
|
|
|
// (evaluated by the browsers javascript engine at run time).
|
46
|
|
|
const JS_VALUE = 'UnquotedValue';
|
47
|
|
|
// Specifies that the parameter is a call to a javascript function
|
48
|
|
|
const JS_CALL = 'JsCall';
|
49
|
|
|
// Specifies that the parameter must be json encoded
|
50
|
|
|
const JSON_VALUE = 'JsonValue';
|
51
|
|
|
// Specifies that the parameter will be an integer used to generate pagination links.
|
52
|
|
|
const PAGE_NUMBER = 'PageNumber';
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* The parameter type
|
56
|
|
|
*
|
57
|
|
|
* @var string
|
58
|
|
|
*/
|
59
|
|
|
protected $sType;
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* The parameter value
|
63
|
|
|
*
|
64
|
|
|
* @var mixed
|
65
|
|
|
*/
|
66
|
|
|
protected $xValue;
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Convert the parameter value to integer
|
70
|
|
|
*
|
71
|
|
|
* @var bool
|
|
|
|
|
72
|
|
|
*/
|
73
|
|
|
protected $bToInt = false;
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* The constructor.
|
77
|
|
|
*
|
78
|
|
|
* @param string $sType The parameter type
|
|
|
|
|
79
|
|
|
* @param mixed $xValue The parameter value
|
|
|
|
|
80
|
|
|
*/
|
81
|
|
|
public function __construct(string $sType, $xValue)
|
|
|
|
|
82
|
|
|
{
|
83
|
|
|
$this->sType = $sType;
|
84
|
|
|
$this->xValue = $xValue;
|
85
|
|
|
}
|
|
|
|
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* Get the parameter type
|
89
|
|
|
*
|
90
|
|
|
* @return string
|
91
|
|
|
*/
|
92
|
|
|
public function getType(): string
|
93
|
|
|
{
|
94
|
|
|
return $this->sType;
|
95
|
|
|
}
|
|
|
|
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* Get the parameter value
|
99
|
|
|
*
|
100
|
|
|
* @return mixed
|
101
|
|
|
*/
|
102
|
|
|
public function getValue()
|
103
|
|
|
{
|
104
|
|
|
return $this->xValue;
|
105
|
|
|
}
|
|
|
|
|
106
|
|
|
|
107
|
|
|
/**
|
108
|
|
|
* Set the parameter value
|
109
|
|
|
*
|
110
|
|
|
* @param mixed $xValue The parameter value
|
|
|
|
|
111
|
|
|
*
|
112
|
|
|
* @return void
|
113
|
|
|
*/
|
114
|
|
|
public function setValue($xValue)
|
115
|
|
|
{
|
116
|
|
|
$this->xValue = $xValue;
|
117
|
|
|
}
|
|
|
|
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* @return ParameterInterface
|
121
|
|
|
*/
|
122
|
|
|
public function toInt(): ParameterInterface
|
123
|
|
|
{
|
124
|
|
|
$this->bToInt = true;
|
125
|
|
|
return $this;
|
126
|
|
|
}
|
|
|
|
|
127
|
|
|
|
128
|
|
|
/**
|
129
|
|
|
* Create a Parameter instance using the given value
|
130
|
|
|
*
|
131
|
|
|
* @param mixed $xValue The parameter value
|
|
|
|
|
132
|
|
|
*
|
133
|
|
|
* @return ParameterInterface
|
134
|
|
|
*/
|
135
|
|
|
public static function make($xValue): ParameterInterface
|
136
|
|
|
{
|
137
|
|
|
if($xValue instanceof ParameterInterface)
|
138
|
|
|
{
|
139
|
|
|
return $xValue;
|
140
|
|
|
}
|
141
|
|
|
if(is_numeric($xValue))
|
142
|
|
|
{
|
143
|
|
|
return new Parameter(self::NUMERIC_VALUE, $xValue);
|
144
|
|
|
}
|
145
|
|
|
if(is_string($xValue))
|
146
|
|
|
{
|
147
|
|
|
return new Parameter(self::QUOTED_VALUE, $xValue);
|
148
|
|
|
}
|
149
|
|
|
if(is_bool($xValue))
|
150
|
|
|
{
|
151
|
|
|
return new Parameter(self::BOOL_VALUE, $xValue);
|
152
|
|
|
}
|
153
|
|
|
if($xValue instanceof JsCall)
|
154
|
|
|
{
|
155
|
|
|
return new Parameter(self::JS_CALL, $xValue);
|
156
|
|
|
}
|
157
|
|
|
// if(is_array($xValue) || is_object($xValue))
|
158
|
|
|
{
|
159
|
|
|
return new Parameter(self::JSON_VALUE, $xValue);
|
160
|
|
|
}
|
161
|
|
|
}
|
|
|
|
|
162
|
|
|
|
163
|
|
|
/**
|
164
|
|
|
* Add quotes to a given value
|
165
|
|
|
*
|
166
|
|
|
* @param string $sValue The value to be quoted
|
|
|
|
|
167
|
|
|
*
|
168
|
|
|
* @return string
|
169
|
|
|
*/
|
170
|
|
|
private function getQuotedValue(string $sValue): string
|
171
|
|
|
{
|
172
|
|
|
return "'" . $sValue . "'";
|
173
|
|
|
}
|
|
|
|
|
174
|
|
|
|
175
|
|
|
/**
|
176
|
|
|
* Get a js call to Jaxon with a single parameter
|
177
|
|
|
*
|
178
|
|
|
* @param string $sFunction The function name
|
|
|
|
|
179
|
|
|
* @param string $sParameter The function parameter
|
|
|
|
|
180
|
|
|
*
|
181
|
|
|
* @return string
|
182
|
|
|
*/
|
183
|
|
|
private function getJaxonCall(string $sFunction, string $sParameter): string
|
184
|
|
|
{
|
185
|
|
|
return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')';
|
186
|
|
|
}
|
|
|
|
|
187
|
|
|
|
188
|
|
|
/**
|
189
|
|
|
* Get the script for an array of form values.
|
190
|
|
|
*
|
191
|
|
|
* @return string
|
192
|
|
|
*/
|
193
|
|
|
protected function getFormValuesScript(): string
|
194
|
|
|
{
|
195
|
|
|
return $this->getJaxonCall('getFormValues', $this->xValue);
|
196
|
|
|
}
|
|
|
|
|
197
|
|
|
|
198
|
|
|
/**
|
199
|
|
|
* Get the script for an input control.
|
200
|
|
|
*
|
201
|
|
|
* @return string
|
202
|
|
|
*/
|
203
|
|
|
protected function getInputValueScript(): string
|
204
|
|
|
{
|
205
|
|
|
return $this->getJaxonCall('$', $this->xValue) . '.value';
|
206
|
|
|
}
|
|
|
|
|
207
|
|
|
|
208
|
|
|
/**
|
209
|
|
|
* Get the script for a bool value of a checkbox.
|
210
|
|
|
*
|
211
|
|
|
* @return string
|
212
|
|
|
*/
|
213
|
|
|
protected function getCheckedValueScript(): string
|
214
|
|
|
{
|
215
|
|
|
return $this->getJaxonCall('$', $this->xValue) . '.checked';
|
216
|
|
|
}
|
|
|
|
|
217
|
|
|
|
218
|
|
|
/**
|
219
|
|
|
* Get the script for the innerHTML value of the element.
|
220
|
|
|
*
|
221
|
|
|
* @return string
|
222
|
|
|
*/
|
223
|
|
|
protected function getElementInnerHTMLScript(): string
|
|
|
|
|
224
|
|
|
{
|
225
|
|
|
return $this->getJaxonCall('$', $this->xValue) . '.innerHTML';
|
226
|
|
|
}
|
|
|
|
|
227
|
|
|
|
228
|
|
|
/**
|
229
|
|
|
* Get the script for a quoted value (string).
|
230
|
|
|
*
|
231
|
|
|
* @return string
|
232
|
|
|
*/
|
233
|
|
|
protected function getQuotedValueScript(): string
|
234
|
|
|
{
|
235
|
|
|
return $this->getQuotedValue(addslashes($this->xValue));
|
236
|
|
|
}
|
|
|
|
|
237
|
|
|
|
238
|
|
|
/**
|
239
|
|
|
* Get the script for a bool value (true or false).
|
240
|
|
|
*
|
241
|
|
|
* @return string
|
242
|
|
|
*/
|
243
|
|
|
protected function getBoolValueScript(): string
|
244
|
|
|
{
|
245
|
|
|
return ($this->xValue) ? 'true' : 'false';
|
246
|
|
|
}
|
|
|
|
|
247
|
|
|
|
248
|
|
|
/**
|
249
|
|
|
* Get the script for a numeric, non-quoted value.
|
250
|
|
|
*
|
251
|
|
|
* @return string
|
252
|
|
|
*/
|
253
|
|
|
protected function getNumericValueScript(): string
|
254
|
|
|
{
|
255
|
|
|
return (string)$this->xValue;
|
256
|
|
|
}
|
|
|
|
|
257
|
|
|
|
258
|
|
|
/**
|
259
|
|
|
* Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
|
260
|
|
|
*
|
261
|
|
|
* @return string
|
262
|
|
|
*/
|
263
|
|
|
protected function getUnquotedValueScript(): string
|
264
|
|
|
{
|
265
|
|
|
return (string)$this->xValue;
|
266
|
|
|
}
|
|
|
|
|
267
|
|
|
|
268
|
|
|
/**
|
269
|
|
|
* Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
|
270
|
|
|
*
|
271
|
|
|
* @return string
|
272
|
|
|
*/
|
273
|
|
|
protected function getJsonValueScript(): string
|
274
|
|
|
{
|
275
|
|
|
// Unable to use double quotes here because they cannot be handled on client side.
|
276
|
|
|
// So we are using simple quotes even if the Json standard recommends double quotes.
|
277
|
|
|
return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT));
|
278
|
|
|
}
|
|
|
|
|
279
|
|
|
|
280
|
|
|
/**
|
281
|
|
|
* Get the script for an integer used to generate pagination links.
|
282
|
|
|
*
|
283
|
|
|
* @return string
|
284
|
|
|
*/
|
285
|
|
|
protected function getPageNumberScript(): string
|
286
|
|
|
{
|
287
|
|
|
return (string)$this->xValue;
|
288
|
|
|
}
|
|
|
|
|
289
|
|
|
|
290
|
|
|
/**
|
291
|
|
|
* Get the script for a call to a javascript function.
|
292
|
|
|
*
|
293
|
|
|
* @return string
|
294
|
|
|
*/
|
295
|
|
|
protected function getJsCallScript(): string
|
296
|
|
|
{
|
297
|
|
|
return '(e) => {' . $this->xValue->getScript() . ';}';
|
298
|
|
|
}
|
|
|
|
|
299
|
|
|
|
300
|
|
|
/**
|
301
|
|
|
* Generate the javascript code.
|
302
|
|
|
*
|
303
|
|
|
* @return string
|
304
|
|
|
*/
|
305
|
|
|
public function getScript(): string
|
306
|
|
|
{
|
307
|
|
|
$sMethodName = 'get' . $this->sType . 'Script';
|
308
|
|
|
if(!method_exists($this, $sMethodName))
|
309
|
|
|
{
|
310
|
|
|
return '';
|
311
|
|
|
}
|
312
|
|
|
$sScript = $this->$sMethodName();
|
313
|
|
|
return $this->bToInt ? "parseInt($sScript)" : $sScript;
|
314
|
|
|
}
|
|
|
|
|
315
|
|
|
|
316
|
|
|
/**
|
317
|
|
|
* Magic function to generate the jQuery call.
|
318
|
|
|
*
|
319
|
|
|
* @return string
|
320
|
|
|
*/
|
321
|
|
|
public function __toString()
|
322
|
|
|
{
|
323
|
|
|
return $this->getScript();
|
324
|
|
|
}
|
|
|
|
|
325
|
|
|
}
|
326
|
|
|
|