Test Setup Failed
Branch feature/code_improvement (b0d56f)
by Thierry
02:54
created

DomCommands::assign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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