|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DomTreeTrait.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
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Jaxon\Response\Traits; |
|
12
|
|
|
|
|
13
|
|
|
use Jaxon\Response\Response; |
|
14
|
|
|
|
|
15
|
|
|
trait DomTreeTrait |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
21
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
22
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
23
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
24
|
|
|
* |
|
25
|
|
|
* @return Response |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add a command to start a DOM response |
|
31
|
|
|
* |
|
32
|
|
|
* @return Response |
|
33
|
|
|
*/ |
|
34
|
|
|
public function domStartResponse(): Response |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->_addCommand('DSR', [], ''); |
|
37
|
|
|
} |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add a command to create a DOM element |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $sVariable The DOM element name (id or class) |
|
|
|
|
|
|
43
|
|
|
* @param string $sTag The HTML tag of the new DOM element |
|
|
|
|
|
|
44
|
|
|
* |
|
45
|
|
|
* @return Response |
|
46
|
|
|
*/ |
|
47
|
|
|
public function domCreateElement(string $sVariable, string $sTag): Response |
|
48
|
|
|
{ |
|
49
|
|
|
$aAttributes = ['tgt' => $sVariable]; |
|
50
|
|
|
return $this->_addCommand('DCE', $aAttributes, $sTag); |
|
51
|
|
|
} |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add a command to set an attribute on a DOM element |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $sVariable The DOM element name (id or class) |
|
|
|
|
|
|
57
|
|
|
* @param string $sKey The name of the attribute |
|
|
|
|
|
|
58
|
|
|
* @param string $sValue The value of the attribute |
|
59
|
|
|
* |
|
60
|
|
|
* @return Response |
|
61
|
|
|
*/ |
|
62
|
|
|
public function domSetAttribute(string $sVariable, string $sKey, string $sValue): Response |
|
63
|
|
|
{ |
|
64
|
|
|
$aAttributes = ['tgt' => $sVariable, 'key' => $sKey]; |
|
65
|
|
|
return $this->_addCommand('DSA', $aAttributes, $sValue); |
|
66
|
|
|
} |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add a command to remove children from a DOM element |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $sParent The DOM parent element |
|
|
|
|
|
|
72
|
|
|
* @param int $nSkip The number of children to skip |
|
|
|
|
|
|
73
|
|
|
* @param int $nRemove The number of children to remove |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return Response |
|
76
|
|
|
*/ |
|
77
|
|
|
public function domRemoveChildren(string $sParent, int $nSkip = 0, int $nRemove = 0): Response |
|
78
|
|
|
{ |
|
79
|
|
|
$aAttributes = ['skip' => $nSkip, 'remove' => $nRemove]; |
|
80
|
|
|
return $this->_addCommand('DRC', $aAttributes, $sParent, true); |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Add a command to append a child to a DOM element |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $sParent The DOM parent element |
|
|
|
|
|
|
87
|
|
|
* @param string $sVariable The DOM element name (id or class) |
|
|
|
|
|
|
88
|
|
|
* |
|
89
|
|
|
* @return Response |
|
90
|
|
|
*/ |
|
91
|
|
|
public function domAppendChild(string $sParent, string $sVariable): Response |
|
92
|
|
|
{ |
|
93
|
|
|
$aAttributes = ['par' => $sParent]; |
|
94
|
|
|
return $this->_addCommand('DAC', $aAttributes, $sVariable); |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add a command to insert a DOM element before another |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $sTarget The DOM target element |
|
|
|
|
|
|
101
|
|
|
* @param string $sVariable The DOM element name (id or class) |
|
|
|
|
|
|
102
|
|
|
* |
|
103
|
|
|
* @return Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public function domInsertBefore(string $sTarget, string $sVariable): Response |
|
106
|
|
|
{ |
|
107
|
|
|
$aAttributes = ['tgt' => $sTarget]; |
|
108
|
|
|
return $this->_addCommand('DIB', $aAttributes, $sVariable); |
|
109
|
|
|
} |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Add a command to insert a DOM element after another |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $sTarget The DOM target element |
|
|
|
|
|
|
115
|
|
|
* @param string $sVariable The DOM element name (id or class) |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return Response |
|
118
|
|
|
*/ |
|
119
|
|
|
public function domInsertAfter(string $sTarget, string $sVariable): Response |
|
120
|
|
|
{ |
|
121
|
|
|
$aAttributes = ['tgt' => $sTarget]; |
|
122
|
|
|
return $this->_addCommand('DIA', $aAttributes, $sVariable); |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Add a command to append a text to a DOM element |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $sParent The DOM parent element |
|
|
|
|
|
|
129
|
|
|
* @param string $sText The HTML text to append |
|
|
|
|
|
|
130
|
|
|
* |
|
131
|
|
|
* @return Response |
|
132
|
|
|
*/ |
|
133
|
|
|
public function domAppendText(string $sParent, string $sText): Response |
|
134
|
|
|
{ |
|
135
|
|
|
$aAttributes = ['par' => $sParent]; |
|
136
|
|
|
return $this->_addCommand('DAT', $aAttributes, $sText); |
|
137
|
|
|
} |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Add a command to end a DOM response |
|
141
|
|
|
* |
|
142
|
|
|
* @return Response |
|
143
|
|
|
*/ |
|
144
|
|
|
public function domEndResponse(): Response |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->_addCommand('DER', [], ''); |
|
147
|
|
|
} |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|