Passed
Push — v5.x ( fe38d4...6ac3c7 )
by Thierry
10:27
created

Selector::toArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 0
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 js call 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 is_a;
25
use function trim;
26
27
class Selector implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Selector
Loading history...
28
{
29
    /**
30
     * The jQuery selector path
31
     *
32
     * @var string
33
     */
34
    protected $sPath;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
35
36
    /**
37
     * The jQuery selector context
38
     *
39
     * @var mixed
40
     */
41
    protected $xContext;
42
43
    /**
44
     * The actions to be applied on the selected element
45
     *
46
     * @var array
47
     */
48
    protected $aCalls = [];
49
50
    /**
51
     * Convert the selector value to integer
52
     *
53
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
54
     */
55
    protected $bToInt = false;
56
57
    /**
58
     * The constructor.
59
     *
60
     * @param string $sPath    The jQuery selector path
61
     * @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...
62
     */
63
    public function __construct(string $sPath, $xContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
64
    {
65
        $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...
66
        $this->xContext = $xContext;
67
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
68
69
    /**
70
     * @inheritDoc
71
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
72
    public function getType(): string
73
    {
74
        return 'expr';
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Add a call to a jQuery method on the selected elements
79
     *
80
     * @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...
81
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     *
83
     * @return Selector
84
     */
85
    public function __call(string $sMethod, array $aArguments)
86
    {
87
        // Append the action into the array
88
        $this->aCalls[] = new Selector\Method($sMethod, $aArguments);
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;
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Set the value of an attribute on the first selected element
108
     *
109
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     * @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...
111
     *
112
     * @return void
113
     */
114
    public function __set(string $sAttribute, $xValue)
115
    {
116
        // Append the action into the array
117
        $this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue);
118
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Request\Js\Selector which is incompatible with the documented return type void.
Loading history...
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Set an event handler on the first selected element
123
     *
124
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     * @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...
126
     *
127
     * @return Selector
128
     */
129
    public function on(string $sName, Call $xHandler): Selector
130
    {
131
        $this->aCalls[] = new Selector\Event($sName, $xHandler);
132
        return $this;
133
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
134
135
    /**
136
     * Set an "click" event handler on the first selected element
137
     *
138
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     *
140
     * @return Selector
141
     */
142
    public function click(Call $xHandler): Selector
143
    {
144
        $this->on('click', $xHandler);
145
        return $this;
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
     * Get the selector js.
159
     *
160
     * @return string
161
     */
162
    private function getPathAsStr()
163
    {
164
        $jQuery = 'jaxon.jq'; // The JQuery selector
165
        if(!$this->sPath)
166
        {
167
            // If an empty selector is given, use the event target instead
168
            return "$jQuery(e.currentTarget)";
169
        }
170
        if(!$this->xContext)
171
        {
172
            return "$jQuery('" . $this->sPath . "')";
173
        }
174
175
        $sContext = is_a($this->xContext, self::class) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
176
            $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

176
            $this->xContext->/** @scrutinizer ignore-call */ 
177
                             getScript() :
Loading history...
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
177
            "$jQuery('" . trim("{$this->xContext}") . "')";
178
        return "$jQuery('{$this->sPath}', $sContext)";
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
182
     * Generate the jQuery call.
183
     *
184
     * @return string
185
     */
186
    public function __toString(): string
187
    {
188
        $sScript = $this->getPathAsStr() . implode('', $this->aCalls);
189
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
190
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
191
192
    /**
193
     * @return array
194
     */
195
    private function getPathAsArray()
196
    {
197
        $sName = $this->sPath ?? 'this';
198
        $aCall = ['_type' => 'select', '_name' => $sName];
199
        if(($this->xContext))
200
        {
201
            $aCall['context'] = is_a($this->xContext, self::class) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
202
                $this->xContext->jsonSerialize() :$this->xContext;
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; 0 found
Loading history...
203
        }
204
        return $aCall;
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Generate the jQuery call, when converting the response into json.
209
     *
210
     * @return array
211
     */
212
    public function jsonSerialize(): array
213
    {
214
        $aCalls = [$this->getPathAsArray()];
215
        foreach($this->aCalls as $xCall)
216
        {
217
            $aCalls[] = $xCall->jsonSerialize();
218
        }
219
        if($this->bToInt)
220
        {
221
            $aCalls[] = [
222
                '_type' => 'method',
223
                '_name' => 'toInt',
224
                'args' => [],
225
            ];
226
        }
227
        return ['_type' => $this->getType(), 'calls' => $aCalls];
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
229
}
230