Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
created

HtmlDomTrait::insertBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
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
use JsonSerializable;
17
18
trait HtmlDomTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait HtmlDomTrait
Loading history...
19
{
20
     /**
21
     * Add a response command to the array of commands that will be sent to the browser
22
     *
23
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
24
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
25
     *
26
     * @return ResponseInterface
27
     */
28
    abstract public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface;
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
29
30
    /**
31
     * Add a command to assign the specified value to the given element's attribute
32
     *
33
     * @param string $sTarget    The id of the html element on the browser
34
     * @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...
35
     * @param string $sValue    The value to be assigned to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
36
     *
37
     * @return ResponseInterface
38
     */
39
    public function assign(string $sTarget, string $sAttribute, string $sValue): ResponseInterface
40
    {
41
        return $this->addCommand('as', [
42
            'id' => $this->str($sTarget),
0 ignored issues
show
Bug introduced by
It seems like str() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
            'id' => $this->/** @scrutinizer ignore-call */ str($sTarget),
Loading history...
43
            'attr' => $this->str($sAttribute),
44
            'value' => $this->str($sValue),
45
        ]);
46
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
47
48
    /**
49
     * Add a command to assign the specified HTML content to the given element
50
     *
51
     * This is a shortcut for assign() on the innerHTML attribute.
52
     *
53
     * @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...
54
     * @param string $sValue    The value to be assigned to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function html(string $sTarget, string $sValue): ResponseInterface
59
    {
60
        return $this->assign($sTarget, 'innerHTML', $sValue);
61
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
62
63
    /**
64
     * Add a command to append the specified data to the given element's attribute
65
     *
66
     * @param string $sTarget    The id of the element to be updated
67
     * @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...
68
     * @param string $sValue    The data to be appended to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function append(string $sTarget, string $sAttribute, string $sValue): ResponseInterface
73
    {
74
        return $this->addCommand('ap', [
75
            'id' => $this->str($sTarget),
76
            'attr' => $this->str($sAttribute),
77
            'value' => $this->str($sValue),
78
        ]);
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * Add a command to prepend the specified data to the given element's attribute
83
     *
84
     * @param string $sTarget    The id of the element to be updated
85
     * @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...
86
     * @param string $sValue    The value to be prepended to the attribute
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
87
     *
88
     * @return ResponseInterface
89
     */
90
    public function prepend(string $sTarget, string $sAttribute, string $sValue): ResponseInterface
91
    {
92
        return $this->addCommand('pp', [
93
            'id' => $this->str($sTarget),
94
            'attr' => $this->str($sAttribute),
95
            'value' => $this->str($sValue),
96
        ]);
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Add a command to replace a specified value with another value within the given element's attribute
101
     *
102
     * @param string $sTarget    The id of the element to update
103
     * @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...
104
     * @param string $sSearch    The needle to search for
105
     * @param string $sReplace    The data to use in place of the needle
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
106
     *
107
     * @return ResponseInterface
108
     */
109
    public function replace(string $sTarget, string $sAttribute,
110
        string $sSearch, string $sReplace): ResponseInterface
111
    {
112
        return $this->addCommand('rp', [
113
            'id' => $this->str($sTarget),
114
            'attr' => $this->str($sAttribute),
115
            'search' => $this->str($sSearch),
116
            'replace' => $this->str($sReplace),
117
        ]);
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Add a command to clear the specified attribute of the given element
122
     *
123
     * @param string $sTarget    The id of the element to be updated.
124
     * @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...
125
     *
126
     * @return ResponseInterface
127
     */
128
    public function clear(string $sTarget, string $sAttribute = 'innerHTML'): ResponseInterface
129
    {
130
        return $this->assign($sTarget, $sAttribute, '');
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Add a command to remove an element from the document
135
     *
136
     * @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...
137
     *
138
     * @return ResponseInterface
139
     */
140
    public function remove(string $sTarget): ResponseInterface
141
    {
142
        return $this->addCommand('rm', ['id' => $this->str($sTarget)]);
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * Add a command to create a new element on the browser
147
     *
148
     * @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...
149
     * @param string $sTag    The tag name to be used for the new element
150
     * @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...
151
     *
152
     * @return ResponseInterface
153
     */
154
    public function create(string $sParent, string $sTag, string $sId): ResponseInterface
155
    {
156
        return $this->addCommand('ce', [
157
            'id' => $this->str($sParent),
158
            'tag' => [
159
                'name' => $this->str($sTag),
160
                'id' => $this->str($sId),
161
            ],
162
        ]);
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
     *
168
     * @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...
169
     * @param string $sTag    The tag name to be used for the new element
170
     * @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...
171
     *
172
     * @return ResponseInterface
173
     */
174
    public function insertBefore(string $sBefore, string $sTag, string $sId): ResponseInterface
175
    {
176
        return $this->addCommand('ie', [
177
            'id' => $this->str($sBefore),
178
            'tag' => [
179
                'name' => $this->str($sTag),
180
                'id' => $this->str($sId),
181
            ],
182
        ]);
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 insert a new element just prior to the specified element
187
     * This is an alias for insertBefore.
188
     *
189
     * @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...
190
     * @param string $sTag    The tag name to be used for the new element
191
     * @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...
192
     *
193
     * @return ResponseInterface
194
     */
195
    public function insert(string $sBefore, string $sTag, string $sId): ResponseInterface
196
    {
197
        return $this->insertBefore($sBefore, $sTag, $sId);
198
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
199
200
    /**
201
     * Add a command to insert a new element after the specified
202
     *
203
     * @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...
204
     * @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...
205
     * @param string $sId    The id to assign to the new element
206
     *
207
     * @return ResponseInterface
208
     */
209
    public function insertAfter(string $sAfter, string $sTag, string $sId): ResponseInterface
210
    {
211
        return $this->addCommand('ia', [
212
            'id' => $this->str($sAfter),
213
            'tag' => [
214
                'name' => $this->str($sTag),
215
                'id' => $this->str($sId),
216
            ],
217
        ]);
218
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
219
}
220