Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

DomSelector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getScript() 0 7 2
A jsonSerialize() 0 3 1
A __toString() 0 3 1
A __construct() 0 17 3
A __get() 0 6 1
A __set() 0 4 1
A __call() 0 6 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\Contracts\Parameter 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
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
20
21
namespace Jaxon\Response\Plugin\JQuery;
22
23
use Jaxon\Request\Call\Contracts\Parameter;
24
use Jaxon\Response\Plugin\JQuery\Call\AttrGet;
25
use Jaxon\Response\Plugin\JQuery\Call\AttrSet;
26
use Jaxon\Response\Plugin\JQuery\Call\Method;
27
28
use JsonSerializable;
29
30
use function count;
31
use function implode;
32
use function trim;
33
34
class DomSelector implements JsonSerializable, Parameter
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
     * The constructor.
52
     *
53
     * @param string $jQueryNs    The jQuery symbol
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
54
     * @param string $sPath    The jQuery selector path
55
     * @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...
56
     */
57
    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...
58
    {
59
        $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...
60
        $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...
61
        $this->aCalls = [];
62
63
        if(!$sPath)
64
        {
65
            $this->sPath = "$jQueryNs(this)"; // If an empty selector is given, use javascript "this" instead
66
        }
67
        elseif(($sContext))
68
        {
69
            $this->sPath = "$jQueryNs('" . $sPath . "', $jQueryNs('" . $sContext . "'))";
70
        }
71
        else
72
        {
73
            $this->sPath = "$jQueryNs('" . $sPath . "')";
74
        }
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
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     *
83
     * @return DomSelector
84
     */
85
    public function __call(string $sMethod, array $aArguments)
86
    {
87
        // Push the action into the array
88
        $this->aCalls[] = new Method($sMethod, $aArguments);
89
        // Return $this so the calls can be chained
90
        return $this;
91
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
92
93
    /**
94
     * Get the value of an attribute on the first selected element
95
     *
96
     * @param string  $sAttribute
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
97
     *
98
     * @return DomSelector
99
     */
100
    public function __get(string $sAttribute)
101
    {
102
        // Push the action into the array
103
        $this->aCalls[] = new AttrGet($sAttribute);
104
        // Return $this so the calls can be chained
105
        return $this;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * Set the value of an attribute on the first selected element
110
     *
111
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     * @param $xValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
113
     *
114
     * @return void
115
     */
116
    public function __set(string $sAttribute, $xValue)
117
    {
118
        // Push the action into the array
119
        $this->aCalls[] = new AttrSet($sAttribute, $xValue);
120
        // No other call is allowed after a set
121
        // return $this;
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Generate the jQuery call.
126
     *
127
     * @return string
128
     */
129
    public function getScript(): string
130
    {
131
        if(count($this->aCalls) === 0)
132
        {
133
            return $this->sPath;
134
        }
135
        return $this->sPath . '.' . implode('.', $this->aCalls);
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Magic function to generate the jQuery call.
140
     *
141
     * @return string
142
     */
143
    public function __toString()
144
    {
145
        return $this->getScript();
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Generate the jQuery call, when converting the response into json.
150
     *
151
     * This is a method of the JsonSerializable interface.
152
     *
153
     * @return string
154
     */
155
    public function jsonSerialize(): string
156
    {
157
        return $this->getScript();
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
159
}
160