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

JsCall   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 24
dl 0
loc 146
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getScript() 0 4 2
A addParameter() 0 4 1
A addParameters() 0 14 3
A __construct() 0 3 1
A jsonSerialize() 0 3 1
A clearParameters() 0 4 1
A toInt() 0 4 1
A __toString() 0 3 1
A pushParameter() 0 4 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
class JsCall implements JsonSerializable
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JsCall
Loading history...
24
{
25
    /**
26
     * The name of the javascript function
27
     *
28
     * @var string
29
     */
30
    private $sFunction;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
31
32
    /**
33
     * @var string
34
     */
35
    private $sQuoteCharacter = "'";
0 ignored issues
show
introduced by
The private property $sQuoteCharacter is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @var array
39
     */
40
    protected $aParameters = [];
41
42
    /**
43
     * Convert the parameter value to integer
44
     *
45
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
46
     */
47
    protected $bToInt = false;
48
49
    /**
50
     * The constructor.
51
     *
52
     * @param string $sFunction    The javascript function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
53
     */
54
    public function __construct(string $sFunction)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
55
    {
56
        $this->sFunction = $sFunction;
57
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
58
59
    /**
60
     * @return JsCall
61
     */
62
    public function toInt(): JsCall
63
    {
64
        $this->bToInt = true;
65
        return $this;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * Clear the parameter list associated with this request
70
     *
71
     * @return JsCall
72
     */
73
    public function clearParameters(): JsCall
74
    {
75
        $this->aParameters = [];
76
        return $this;
77
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
78
79
    /**
80
     * Set the value of the parameter at the given position
81
     *
82
     * @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...
83
     *
84
     * @return JsCall
85
     */
86
    public function pushParameter(ParameterInterface $xParameter): JsCall
87
    {
88
        $this->aParameters[] = $xParameter;
89
        return $this;
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Add a parameter value to the parameter list for this request
94
     *
95
     * @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...
96
     * @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...
97
     *
98
     * 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...
99
     * <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...
100
     * 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...
101
     * - <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...
102
     * - <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...
103
     * - <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...
104
     *   (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...
105
     *   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...
106
     *
107
     * @return JsCall
108
     */
109
    public function addParameter(string $sType, string $sValue): JsCall
110
    {
111
        $this->pushParameter(new Parameter($sType, $sValue));
112
        return $this;
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
114
115
    /**
116
     * Add a set of parameters to this request
117
     *
118
     * @param array $aParameters    The parameters
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
119
     *
120
     * @return JsCall
121
     */
122
    public function addParameters(array $aParameters): JsCall
123
    {
124
        foreach($aParameters as $xParameter)
125
        {
126
            if($xParameter instanceof JsCall)
127
            {
128
                $this->addParameter(Parameter::JS_VALUE, 'function(){' . $xParameter->getScript() . ';}');
129
            }
130
            else
131
            {
132
                $this->pushParameter(Parameter::make($xParameter));
133
            }
134
        }
135
        return $this;
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Returns a string representation of the script output (javascript) from this request object
140
     *
141
     * @return string
142
     */
143
    public function getScript(): string
144
    {
145
        $sScript = $this->sFunction . '(' . implode(', ', $this->aParameters) . ')';
146
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * Convert this request object to string
151
     *
152
     * @return string
153
     */
154
    public function __toString()
155
    {
156
        return $this->getScript();
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Convert this request object to string, when converting the response into json.
161
     *
162
     * This is a method of the JsonSerializable interface.
163
     *
164
     * @return string
165
     */
166
    public function jsonSerialize(): string
167
    {
168
        return $this->getScript();
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
170
}
171