|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Response\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
6
|
|
|
use Jaxon\Response\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
use function array_filter; |
|
9
|
|
|
use function array_map; |
|
10
|
|
|
use function array_merge; |
|
11
|
|
|
use function count; |
|
12
|
|
|
use function is_array; |
|
13
|
|
|
use function is_integer; |
|
14
|
|
|
use function trim; |
|
15
|
|
|
|
|
16
|
|
|
trait CommandTrait |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The commands that will be sent to the browser in the response |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $aCommands = []; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get the commands in the response |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getCommands(): array |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
return $this->aCommands; |
|
33
|
|
|
} |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the number of commands in the response |
|
37
|
|
|
* |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getCommandCount(): int |
|
41
|
|
|
{ |
|
42
|
|
|
return count($this->aCommands); |
|
43
|
|
|
} |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Clear all the commands already added to the response |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function clearCommands() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->aCommands = []; |
|
53
|
|
|
} |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Merge the commands with those in this <Response> object |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $aCommands The commands to merge |
|
|
|
|
|
|
59
|
|
|
* @param bool $bBefore Add the new commands to the beginning of the list |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function appendCommands(array $aCommands, bool $bBefore = false) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->aCommands = ($bBefore) ? |
|
|
|
|
|
|
66
|
|
|
array_merge($aCommands, $this->aCommands) : |
|
|
|
|
|
|
67
|
|
|
array_merge($this->aCommands, $aCommands); |
|
68
|
|
|
} |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Merge the response commands with those in this <Response> object |
|
72
|
|
|
* |
|
73
|
|
|
* @param ResponseInterface $xResponse The <Response> object |
|
|
|
|
|
|
74
|
|
|
* @param bool $bBefore Add the new commands to the beginning of the list |
|
|
|
|
|
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function appendResponse(ResponseInterface $xResponse, bool $bBefore = false) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->appendCommands($xResponse->getCommands(), $bBefore); |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
87
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
88
|
|
|
* |
|
89
|
|
|
* @return ResponseInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
public function addRawCommand(array $aAttributes, $mData): ResponseInterface |
|
92
|
|
|
{ |
|
93
|
|
|
$aAttributes['data'] = $mData; |
|
94
|
|
|
$this->aCommands[] = $aAttributes; |
|
|
|
|
|
|
95
|
|
|
return $this; |
|
|
|
|
|
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
100
|
|
|
* Convert all attributes, excepted integers, to string. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
103
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
104
|
|
|
* |
|
105
|
|
|
* @return ResponseInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
public function addCommand(array $aAttributes, $mData): ResponseInterface |
|
108
|
|
|
{ |
|
109
|
|
|
$aAttributes = array_map(function($xAttribute) { |
|
110
|
|
|
return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t"); |
|
111
|
|
|
}, $aAttributes); |
|
112
|
|
|
return $this->addRawCommand($aAttributes, $mData); |
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
119
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
120
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
121
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
122
|
|
|
* |
|
123
|
|
|
* @return ResponseInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function _addCommand(string $sName, array $aAttributes, |
|
126
|
|
|
$mData, bool $bRemoveEmpty = false): ResponseInterface |
|
127
|
|
|
{ |
|
128
|
|
|
$mData = is_array($mData) ? array_map(function($sData) { |
|
129
|
|
|
return trim((string)$sData, " \t\n"); |
|
130
|
|
|
}, $mData) : trim((string)$mData, " \t\n"); |
|
131
|
|
|
if($bRemoveEmpty) |
|
132
|
|
|
{ |
|
133
|
|
|
$aAttributes = array_filter($aAttributes, function($xValue) { |
|
134
|
|
|
return $xValue === ''; |
|
135
|
|
|
}); |
|
136
|
|
|
} |
|
137
|
|
|
$aAttributes['cmd'] = $sName; |
|
138
|
|
|
return $this->addCommand($aAttributes, $mData); |
|
139
|
|
|
} |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Add a response command that is generated by a plugin |
|
143
|
|
|
* |
|
144
|
|
|
* @param ResponsePlugin $xPlugin The plugin object |
|
|
|
|
|
|
145
|
|
|
* @param array $aAttributes The attributes for this response command |
|
|
|
|
|
|
146
|
|
|
* @param mixed $mData The data to be sent with this command |
|
|
|
|
|
|
147
|
|
|
* |
|
148
|
|
|
* @return ResponseInterface |
|
149
|
|
|
*/ |
|
150
|
|
|
public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): ResponseInterface |
|
151
|
|
|
{ |
|
152
|
|
|
$aAttributes['plg'] = $xPlugin->getName(); |
|
153
|
|
|
return $this->addCommand($aAttributes, $mData); |
|
154
|
|
|
} |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|