Passed
Push — main ( 661f96...723415 )
by Thierry
02:20
created

DomSelector::cb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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