Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

DomTrait::contextAssign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DomTrait.php
5
 *
6
 * Provides DOM (HTML) related commands for the Response
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
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...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
12
13
namespace Jaxon\Response\Traits;
14
15
use Jaxon\Response\ResponseInterface;
16
17
trait DomTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait DomTrait
Loading history...
18
{
19
    /**
20
     * Add a response command to the array of commands that will be sent to the browser
21
     *
22
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
23
     * @param array $aAttributes    Associative array of attributes that will describe the command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
24
     * @param mixed $mData    The data to be associated with this command
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
25
     * @param bool $bRemoveEmpty    If true, remove empty attributes
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
26
     *
27
     * @return ResponseInterface
28
     */
29
    abstract protected function _addCommand(string $sName, array $aAttributes,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
30
        $mData, bool $bRemoveEmpty = false): ResponseInterface;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
31
32
    /**
33
     * Add a command to assign the specified value to the given element's attribute
34
     *
35
     * @param string $sTarget    The id of the html element on the browser
36
     * @param string $sAttribute    The attribute to be assigned
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
37
     * @param string $sData    The value to be assigned to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
38
     *
39
     * @return ResponseInterface
40
     */
41
    public function assign(string $sTarget, string $sAttribute, string $sData): ResponseInterface
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
42
    {
43
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
44
        return $this->_addCommand('as', $aAttributes, $sData);
45
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
46
47
    /**
48
     * Add a command to assign the specified HTML content to the given element
49
     *
50
     * This is a shortcut for assign() on the innerHTML attribute.
51
     *
52
     * @param string $sTarget    The id of the html element on the browser
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
53
     * @param string $sData    The value to be assigned to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function html(string $sTarget, string $sData): ResponseInterface
58
    {
59
        return $this->assign($sTarget, 'innerHTML', $sData);
60
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
61
62
    /**
63
     * Add a command to append the specified data to the given element's attribute
64
     *
65
     * @param string $sTarget    The id of the element to be updated
66
     * @param string $sAttribute    The name of the attribute to be appended to
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
67
     * @param string $sData    The data to be appended to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
68
     *
69
     * @return ResponseInterface
70
     */
71
    public function append(string $sTarget, string $sAttribute, string $sData): ResponseInterface
72
    {
73
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
74
        return $this->_addCommand('ap', $aAttributes, $sData);
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Add a command to prepend the specified data to the given element's attribute
79
     *
80
     * @param string $sTarget    The id of the element to be updated
81
     * @param string $sAttribute    The name of the attribute to be prepended to
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
82
     * @param string $sData    The value to be prepended to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
83
     *
84
     * @return ResponseInterface
85
     */
86
    public function prepend(string $sTarget, string $sAttribute, string $sData): ResponseInterface
87
    {
88
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
89
        return $this->_addCommand('pp', $aAttributes, $sData);
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Add a command to replace a specified value with another value within the given element's attribute
94
     *
95
     * @param string $sTarget    The id of the element to update
96
     * @param string $sAttribute    The attribute to be updated
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
97
     * @param string $sSearch    The needle to search for
98
     * @param string $sData    The data to use in place of the needle
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function replace(string $sTarget, string $sAttribute, string $sSearch, string $sData): ResponseInterface
103
    {
104
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
105
        $aData = ['s' => $sSearch, 'r' => $sData];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
106
        return $this->_addCommand('rp', $aAttributes, $aData);
107
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
108
109
    /**
110
     * Add a command to clear the specified attribute of the given element
111
     *
112
     * @param string $sTarget    The id of the element to be updated.
113
     * @param string $sAttribute    The attribute to be cleared
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
114
     *
115
     * @return ResponseInterface
116
     */
117
    public function clear(string $sTarget, string $sAttribute = 'innerHTML'): ResponseInterface
118
    {
119
        return $this->assign($sTarget, $sAttribute, '');
120
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
121
122
    /**
123
     * Add a command to remove an element from the document
124
     *
125
     * @param string $sTarget    The id of the element to be removed
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
126
     *
127
     * @return ResponseInterface
128
     */
129
    public function remove(string $sTarget): ResponseInterface
130
    {
131
        $aAttributes = ['id' => $sTarget];
132
        return $this->_addCommand('rm', $aAttributes, '');
133
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
134
135
    /**
136
     * Add a command to create a new element on the browser
137
     *
138
     * @param string $sParent    The id of the parent element
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
139
     * @param string $sTag    The tag name to be used for the new element
140
     * @param string $sId    The id to assign to the new element
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
141
     *
142
     * @return ResponseInterface
143
     */
144
    public function create(string $sParent, string $sTag, string $sId): ResponseInterface
145
    {
146
        $aAttributes = ['id' => $sParent, 'prop' => $sId];
147
        return $this->_addCommand('ce', $aAttributes, $sTag);
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Add a command to insert a new element just prior to the specified element
152
     *
153
     * @param string $sBefore    The id of the element used as a reference point for the insertion
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
154
     * @param string $sTag    The tag name to be used for the new element
155
     * @param string $sId    The id to assign to the new element
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
156
     *
157
     * @return ResponseInterface
158
     */
159
    public function insertBefore(string $sBefore, string $sTag, string $sId): ResponseInterface
160
    {
161
        $aAttributes = ['id' => $sBefore, 'prop' => $sId];
162
        return $this->_addCommand('ie', $aAttributes, $sTag);
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
164
165
    /**
166
     * Add a command to insert a new element just prior to the specified element
167
     * This is an alias for insertBefore.
168
     *
169
     * @param string $sBefore    The id of the element used as a reference point for the insertion
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
170
     * @param string $sTag    The tag name to be used for the new element
171
     * @param string $sId    The id to assign to the new element
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
172
     *
173
     * @return ResponseInterface
174
     */
175
    public function insert(string $sBefore, string $sTag, string $sId): ResponseInterface
176
    {
177
        return $this->insertBefore($sBefore, $sTag, $sId);
178
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
179
180
    /**
181
     * Add a command to insert a new element after the specified
182
     *
183
     * @param string $sAfter    The id of the element used as a reference point for the insertion
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
184
     * @param string $sTag    The tag name to be used for the new element
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
185
     * @param string $sId    The id to assign to the new element
186
     *
187
     * @return ResponseInterface
188
     */
189
    public function insertAfter(string $sAfter, string $sTag, string $sId): ResponseInterface
190
    {
191
        $aAttributes = ['id' => $sAfter, 'prop' => $sId];
192
        return $this->_addCommand('ia', $aAttributes, $sTag);
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
194
}
195