Passed
Push — v5.x ( 59ec3d...dec8cc )
by Thierry
02:06
created

DomSelector::on()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * DomSelector.php - A jQuery selector
5
 *
6
 * This class is used to create client side requests to the Jaxon functions and callable objects.
7
 *
8
 * When inserted into a Jaxon response, a DomSelector object must be converted to the corresponding jQuery code.
9
 * Therefore, the DomSelector class implements the JsonSerializable interface.
10
 *
11
 * When used as a parameter of a Jaxon call, the DomSelector must be converted to Jaxon request parameter.
12
 * Therefore, the DomSelector class also implements the Jaxon\Request\Call\ParameterInterface interface.
13
 *
14
 * @package jaxon-jquery
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-jquery" is not valid; consider "Jaxonjquery" instead
Loading history...
15
 * @author Thierry Feuzeu <[email protected]>
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-jquery
19
 */
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...
20
21
namespace Jaxon\Plugin\Response\JQuery;
22
23
use Jaxon\Plugin\Response\JQuery\Call\AttrGet;
24
use Jaxon\Plugin\Response\JQuery\Call\AttrSet;
25
use Jaxon\Plugin\Response\JQuery\Call\Event;
26
use Jaxon\Plugin\Response\JQuery\Call\Method;
27
use Jaxon\Request\Call\Call;
28
use Jaxon\Request\Call\ParameterInterface;
29
30
use function trim;
31
32
class DomSelector implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DomSelector
Loading history...
33
{
34
    /**
35
     * The jQuery selector path
36
     *
37
     * @var string
38
     */
39
    protected $sPath;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
40
41
    /**
42
     * The jQuery selector context
43
     *
44
     * @var mixed
45
     */
46
    protected $xContext;
47
48
    /**
49
     * The actions to be applied on the selected element
50
     *
51
     * @var array
52
     */
53
    protected $aCalls = [];
54
55
    /**
56
     * Convert the selector value to integer
57
     *
58
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
59
     */
60
    protected $bToInt = false;
61
62
    /**
63
     * The constructor.
64
     *
65
     * @param string $jQueryNs    The jQuery symbol
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $jQueryNs does not match actual variable name $sPath
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
66
     * @param string $sPath    The jQuery selector path
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $sPath does not match actual variable name $xContext
Loading history...
67
     * @param mixed $xContext    A context associated to the selector
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Superfluous parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
68
     */
69
    public function __construct(string $sPath, $xContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
70
    {
71
        $this->sPath = trim($sPath, " \t");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
        $this->xContext = $xContext;
73
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
74
75
    /**
76
     * @inheritDoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getType(): string
79
    {
80
        return 'selector';
81
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
82
83
    /**
84
     * Add a call to a jQuery method on the selected elements
85
     *
86
     * @param string  $sMethod
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
87
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
88
     *
89
     * @return DomSelector
90
     */
91
    public function __call(string $sMethod, array $aArguments)
92
    {
93
        // Append the action into the array
94
        $this->aCalls[] = new Method($sMethod, $aArguments);
95
        // Return $this so the calls can be chained
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
     * Get the value of an attribute on the first selected element
101
     *
102
     * @param string  $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
103
     *
104
     * @return DomSelector
105
     */
106
    public function __get(string $sAttribute)
107
    {
108
        // Append the action into the array
109
        $this->aCalls[] = new AttrGet($sAttribute);
110
        // Return $this so the calls can be chained
111
        return $this;
112
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
113
114
    /**
115
     * Set the value of an attribute on the first selected element
116
     *
117
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
118
     * @param mixed $xValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
119
     *
120
     * @return void
121
     */
122
    public function __set(string $sAttribute, $xValue)
123
    {
124
        // Append the action into the array
125
        $this->aCalls[] = new AttrSet($sAttribute, $xValue);
126
        // No other call is allowed after a set
127
        // return $this;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Set an event handler on the first selected element
132
     *
133
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
135
     *
136
     * @return void
137
     */
138
    public function on(string $sName, Call $xHandler)
139
    {
140
        $this->aCalls[] = new Event($sName, $xHandler);
141
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
142
143
    /**
144
     * Set an "click" event handler on the first selected element
145
     *
146
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
147
     *
148
     * @return void
149
     */
150
    public function click(Call $xHandler)
151
    {
152
        $this->aCalls[] = new Event('click', $xHandler);
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * @return DomSelector
157
     */
158
    public function toInt(): DomSelector
159
    {
160
        $this->bToInt = true;
161
        return $this;
162
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
163
164
    /**
165
     * @return array
166
     */
167
    private function selector()
168
    {
169
        $sName = $this->sPath ?? 'this';
170
        $aCall = ['_type' => 'select', '_name' => $sName];
171
        if(($this->xContext))
172
        {
173
            $aCall['context'] = $this->xContext;
174
        }
175
        return $aCall;
176
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
177
178
    /**
179
     * @return array
180
     */
181
    public function toArray(): array
182
    {
183
        $aCalls = [$this->selector()];
184
        foreach($this->aCalls as $xCall)
185
        {
186
            $aCalls[] = $xCall->jsonSerialize();
187
        }
188
        if($this->bToInt)
189
        {
190
            $aCalls[] = [
191
                '_type' => 'func',
192
                '_name' => 'toInt',
193
                'args' => [[ '_type' => '_', '_name' => 'this' ]],
194
            ];
195
        }
196
        return ['_type' => 'expr', 'calls' => $aCalls];
197
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
198
199
    /**
200
     * Generate the jQuery call, when converting the response into json.
201
     * This is a method of the JsonSerializable interface.
202
     *
203
     * @return array
204
     */
205
    public function jsonSerialize(): array
206
    {
207
        return $this->toArray();
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
209
}
210