Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
created

DomSelector::makeCallsArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
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\Method;
26
use Jaxon\Request\Call\JsCall;
27
use Jaxon\Request\Call\ParameterInterface;
28
29
use function array_merge;
30
use function count;
31
use function is_a;
32
use function trim;
33
34
class DomSelector implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DomSelector
Loading history...
35
{
36
    /**
37
     * The jQuery selector path
38
     *
39
     * @var string
40
     */
41
    protected $sPath;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
42
43
    /**
44
     * The jQuery selector path
45
     *
46
     * @var mixed
47
     */
48
    protected $xContext;
49
50
    /**
51
     * The actions to be applied on the selected element
52
     *
53
     * @var array
54
     */
55
    protected $aCalls;
56
57
    /**
58
     * Convert the selector value to integer
59
     *
60
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
61
     */
62
    protected $bToInt = false;
63
64
    /**
65
     * True if this selector is a callback
66
     *
67
     * @var bool|null
0 ignored issues
show
Bug introduced by
Expected "boolean|null" but found "bool|null" for @var tag in member variable comment
Loading history...
68
     */
69
    protected $bIsCallback = null;
70
71
    /**
72
     * The constructor.
73
     *
74
     * @param string $jQueryNs    The jQuery symbol
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Doc comment for parameter $jQueryNs does not match actual variable name $sPath
Loading history...
75
     * @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...
76
     * @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...
Coding Style introduced by
Superfluous parameter comment
Loading history...
77
     */
78
    public function __construct(string $sPath, $xContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
79
    {
80
        $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...
81
        $this->xContext = $xContext;
82
        $this->aCalls = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
84
    /**
85
     * @inheritDoc
86
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
87
    public function getType(): string
88
    {
89
        return 'select';
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Add a call to a jQuery method on the selected elements
94
     *
95
     * @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...
96
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
97
     *
98
     * @return DomSelector
99
     */
100
    public function __call(string $sMethod, array $aArguments)
101
    {
102
        if(count($aArguments) === 1)
103
        {
104
            // If the only parameter is a selector, and the first call
105
            // on that selector is a method, then the selector is a callback.
106
            $xArgument = $aArguments[0];
107
            if(is_a($xArgument, self::class) && $xArgument->bIsCallback === null &&
108
                count($xArgument->aCalls) > 0 && is_a($xArgument->aCalls[0], JsCall::class))
109
            {
110
                $xArgument->bIsCallback = true;
111
            }
112
        }
113
        // Push the action into the array
114
        $this->aCalls[] = new Method($sMethod, $aArguments);
115
        // Return $this so the calls can be chained
116
        return $this;
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * Get the value of an attribute on the first selected element
121
     *
122
     * @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...
123
     *
124
     * @return DomSelector
125
     */
126
    public function __get(string $sAttribute)
127
    {
128
        // Push the action into the array
129
        $this->aCalls[] = new AttrGet($sAttribute);
130
        // Return $this so the calls can be chained
131
        return $this;
132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
133
134
    /**
135
     * Set the value of an attribute on the first selected element
136
     *
137
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     * @param $xValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     *
140
     * @return void
141
     */
142
    public function __set(string $sAttribute, $xValue)
143
    {
144
        // Push the action into the array
145
        $this->aCalls[] = new AttrSet($sAttribute, $xValue);
146
        // No other call is allowed after a set
147
        // return $this;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Explicitely declare the selector as a callback.
152
     *
153
     * @param bool $bIsCallback
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
154
     *
155
     * @return DomSelector
156
     */
157
    public function cb(bool $bIsCallback = true): DomSelector
158
    {
159
        $this->bIsCallback = $bIsCallback;
160
        return $this;
161
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
162
163
    /**
164
     * @return DomSelector
165
     */
166
    public function toInt(): DomSelector
167
    {
168
        $this->bToInt = true;
169
        return $this;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * @return array
174
     */
175
    private function selectCalls()
176
    {
177
        if(!$this->sPath)
178
        {
179
            // If an empty selector is given, use the event target instead
180
            return [['_type' => 'select', '_name' => 'this']];
181
        }
182
        if(!$this->xContext)
183
        {
184
            return [['_type' => 'select', '_name' => $this->sPath]];
185
        }
186
        // Todo: chain the 2 selectors.
187
        return [
188
            // ['_type' => 'select', '_name' => $this->xContext],
189
            ['_type' => 'select', '_name' => $this->sPath],
190
        ];
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
194
     * @param Method|AttrSet|AttrGet $xParam
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
195
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
196
    private function makeCallsArray($xParam): array
197
    {
198
        if(!is_a($xParam, Method::class))
199
        {
200
            // Return an array of array.
201
            return [$xParam->jsonSerialize()];
202
        }
203
        // The param is serialized to an array of arrays.
204
        $aCalls = $xParam->jsonSerialize();
205
        // Set the correct type on the first call.
206
        $aCalls[0]['_type'] = $this->bIsCallback ? 'event' : 'method';
207
        return $aCalls;
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
209
210
    /**
211
     * @return array
212
     */
213
    public function toArray(): array
214
    {
215
        $aCalls = $this->selectCalls();
216
        foreach($this->aCalls as $xCall)
217
        {
218
            $aCalls = array_merge($aCalls, $this->makeCallsArray($xCall));
219
        }
220
        if($this->bToInt)
221
        {
222
            $aCalls[] = [
223
                '_type' => 'func',
224
                '_name' => 'jaxon.utils.string.toInt',
225
                'params' => [
226
                    [ '_type' => '_', '_name' => 'this' ],
227
                ],
228
            ];
229
        }
230
        return $aCalls;
231
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
232
233
    /**
234
     * Generate the jQuery call, when converting the response into json.
235
     *
236
     * This is a method of the JsonSerializable interface.
237
     *
238
     * @return array
239
     */
240
    public function jsonSerialize(): array
241
    {
242
        return $this->toArray();
243
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
244
}
245