Passed
Push — master ( 5212d6...22a324 )
by Thierry
03:24
created

DomSelector::toInt()   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 0
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\ParameterInterface;
27
28
use JsonSerializable;
29
30
use function count;
31
use function implode;
32
use function trim;
33
34
class DomSelector implements JsonSerializable, 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 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 $jQueryNs    The jQuery symbol
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
61
     * @param string $sPath    The jQuery selector path
62
     * @param string $sContext    A context associated to the selector
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
63
     */
64
    public function __construct(string $jQueryNs, string $sPath, string $sContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
65
    {
66
        $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...
67
        $sContext = trim($sContext, " \t");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
68
        $this->aCalls = [];
69
70
        if(!$sPath)
71
        {
72
            $this->sPath = "$jQueryNs(this)"; // If an empty selector is given, use javascript "this" instead
73
        }
74
        elseif(($sContext))
75
        {
76
            $this->sPath = "$jQueryNs('" . $sPath . "', $jQueryNs('" . $sContext . "'))";
77
        }
78
        else
79
        {
80
            $this->sPath = "$jQueryNs('" . $sPath . "')";
81
        }
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * Add a call to a jQuery method on the selected elements
86
     *
87
     * @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...
88
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     *
90
     * @return DomSelector
91
     */
92
    public function __call(string $sMethod, array $aArguments)
93
    {
94
        // Push the action into the array
95
        $this->aCalls[] = new Method($sMethod, $aArguments);
96
        // Return $this so the calls can be chained
97
        return $this;
98
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
99
100
    /**
101
     * Get the value of an attribute on the first selected element
102
     *
103
     * @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...
104
     *
105
     * @return DomSelector
106
     */
107
    public function __get(string $sAttribute)
108
    {
109
        // Push the action into the array
110
        $this->aCalls[] = new AttrGet($sAttribute);
111
        // Return $this so the calls can be chained
112
        return $this;
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
114
115
    /**
116
     * Set the value of an attribute on the first selected element
117
     *
118
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
119
     * @param $xValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
120
     *
121
     * @return void
122
     */
123
    public function __set(string $sAttribute, $xValue)
124
    {
125
        // Push the action into the array
126
        $this->aCalls[] = new AttrSet($sAttribute, $xValue);
127
        // No other call is allowed after a set
128
        // return $this;
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * @return DomSelector
133
     */
134
    public function toInt(): DomSelector
135
    {
136
        $this->bToInt = true;
137
        return $this;
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * Generate the jQuery call.
142
     *
143
     * @return string
144
     */
145
    public function getScript(): string
146
    {
147
        $sScript = $this->sPath;
148
        if(count($this->aCalls) > 0)
149
        {
150
            $sScript .= '.' . implode('.', $this->aCalls);
151
        }
152
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * Magic function to generate the jQuery call.
157
     *
158
     * @return string
159
     */
160
    public function __toString()
161
    {
162
        return $this->getScript();
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
164
165
    /**
166
     * Generate the jQuery call, when converting the response into json.
167
     *
168
     * This is a method of the JsonSerializable interface.
169
     *
170
     * @return string
171
     */
172
    public function jsonSerialize(): string
173
    {
174
        return $this->getScript();
175
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
176
}
177