Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
created

Parameter::jsonSerialize()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 28
rs 6.9666
cc 12
nc 12
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
22
class Parameter implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Parameter
Loading history...
23
{
24
    /*
25
     * Request parameters
26
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
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
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
69
     */
70
    protected $bToInt = false;
71
72
    /**
73
     * The constructor.
74
     *
75
     * @param string $sType    The parameter type
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
76
     * @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...
77
     */
78
    public function __construct(string $sType, $xValue)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
79
    {
80
        $this->sType = $sType;
81
        $this->xValue = $xValue;
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Get the parameter type
86
     *
87
     * @return string
88
     */
89
    public function getType(): string
90
    {
91
        return $this->sType;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Get the parameter value
96
     *
97
     * @return mixed
98
     */
99
    public function getValue()
100
    {
101
        return $this->xValue;
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Set the parameter value
106
     *
107
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
108
     *
109
     * @return void
110
     */
111
    public function setValue($xValue)
112
    {
113
        $this->xValue = $xValue;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * @return ParameterInterface
118
     */
119
    public function toInt(): ParameterInterface
120
    {
121
        $this->bToInt = true;
122
        return $this;
123
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
124
125
    /**
126
     * Create a Parameter instance using the given value
127
     *
128
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
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
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
159
160
    /**
161
     * Convert to a value to be inserted in an array for json conversion
162
     *
163
     * @return mixed
164
     */
165
    public function jsonSerialize()
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::PAGE_NUMBER:
185
            return ['_type' => 'page', '_name' => ''];
186
        case self::QUOTED_VALUE:
187
        case self::BOOL_VALUE:
188
        case self::NUMERIC_VALUE:
189
        case self::JSON_VALUE:
190
        default:
191
            // Return the value as is.
192
            return $this->getValue();
193
        }
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
195
}
196