Passed
Push — v5.x ( 6eb5e2...afe246 )
by Thierry
02:28
created

Selector::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
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Selector.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 Selector object must be converted to the corresponding jQuery code.
9
 * Therefore, the Selector class implements the JsonSerializable interface.
10
 *
11
 * When used as a parameter of a Jaxon call, the Selector must be converted to Jaxon request parameter.
12
 * Therefore, the Selector class also implements the Jaxon\Request\Js\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\Request\Js;
22
23
use function implode;
24
use function trim;
25
26
class Selector implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Selector
Loading history...
27
{
28
    /**
29
     * The jQuery selector path
30
     *
31
     * @var string
32
     */
33
    protected $sPath;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * The jQuery selector context
37
     *
38
     * @var mixed
39
     */
40
    protected $xContext;
41
42
    /**
43
     * The actions to be applied on the selected element
44
     *
45
     * @var array
46
     */
47
    protected $aCalls = [];
48
49
    /**
50
     * Convert the selector value to integer
51
     *
52
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
53
     */
54
    protected $bToInt = false;
55
56
    /**
57
     * The constructor.
58
     *
59
     * @param string $sPath    The jQuery selector path
60
     * @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
Expected 1 spaces after parameter name; 4 found
Loading history...
61
     */
62
    public function __construct(string $sPath, $xContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
63
    {
64
        $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...
65
        $this->xContext = $xContext;
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * @inheritDoc
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71
    public function getType(): string
72
    {
73
        return 'selector';
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * Add a call to a jQuery method on the selected elements
78
     *
79
     * @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...
80
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     *
82
     * @return Selector
83
     */
84
    public function __call(string $sMethod, array $aArguments)
85
    {
86
        // Append the action into the array
87
        $this->aCalls[] = new Selector\Method($sMethod, $aArguments);
88
        // Return $this so the calls can be chained
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
     * Get the value of an attribute on the first selected element
94
     *
95
     * @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...
96
     *
97
     * @return Selector
98
     */
99
    public function __get(string $sAttribute)
100
    {
101
        // Append the action into the array
102
        $this->aCalls[] = new Selector\AttrGet($sAttribute);
103
        // Return $this so the calls can be chained
104
        return $this;
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Set the value of an attribute on the first selected element
109
     *
110
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @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...
112
     *
113
     * @return void
114
     */
115
    public function __set(string $sAttribute, $xValue)
116
    {
117
        // Append the action into the array
118
        $this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue);
119
        // No other call is allowed after a set
120
        // return $this;
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
124
     * Set an event handler on the first selected element
125
     *
126
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
127
     * @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...
128
     *
129
     * @return void
130
     */
131
    public function on(string $sName, Call $xHandler)
132
    {
133
        $this->aCalls[] = new Selector\Event($sName, $xHandler);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Set an "click" event handler on the first selected element
138
     *
139
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
     *
141
     * @return void
142
     */
143
    public function click(Call $xHandler)
144
    {
145
        $this->on('click', $xHandler);
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * @return Selector
150
     */
151
    public function toInt(): Selector
152
    {
153
        $this->bToInt = true;
154
        return $this;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * @return array
159
     */
160
    private function selector()
161
    {
162
        $sName = $this->sPath ?? 'this';
163
        $aCall = ['_type' => 'select', '_name' => $sName];
164
        if(($this->xContext))
165
        {
166
            $aCall['context'] = $this->xContext;
167
        }
168
        return $aCall;
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
170
171
    /**
172
     * Get the selector js.
173
     *
174
     * @return string
175
     */
176
    private function getPath()
177
    {
178
        $jQuery = 'jaxon.jq'; // The JQuery selector
179
        if(!$this->sPath)
180
        {
181
            // If an empty selector is given, use the event target instead
182
            return "$jQuery(e.currentTarget)";
183
        }
184
        if(!$this->xContext)
185
        {
186
            return "$jQuery('" . $this->sPath . "')";
187
        }
188
189
        $sContext = is_a($this->xContext, self::class) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
190
            $this->xContext->getScript() :
0 ignored issues
show
Bug introduced by
The method getScript() does not exist on Jaxon\Request\Js\Selector. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
            $this->xContext->/** @scrutinizer ignore-call */ 
191
                             getScript() :
Loading history...
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
191
            "$jQuery('" . trim("{$this->xContext}") . "')";
192
        return "$jQuery('{$this->sPath}', $sContext)";
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
194
195
    /**
196
     * Generate the jQuery call.
197
     *
198
     * @return string
199
     */
200
    public function __toString(): string
201
    {
202
        $sScript = $this->getPath() . implode('', $this->aCalls);
203
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * @return array
208
     */
209
    public function toArray(): array
210
    {
211
        $aCalls = [$this->selector()];
212
        foreach($this->aCalls as $xCall)
213
        {
214
            $aCalls[] = $xCall->jsonSerialize();
215
        }
216
        if($this->bToInt)
217
        {
218
            $aCalls[] = [
219
                '_type' => 'func',
220
                '_name' => 'toInt',
221
                'args' => [[ '_type' => '_', '_name' => 'this' ]],
222
            ];
223
        }
224
        return ['_type' => 'expr', 'calls' => $aCalls];
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
228
     * Generate the jQuery call, when converting the response into json.
229
     * This is a method of the JsonSerializable interface.
230
     *
231
     * @return array
232
     */
233
    public function jsonSerialize(): array
234
    {
235
        return $this->toArray();
236
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
237
}
238