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

JsCall::jsonSerialize()   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
 * 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
     * The constructor.
44
     *
45
     * @param string $sFunction    The javascript function
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
46
     */
47
    public function __construct(string $sFunction)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
48
    {
49
        $this->sFunction = $sFunction;
50
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
51
52
    /**
53
     * Clear the parameter list associated with this request
54
     *
55
     * @return JsCall
56
     */
57
    public function clearParameters(): JsCall
58
    {
59
        $this->aParameters = [];
60
        return $this;
61
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
62
63
    /**
64
     * Set the value of the parameter at the given position
65
     *
66
     * @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...
67
     *
68
     * @return JsCall
69
     */
70
    public function pushParameter(ParameterInterface $xParameter): JsCall
71
    {
72
        $this->aParameters[] = $xParameter;
73
        return $this;
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * Add a parameter value to the parameter list for this request
78
     *
79
     * @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...
80
     * @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...
81
     *
82
     * 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...
83
     * <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...
84
     * 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...
85
     * - <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...
86
     * - <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...
87
     * - <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...
88
     *   (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...
89
     *   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...
90
     *
91
     * @return JsCall
92
     */
93
    public function addParameter(string $sType, string $sValue): JsCall
94
    {
95
        $this->pushParameter(new Parameter($sType, $sValue));
96
        return $this;
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Add a set of parameters to this request
101
     *
102
     * @param array $aParameters    The parameters
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
103
     *
104
     * @return JsCall
105
     */
106
    public function addParameters(array $aParameters): JsCall
107
    {
108
        foreach($aParameters as $xParameter)
109
        {
110
            if($xParameter instanceof JsCall)
111
            {
112
                $this->addParameter(Parameter::JS_VALUE, 'function(){' . $xParameter->getScript() . ';}');
113
            }
114
            else
115
            {
116
                $this->pushParameter(Parameter::make($xParameter));
117
            }
118
        }
119
        return $this;
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Returns a string representation of the script output (javascript) from this request object
124
     *
125
     * @return string
126
     */
127
    public function getScript(): string
128
    {
129
        return $this->sFunction . '(' . implode(', ', $this->aParameters) . ')';
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * Convert this request object to string
134
     *
135
     * @return string
136
     */
137
    public function __toString()
138
    {
139
        return $this->getScript();
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Convert this request object to string, when converting the response into json.
144
     *
145
     * This is a method of the JsonSerializable interface.
146
     *
147
     * @return string
148
     */
149
    public function jsonSerialize(): string
150
    {
151
        return $this->getScript();
152
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
153
}
154