Completed
Push — master ( 34bc1a...41d5a5 )
by Thierry
02:48 queued 02:48
created

Parameter   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 266
rs 10
wmc 25

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputValueScript() 0 3 1
A getQuotedValue() 0 3 1
A getElementInnerHTMLScript() 0 3 1
A __construct() 0 4 1
A setValue() 0 3 1
A getValue() 0 3 1
A getUnquotedValueScript() 0 3 1
A getNumericValueScript() 0 3 1
A getCheckedValueScript() 0 3 1
A getType() 0 3 1
A getBoolValueScript() 0 3 2
A getQuotedValueScript() 0 3 1
A getPageNumberScript() 0 3 1
A getJsonValueScript() 0 5 1
A getJsCall() 0 3 1
A getScript() 0 8 2
A getFormValuesScript() 0 3 1
A make() 0 21 5
A __toString() 0 3 1
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
     * The constructor.
68
     *
69
     * @param string $sType    The parameter type
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
70
     * @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...
71
     */
72
    public function __construct(string $sType, $xValue)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
73
    {
74
        $this->sType = $sType;
75
        $this->xValue = $xValue;
76
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
77
78
    /**
79
     * Get the parameter type
80
     *
81
     * @return string
82
     */
83
    public function getType(): string
84
    {
85
        return $this->sType;
86
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
87
88
    /**
89
     * Get the parameter value
90
     *
91
     * @return mixed
92
     */
93
    public function getValue()
94
    {
95
        return $this->xValue;
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Set the parameter value
100
     *
101
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
102
     *
103
     * @return void
104
     */
105
    public function setValue($xValue)
106
    {
107
        $this->xValue = $xValue;
108
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
109
110
    /**
111
     * Create a Parameter instance using the given value
112
     *
113
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
114
     *
115
     * @return ParameterInterface
116
     */
117
    public static function make($xValue): ParameterInterface
118
    {
119
        if($xValue instanceof ParameterInterface)
120
        {
121
            return $xValue;
122
        }
123
        if(is_numeric($xValue))
124
        {
125
            return new Parameter(self::NUMERIC_VALUE, $xValue);
126
        }
127
        if(is_string($xValue))
128
        {
129
            return new Parameter(self::QUOTED_VALUE, $xValue);
130
        }
131
        if(is_bool($xValue))
132
        {
133
            return new Parameter(self::BOOL_VALUE, $xValue);
134
        }
135
        // if(is_array($xValue) || is_object($xValue))
136
        {
137
            return new Parameter(self::JSON_VALUE, $xValue);
138
        }
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * Add quotes to a given value
143
     *
144
     * @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...
145
     *
146
     * @return string
147
     */
148
    private function getQuotedValue(string $sValue): string
149
    {
150
        return "'" . $sValue . "'";
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Get a js call to Jaxon with a single parameter
155
     *
156
     * @param string $sFunction    The function name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
157
     * @param string $sParameter    The function parameter
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
158
     *
159
     * @return string
160
     */
161
    private function getJsCall(string $sFunction, string $sParameter): string
162
    {
163
        return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')';
164
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
165
166
    /**
167
     * Get the script for an array of form values.
168
     *
169
     * @return string
170
     */
171
    protected function getFormValuesScript(): string
172
    {
173
        return $this->getJsCall('getFormValues', $this->xValue);
174
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
175
176
    /**
177
     * Get the script for an input control.
178
     *
179
     * @return string
180
     */
181
    protected function getInputValueScript(): string
182
    {
183
        return $this->getJsCall('$', $this->xValue) . '.value';
184
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
185
186
    /**
187
     * Get the script for a bool value of a checkbox.
188
     *
189
     * @return string
190
     */
191
    protected function getCheckedValueScript(): string
192
    {
193
        return $this->getJsCall('$', $this->xValue) . '.checked';
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
195
196
    /**
197
     * Get the script for the innerHTML value of the element.
198
     *
199
     * @return string
200
     */
201
    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...
202
    {
203
        return $this->getJsCall('$', $this->xValue) . '.innerHTML';
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * Get the script for a quoted value (string).
208
     *
209
     * @return string
210
     */
211
    protected function getQuotedValueScript(): string
212
    {
213
        return $this->getQuotedValue(addslashes($this->xValue));
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * Get the script for a bool value (true or false).
218
     *
219
     * @return string
220
     */
221
    protected function getBoolValueScript(): string
222
    {
223
        return ($this->xValue) ? 'true' : 'false';
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Get the script for a numeric, non-quoted value.
228
     *
229
     * @return string
230
     */
231
    protected function getNumericValueScript(): string
232
    {
233
        return (string)$this->xValue;
234
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
235
236
    /**
237
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
238
     *
239
     * @return string
240
     */
241
    protected function getUnquotedValueScript(): string
242
    {
243
        return (string)$this->xValue;
244
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
245
246
    /**
247
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
248
     *
249
     * @return string
250
     */
251
    protected function getJsonValueScript(): string
252
    {
253
        // Unable to use double quotes here because they cannot be handled on client side.
254
        // So we are using simple quotes even if the Json standard recommends double quotes.
255
        return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT));
256
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
257
258
    /**
259
     * Get the script for an integer used to generate pagination links.
260
     *
261
     * @return string
262
     */
263
    protected function getPageNumberScript(): string
264
    {
265
        return (string)$this->xValue;
266
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
267
268
    /**
269
     * Generate the javascript code.
270
     *
271
     * @return string
272
     */
273
    public function getScript(): string
274
    {
275
        $sMethodName = 'get' . $this->sType . 'Script';
276
        if(!method_exists($this, $sMethodName))
277
        {
278
            return '';
279
        }
280
        return $this->$sMethodName();
281
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
282
283
    /**
284
     * Magic function to generate the jQuery call.
285
     *
286
     * @return string
287
     */
288
    public function __toString()
289
    {
290
        return $this->getScript();
291
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
292
}
293