Passed
Push — master ( 6251ab...a07882 )
by Thierry
01:53
created

DomTrait::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * DomTrait.php - Provides DOM (HTML) related commands for the Response
5
 *
6
 * @author Thierry Feuzeu <[email protected]>
7
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
8
 * @link https://github.com/jaxon-php/jaxon-core
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
10
11
namespace Jaxon\Response\Traits;
12
13
use Jaxon\Response\Response;
14
15
trait DomTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait DomTrait
Loading history...
16
{
17
    /**
18
     * Add a response command to the array of commands that will be sent to the browser
19
     *
20
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
21
     * @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...
22
     * @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...
23
     * @param bool $bRemoveEmpty    If true, remove empty attributes
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
24
     *
25
     * @return Response
26
     */
27
    abstract protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
28
29
    /**
30
     * Add a command to assign the specified value to the given element's attribute
31
     *
32
     * @param string $sTarget    The id of the html element on the browser
33
     * @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...
34
     * @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...
35
     *
36
     * @return Response
37
     */
38
    public function assign(string $sTarget, string $sAttribute, string $sData): Response
39
    {
40
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
41
        return $this->_addCommand('as', $aAttributes, $sData);
42
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
43
44
    /**
45
     * Add a command to assign the specified HTML content to the given element
46
     *
47
     * This is a shortcut for assign() on the innerHTML attribute.
48
     *
49
     * @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...
50
     * @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...
51
     *
52
     * @return Response
53
     */
54
    public function html(string $sTarget, string $sData): Response
55
    {
56
        return $this->assign($sTarget, 'innerHTML', $sData);
57
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
58
59
    /**
60
     * Add a command to append the specified data to the given element's attribute
61
     *
62
     * @param string $sTarget    The id of the element to be updated
63
     * @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...
64
     * @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...
65
     *
66
     * @return Response
67
     */
68
    public function append(string $sTarget, string $sAttribute, string $sData): Response
69
    {
70
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
71
        return $this->_addCommand('ap', $aAttributes, $sData);
72
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
73
74
    /**
75
     * Add a command to prepend the specified data to the given element's attribute
76
     *
77
     * @param string $sTarget    The id of the element to be updated
78
     * @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...
79
     * @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...
80
     *
81
     * @return Response
82
     */
83
    public function prepend(string $sTarget, string $sAttribute, string $sData): Response
84
    {
85
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
86
        return $this->_addCommand('pp', $aAttributes, $sData);
87
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
88
89
    /**
90
     * Add a command to replace a specified value with another value within the given element's attribute
91
     *
92
     * @param string $sTarget    The id of the element to update
93
     * @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...
94
     * @param string $sSearch    The needle to search for
95
     * @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...
96
     *
97
     * @return Response
98
     */
99
    public function replace(string $sTarget, string $sAttribute, string $sSearch, string $sData): Response
100
    {
101
        $aAttributes = ['id' => $sTarget, 'prop' => $sAttribute];
102
        $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...
103
        return $this->_addCommand('rp', $aAttributes, $aData);
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Add a command to clear the specified attribute of the given element
108
     *
109
     * @param string $sTarget    The id of the element to be updated.
110
     * @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...
111
     *
112
     * @return Response
113
     */
114
    public function clear(string $sTarget, string $sAttribute = 'innerHTML'): Response
115
    {
116
        return $this->assign($sTarget, $sAttribute, '');
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * Add a command to assign a value to a member of a javascript object (or element)
121
     * that is specified by the context member of the request
122
     *
123
     * The object is referenced using the 'this' keyword in the sAttribute parameter.
124
     *
125
     * @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...
126
     * @param string $sData    The value to assign
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
127
     *
128
     * @return Response
129
     */
130
    public function contextAssign(string $sAttribute, string $sData): Response
131
    {
132
        $aAttributes = ['prop' => $sAttribute];
133
        return $this->_addCommand('c:as', $aAttributes, $sData);
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Add a command to append a value onto the specified member of the javascript
138
     * context object (or element) specified by the context member of the request
139
     *
140
     * The object is referenced using the 'this' keyword in the sAttribute parameter.
141
     *
142
     * @param string $sAttribute    The attribute to be appended to
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
143
     * @param string $sData    The value to append
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
144
     *
145
     * @return Response
146
     */
147
    public function contextAppend(string $sAttribute, string $sData): Response
148
    {
149
        $aAttributes = ['prop' => $sAttribute];
150
        return $this->_addCommand('c:ap', $aAttributes, $sData);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
154
     * Add a command to prepend the speicified data to the given member of the current
155
     * javascript object specified by context in the current request
156
     *
157
     * The object is access via the 'this' keyword in the sAttribute parameter.
158
     *
159
     * @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...
160
     * @param string $sData    The value to be prepended
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
161
     *
162
     * @return Response
163
     */
164
    public function contextPrepend(string $sAttribute, string $sData): Response
165
    {
166
        $aAttributes = ['prop' => $sAttribute];
167
        return $this->_addCommand('c:pp', $aAttributes, $sData);
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * Add a command to to clear the value of the attribute specified in the sAttribute parameter
172
     *
173
     * The member is access via the 'this' keyword and can be used to update a javascript
174
     * object specified by context in the request parameters.
175
     *
176
     * @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...
177
     *
178
     * @return Response
179
     */
180
    public function contextClear(string $sAttribute): Response
181
    {
182
        return $this->contextAssign($sAttribute, '');
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * Add a command to remove an element from the document
187
     *
188
     * @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...
189
     *
190
     * @return Response
191
     */
192
    public function remove(string $sTarget): Response
193
    {
194
        $aAttributes = ['id' => $sTarget];
195
        return $this->_addCommand('rm', $aAttributes, '');
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * Add a command to create a new element on the browser
200
     *
201
     * @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...
202
     * @param string $sTag    The tag name to be used for the new element
203
     * @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...
204
     *
205
     * @return Response
206
     */
207
    public function create(string $sParent, string $sTag, string $sId): Response
208
    {
209
        $aAttributes = ['id' => $sParent, 'prop' => $sId];
210
        return $this->_addCommand('ce', $aAttributes, $sTag);
211
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
212
213
    /**
214
     * Add a command to insert a new element just prior to the specified element
215
     *
216
     * @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...
217
     * @param string $sTag    The tag name to be used for the new element
218
     * @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...
219
     *
220
     * @return Response
221
     */
222
    public function insertBefore(string $sBefore, string $sTag, string $sId): Response
223
    {
224
        $aAttributes = ['id' => $sBefore, 'prop' => $sId];
225
        return $this->_addCommand('ie', $aAttributes, $sTag);
226
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
227
228
    /**
229
     * Add a command to insert a new element just prior to the specified element
230
     * This is an alias for insertBefore.
231
     *
232
     * @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...
233
     * @param string $sTag    The tag name to be used for the new element
234
     * @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...
235
     *
236
     * @return Response
237
     */
238
    public function insert(string $sBefore, string $sTag, string $sId): Response
239
    {
240
        return $this->insertBefore($sBefore, $sTag, $sId);
241
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
242
243
    /**
244
     * Add a command to insert a new element after the specified
245
     *
246
     * @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...
247
     * @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...
248
     * @param string $sId    The id to assign to the new element
249
     *
250
     * @return Response
251
     */
252
    public function insertAfter(string $sAfter, string $sTag, string $sId): Response
253
    {
254
        $aAttributes = ['id' => $sAfter, 'prop' => $sId];
255
        return $this->_addCommand('ia', $aAttributes, $sTag);
256
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
257
258
    /**
259
     * Add a command to create an input element on the browser
260
     *
261
     * @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...
262
     * @param string $sType    The type of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
263
     * @param string $sName    The name of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
264
     * @param string $sId    The id of the new element
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
265
     *
266
     * @return Response
267
     */
268
    public function createInput(string $sParent, string $sType, string $sName, string $sId): Response
269
    {
270
        $aAttributes = ['id' => $sParent, 'prop' => $sId, 'type' => $sType];
271
        return $this->_addCommand('ci', $aAttributes, $sName);
272
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
273
274
    /**
275
     * Add a command to insert a new input element preceding the specified element
276
     *
277
     * @param string $sBefore    The id of the element to be used as the reference point for the insertion
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
278
     * @param string $sType    The type of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
279
     * @param string $sName    The name of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
280
     * @param string $sId    The id of the new element
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
281
     *
282
     * @return Response
283
     */
284
    public function insertInput(string $sBefore, string $sType, string $sName, string $sId): Response
285
    {
286
        $aAttributes = ['id' => $sBefore, 'prop' => $sId, 'type' => $sType];
287
        return $this->_addCommand('ii', $aAttributes, $sName);
288
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
289
290
    /**
291
     * Add a command to insert a new input element after the specified element
292
     *
293
     * @param string $sAfter    The id of the element to be used as the reference point for the insertion
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
294
     * @param string $sType    The type of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
295
     * @param string $sName    The name of the new input element
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
296
     * @param string $sId    The id of the new element
297
     *
298
     * @return Response
299
     */
300
    public function insertInputAfter(string $sAfter, string $sType, string $sName, string $sId): Response
301
    {
302
        $aAttributes = ['id' => $sAfter, 'prop' => $sId, 'type' => $sType];
303
        return $this->_addCommand('iia', $aAttributes, $sName);
304
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
305
}
306