Passed
Push — master ( 5212d6...22a324 )
by Thierry
03:24
created

Parameter::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Parameter
Loading history...
26
{
27
    /*
28
     * Request parameters
29
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
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 must be json encoded
48
    const JSON_VALUE = 'JsonValue';
49
    // Specifies that the parameter will be an integer used to generate pagination links.
50
    const PAGE_NUMBER = 'PageNumber';
51
52
    /**
53
     * The parameter type
54
     *
55
     * @var string
56
     */
57
    protected $sType;
58
59
    /**
60
     * The parameter value
61
     *
62
     * @var mixed
63
     */
64
    protected $xValue;
65
66
    /**
67
     * Convert the parameter value to integer
68
     *
69
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
70
     */
71
    protected $bToInt = false;
72
73
    /**
74
     * The constructor.
75
     *
76
     * @param string $sType    The parameter type
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
77
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
78
     */
79
    public function __construct(string $sType, $xValue)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
80
    {
81
        $this->sType = $sType;
82
        $this->xValue = $xValue;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Get the parameter type
87
     *
88
     * @return string
89
     */
90
    public function getType(): string
91
    {
92
        return $this->sType;
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * Get the parameter value
97
     *
98
     * @return mixed
99
     */
100
    public function getValue()
101
    {
102
        return $this->xValue;
103
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
104
105
    /**
106
     * Set the parameter value
107
     *
108
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
109
     *
110
     * @return void
111
     */
112
    public function setValue($xValue)
113
    {
114
        $this->xValue = $xValue;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * @return ParameterInterface
119
     */
120
    public function toInt(): ParameterInterface
121
    {
122
        $this->bToInt = true;
123
        return $this;
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Create a Parameter instance using the given value
128
     *
129
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
130
     *
131
     * @return ParameterInterface
132
     */
133
    public static function make($xValue): ParameterInterface
134
    {
135
        if($xValue instanceof ParameterInterface)
136
        {
137
            return $xValue;
138
        }
139
        if(is_numeric($xValue))
140
        {
141
            return new Parameter(self::NUMERIC_VALUE, $xValue);
142
        }
143
        if(is_string($xValue))
144
        {
145
            return new Parameter(self::QUOTED_VALUE, $xValue);
146
        }
147
        if(is_bool($xValue))
148
        {
149
            return new Parameter(self::BOOL_VALUE, $xValue);
150
        }
151
        // if(is_array($xValue) || is_object($xValue))
152
        {
153
            return new Parameter(self::JSON_VALUE, $xValue);
154
        }
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Add quotes to a given value
159
     *
160
     * @param string $sValue    The value to be quoted
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
161
     *
162
     * @return string
163
     */
164
    private function getQuotedValue(string $sValue): string
165
    {
166
        return "'" . $sValue . "'";
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Get a js call to Jaxon with a single parameter
171
     *
172
     * @param string $sFunction    The function name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
173
     * @param string $sParameter    The function parameter
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
174
     *
175
     * @return string
176
     */
177
    private function getJsCall(string $sFunction, string $sParameter): string
178
    {
179
        return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')';
180
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
181
182
    /**
183
     * Get the script for an array of form values.
184
     *
185
     * @return string
186
     */
187
    protected function getFormValuesScript(): string
188
    {
189
        return $this->getJsCall('getFormValues', $this->xValue);
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * Get the script for an input control.
194
     *
195
     * @return string
196
     */
197
    protected function getInputValueScript(): string
198
    {
199
        return $this->getJsCall('$', $this->xValue) . '.value';
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Get the script for a bool value of a checkbox.
204
     *
205
     * @return string
206
     */
207
    protected function getCheckedValueScript(): string
208
    {
209
        return $this->getJsCall('$', $this->xValue) . '.checked';
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Get the script for the innerHTML value of the element.
214
     *
215
     * @return string
216
     */
217
    protected function getElementInnerHTMLScript(): string
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
218
    {
219
        return $this->getJsCall('$', $this->xValue) . '.innerHTML';
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
221
222
    /**
223
     * Get the script for a quoted value (string).
224
     *
225
     * @return string
226
     */
227
    protected function getQuotedValueScript(): string
228
    {
229
        return $this->getQuotedValue(addslashes($this->xValue));
230
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
231
232
    /**
233
     * Get the script for a bool value (true or false).
234
     *
235
     * @return string
236
     */
237
    protected function getBoolValueScript(): string
238
    {
239
        return ($this->xValue) ? 'true' : 'false';
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Get the script for a numeric, non-quoted value.
244
     *
245
     * @return string
246
     */
247
    protected function getNumericValueScript(): string
248
    {
249
        return (string)$this->xValue;
250
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
251
252
    /**
253
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
254
     *
255
     * @return string
256
     */
257
    protected function getUnquotedValueScript(): string
258
    {
259
        return (string)$this->xValue;
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
264
     *
265
     * @return string
266
     */
267
    protected function getJsonValueScript(): string
268
    {
269
        // Unable to use double quotes here because they cannot be handled on client side.
270
        // So we are using simple quotes even if the Json standard recommends double quotes.
271
        return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT));
272
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
273
274
    /**
275
     * Get the script for an integer used to generate pagination links.
276
     *
277
     * @return string
278
     */
279
    protected function getPageNumberScript(): string
280
    {
281
        return (string)$this->xValue;
282
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
283
284
    /**
285
     * Generate the javascript code.
286
     *
287
     * @return string
288
     */
289
    public function getScript(): string
290
    {
291
        $sMethodName = 'get' . $this->sType . 'Script';
292
        if(!method_exists($this, $sMethodName))
293
        {
294
            return '';
295
        }
296
        $sScript = $this->$sMethodName();
297
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
298
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
299
300
    /**
301
     * Magic function to generate the jQuery call.
302
     *
303
     * @return string
304
     */
305
    public function __toString()
306
    {
307
        return $this->getScript();
308
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
309
}
310