Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

JsCall::addParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * JsCall.php - A javascript function call, with its parameters.
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
8
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
9
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Thierry Feuzeu
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
13
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
14
 * @copyright 2016 Thierry Feuzeu <[email protected]>
15
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
16
 * @link https://github.com/jaxon-php/jaxon-core
17
 */
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...
18
19
namespace Jaxon\Request\Call;
20
21
use JsonSerializable;
22
23
use function array_map;
24
25
class JsCall implements JsonSerializable
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JsCall
Loading history...
26
{
27
    /**
28
     * The name of the javascript function
29
     *
30
     * @var string
31
     */
32
    private $sFunction;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
33
34
    /**
35
     * @var string
36
     */
37
    private $sQuoteCharacter = "'";
0 ignored issues
show
introduced by
The private property $sQuoteCharacter is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @var array<ParameterInterface>
41
     */
42
    protected $aParameters = [];
43
44
    /**
45
     * Convert the parameter value to integer
46
     *
47
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
48
     */
49
    protected $bToInt = false;
50
51
    /**
52
     * The constructor.
53
     *
54
     * @param string $sFunction    The javascript function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
55
     */
56
    public function __construct(string $sFunction)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
57
    {
58
        $this->sFunction = $sFunction;
59
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
60
61
    /**
62
     * @return JsCall
63
     */
64
    public function toInt(): JsCall
65
    {
66
        $this->bToInt = true;
67
        return $this;
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
69
70
    /**
71
     * Clear the parameter list associated with this request
72
     *
73
     * @return JsCall
74
     */
75
    public function clearParameters(): JsCall
76
    {
77
        $this->aParameters = [];
78
        return $this;
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * Set the value of the parameter at the given position
83
     *
84
     * @param ParameterInterface $xParameter    The value to be used
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
85
     *
86
     * @return JsCall
87
     */
88
    public function pushParameter(ParameterInterface $xParameter): JsCall
89
    {
90
        $this->aParameters[] = $xParameter;
91
        return $this;
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Add a parameter value to the parameter list for this request
96
     *
97
     * @param string $sType    The type of the value to be used
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
98
     * @param string $sValue    The value to be used
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
99
     *
100
     * Types should be one of the following <Parameter::FORM_VALUES>, <Parameter::QUOTED_VALUE>, <Parameter::NUMERIC_VALUE>,
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
101
     * <Parameter::JS_VALUE>, <Parameter::INPUT_VALUE>, <Parameter::CHECKED_VALUE>, <Parameter::PAGE_NUMBER>.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
102
     * The value should be as follows:
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
103
     * - <Parameter::FORM_VALUES> - Use the ID of the form you want to process.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
104
     * - <Parameter::QUOTED_VALUE> - The string data to be passed.
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
105
     * - <Parameter::JS_VALUE> - A string containing valid javascript
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 1
Loading history...
106
     *   (either a javascript variable name that will be in scope at the time of the call or
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 3
Loading history...
107
     *   a javascript function call whose return value will become the parameter).
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 26 spaces but found 3
Loading history...
108
     *
109
     * @return JsCall
110
     */
111
    public function addParameter(string $sType, string $sValue): JsCall
112
    {
113
        $this->pushParameter(new Parameter($sType, $sValue));
114
        return $this;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Add a set of parameters to this request
119
     *
120
     * @param array $aParameters    The parameters
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
121
     *
122
     * @return JsCall
123
     */
124
    public function addParameters(array $aParameters): JsCall
125
    {
126
        foreach($aParameters as $xParameter)
127
        {
128
            $this->pushParameter(Parameter::make($xParameter));
129
        }
130
        return $this;
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Convert this call to array
135
     *
136
     * @return array
137
     */
138
    public function toArray(): array
139
    {
140
        $aCalls = [[
141
            '_type' => 'func',
142
            '_name' => $this->sFunction,
143
            'params' => array_map(function(Parameter $xParam) {
144
                return $xParam->forArray();
145
            }, $this->aParameters),
146
        ]];
0 ignored issues
show
Coding Style introduced by
Closing brace of array declaration must be on a new line
Loading history...
147
        if($this->bToInt)
148
        {
149
            $aCalls[] = [
150
                '_type' => 'func',
151
                '_name' => 'jaxon.utils.string.toInt',
152
                'params' => [[ '_type' => '_', '_name' => 'this' ]],
153
            ];
154
        }
155
        return [
156
            '_type' => 'expr',
157
            'calls' => $aCalls,
158
        ];
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * Convert this call to string, when converting the response into json.
163
     *
164
     * This is a method of the JsonSerializable interface.
165
     *
166
     * @return array
167
     */
168
    public function jsonSerialize(): array
169
    {
170
        return $this->toArray();
171
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
172
}
173