Parameter::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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