|
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
|
|
|
use Jaxon\Jaxon; |
|
17
|
|
|
|
|
18
|
|
|
class Parameter implements Interfaces\Parameter |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The parameter type |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $sType; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The parameter value |
|
29
|
|
|
* |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $xValue; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $sType The parameter type |
|
38
|
|
|
* @param mixed $xValue The parameter value |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($sType, $xValue) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->sType = $sType; |
|
43
|
|
|
$this->xValue = $xValue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the parameter type |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getType() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->sType; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the parameter value |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getValue() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->xValue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set the parameter value |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $xValue The parameter value |
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setValue($xValue) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->xValue = $xValue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create a Parameter instance using the given value |
|
80
|
|
|
* |
|
81
|
|
|
* @param mixed $xValue The parameter value |
|
82
|
|
|
* |
|
83
|
|
|
* @return Parameter |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function make($xValue) |
|
86
|
|
|
{ |
|
87
|
|
|
if($xValue instanceof Interfaces\Parameter) |
|
88
|
|
|
{ |
|
89
|
|
|
return $xValue; |
|
90
|
|
|
} |
|
91
|
|
|
elseif(is_numeric($xValue)) |
|
92
|
|
|
{ |
|
93
|
|
|
return new Parameter(Jaxon::NUMERIC_VALUE, $xValue); |
|
94
|
|
|
} |
|
95
|
|
|
elseif(is_string($xValue)) |
|
96
|
|
|
{ |
|
97
|
|
|
return new Parameter(Jaxon::QUOTED_VALUE, $xValue); |
|
98
|
|
|
} |
|
99
|
|
|
elseif(is_bool($xValue)) |
|
100
|
|
|
{ |
|
101
|
|
|
return new Parameter(Jaxon::BOOL_VALUE, $xValue); |
|
102
|
|
|
} |
|
103
|
|
|
else // if(is_array($xValue) || is_object($xValue)) |
|
104
|
|
|
{ |
|
105
|
|
|
return new Parameter(Jaxon::JS_VALUE, $xValue); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add quotes to a given value |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $xValue The value to be quoted |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
private function getQuotedValue($xValue) |
|
117
|
|
|
{ |
|
118
|
|
|
$sQuoteCharacter = "'"; |
|
119
|
|
|
return $sQuoteCharacter . $xValue . $sQuoteCharacter; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get a js call to Jaxon with a single parameter |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $sFunction The function name |
|
126
|
|
|
* @param string $sParameter The function parameter |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
private function getJsCall($sFunction, $sParameter) |
|
131
|
|
|
{ |
|
132
|
|
|
return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Generate the javascript code. |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getScript() |
|
141
|
|
|
{ |
|
142
|
|
|
$sJsCode = ''; |
|
143
|
|
|
switch($this->sType) |
|
144
|
|
|
{ |
|
145
|
|
|
case Jaxon::FORM_VALUES: |
|
146
|
|
|
$sJsCode = $this->getJsCall('getFormValues', $this->xValue); |
|
147
|
|
|
break; |
|
148
|
|
|
case Jaxon::INPUT_VALUE: |
|
149
|
|
|
$sJsCode = $this->getJsCall('$', $this->xValue) . '.value'; |
|
150
|
|
|
break; |
|
151
|
|
|
case Jaxon::CHECKED_VALUE: |
|
152
|
|
|
$sJsCode = $this->getJsCall('$', $this->xValue) . '.checked'; |
|
153
|
|
|
break; |
|
154
|
|
|
case Jaxon::ELEMENT_INNERHTML: |
|
155
|
|
|
$sJsCode = $this->getJsCall('$', $this->xValue) . '.innerHTML'; |
|
156
|
|
|
break; |
|
157
|
|
|
case Jaxon::QUOTED_VALUE: |
|
158
|
|
|
$sJsCode = $this->getQuotedValue(addslashes($this->xValue)); |
|
159
|
|
|
break; |
|
160
|
|
|
case Jaxon::BOOL_VALUE: |
|
161
|
|
|
$sJsCode = ($this->xValue) ? 'true' : 'false'; |
|
162
|
|
|
break; |
|
163
|
|
|
case Jaxon::PAGE_NUMBER: |
|
164
|
|
|
$sJsCode = (string)$this->xValue; |
|
165
|
|
|
break; |
|
166
|
|
|
case Jaxon::NUMERIC_VALUE: |
|
167
|
|
|
$sJsCode = (string)$this->xValue; |
|
168
|
|
|
break; |
|
169
|
|
|
case Jaxon::JS_VALUE: |
|
170
|
|
|
if(is_array($this->xValue) || is_object($this->xValue)) |
|
171
|
|
|
{ |
|
172
|
|
|
// Unable to use double quotes here because they cannot be handled on client side. |
|
173
|
|
|
// So we are using simple quotes even if the Json standard recommends double quotes. |
|
174
|
|
|
$sJsCode = str_replace(['"'], ["'"], json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT)); |
|
175
|
|
|
} |
|
176
|
|
|
else |
|
177
|
|
|
{ |
|
178
|
|
|
$sJsCode = (string)$this->xValue; |
|
179
|
|
|
} |
|
180
|
|
|
break; |
|
181
|
|
|
} |
|
182
|
|
|
return $sJsCode; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Magic function to generate the jQuery call. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function __toString() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->getScript(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Generate the jQuery call, when converting the response into json. |
|
197
|
|
|
* |
|
198
|
|
|
* This is a method of the JsonSerializable interface. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function jsonSerialize() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->getScript(); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|