|
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
|
|
|
|
|
22
|
|
|
class Parameter implements ParameterInterface |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
/* |
|
25
|
|
|
* Request parameters |
|
26
|
|
|
*/ |
|
|
|
|
|
|
27
|
|
|
// Specifies that the parameter will consist of an array of form values. |
|
28
|
|
|
const FORM_VALUES = 'FormValues'; |
|
29
|
|
|
// Specifies that the parameter will contain the value of an input control. |
|
30
|
|
|
const INPUT_VALUE = 'InputValue'; |
|
31
|
|
|
// Specifies that the parameter will consist of a bool value of a checkbox. |
|
32
|
|
|
const CHECKED_VALUE = 'CheckedValue'; |
|
33
|
|
|
// Specifies that the parameter value will be the innerHTML value of the element. |
|
34
|
|
|
const ELEMENT_INNERHTML = 'ElementInnerHTML'; |
|
35
|
|
|
// Specifies that the parameter will be a quoted value (string). |
|
36
|
|
|
const QUOTED_VALUE = 'QuotedValue'; |
|
37
|
|
|
// Specifies that the parameter will be a bool value (true or false). |
|
38
|
|
|
const BOOL_VALUE = 'BoolValue'; |
|
39
|
|
|
// Specifies that the parameter will be a numeric, non-quoted value. |
|
40
|
|
|
const NUMERIC_VALUE = 'NumericValue'; |
|
41
|
|
|
// Specifies that the parameter will be a non-quoted value |
|
42
|
|
|
// (evaluated by the browsers javascript engine at run time). |
|
43
|
|
|
const JS_VALUE = 'UnquotedValue'; |
|
44
|
|
|
// Specifies that the parameter is a call to a javascript function |
|
45
|
|
|
const JS_CALL = 'JsCall'; |
|
46
|
|
|
// Specifies that the parameter must be json encoded |
|
47
|
|
|
const JSON_VALUE = 'JsonValue'; |
|
48
|
|
|
// Specifies that the parameter will be an integer used to generate pagination links. |
|
49
|
|
|
const PAGE_NUMBER = 'PageNumber'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The parameter type |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $sType; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The parameter value |
|
60
|
|
|
* |
|
61
|
|
|
* @var mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $xValue; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Convert the parameter value to integer |
|
67
|
|
|
* |
|
68
|
|
|
* @var bool |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
|
|
protected $bToInt = false; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The constructor. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $sType The parameter type |
|
|
|
|
|
|
76
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct(string $sType, $xValue) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$this->sType = $sType; |
|
81
|
|
|
$this->xValue = $xValue; |
|
82
|
|
|
} |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the parameter type |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getType(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->sType; |
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the parameter value |
|
96
|
|
|
* |
|
97
|
|
|
* @return mixed |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getValue() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->xValue; |
|
102
|
|
|
} |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the parameter value |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setValue($xValue) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->xValue = $xValue; |
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return ParameterInterface |
|
118
|
|
|
*/ |
|
119
|
|
|
public function toInt(): ParameterInterface |
|
120
|
|
|
{ |
|
121
|
|
|
$this->bToInt = true; |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create a Parameter instance using the given value |
|
127
|
|
|
* |
|
128
|
|
|
* @param mixed $xValue The parameter value |
|
|
|
|
|
|
129
|
|
|
* |
|
130
|
|
|
* @return ParameterInterface |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function make($xValue): ParameterInterface |
|
133
|
|
|
{ |
|
134
|
|
|
if($xValue instanceof ParameterInterface) |
|
135
|
|
|
{ |
|
136
|
|
|
return $xValue; |
|
137
|
|
|
} |
|
138
|
|
|
if(is_numeric($xValue)) |
|
139
|
|
|
{ |
|
140
|
|
|
return new Parameter(self::NUMERIC_VALUE, $xValue); |
|
141
|
|
|
} |
|
142
|
|
|
if(is_string($xValue)) |
|
143
|
|
|
{ |
|
144
|
|
|
return new Parameter(self::QUOTED_VALUE, $xValue); |
|
145
|
|
|
} |
|
146
|
|
|
if(is_bool($xValue)) |
|
147
|
|
|
{ |
|
148
|
|
|
return new Parameter(self::BOOL_VALUE, $xValue); |
|
149
|
|
|
} |
|
150
|
|
|
if($xValue instanceof JsCall) |
|
151
|
|
|
{ |
|
152
|
|
|
return new Parameter(self::JS_CALL, $xValue); |
|
153
|
|
|
} |
|
154
|
|
|
// if(is_array($xValue) || is_object($xValue)) |
|
155
|
|
|
{ |
|
156
|
|
|
return new Parameter(self::JSON_VALUE, $xValue); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Convert to a value to be inserted in an array for json conversion |
|
162
|
|
|
* |
|
163
|
|
|
* @return mixed |
|
164
|
|
|
*/ |
|
165
|
|
|
public function forArray() |
|
166
|
|
|
{ |
|
167
|
|
|
switch($this->getType()) |
|
168
|
|
|
{ |
|
169
|
|
|
case self::JS_CALL: |
|
170
|
|
|
return $this->getValue()->toArray(); |
|
171
|
|
|
case self::JS_VALUE: |
|
172
|
|
|
return [ |
|
173
|
|
|
'_type' => 'expr', |
|
174
|
|
|
'calls' => [['_type' => 'attr', '_name' => $this->getValue()]], |
|
175
|
|
|
]; |
|
176
|
|
|
case self::FORM_VALUES: |
|
177
|
|
|
return ['_type' => 'form', '_name' => $this->getValue()]; |
|
178
|
|
|
case self::INPUT_VALUE: |
|
179
|
|
|
return ['_type' => 'input', '_name' => $this->getValue()]; |
|
180
|
|
|
case self::CHECKED_VALUE: |
|
181
|
|
|
return ['_type' => 'checked', '_name' => $this->getValue()]; |
|
182
|
|
|
case self::ELEMENT_INNERHTML: |
|
183
|
|
|
return ['_type' => 'html', '_name' => $this->getValue()]; |
|
184
|
|
|
case self::QUOTED_VALUE: |
|
185
|
|
|
case self::BOOL_VALUE: |
|
186
|
|
|
case self::NUMERIC_VALUE: |
|
187
|
|
|
case self::JSON_VALUE: |
|
188
|
|
|
case self::PAGE_NUMBER: |
|
189
|
|
|
default: |
|
190
|
|
|
// Return the value as is. |
|
191
|
|
|
return $this->getValue(); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|