@@ -17,21 +17,39 @@ class CompoundTag extends Tag implements Iterator, ArrayAccess, Countable
17
17
* @var Tag[]
18
18
*/
19
19
protected array $ valueArray = [];
20
+ protected ?string $ rawContent = null ;
21
+ protected ?int $ rawContentFormat = null ;
22
+
23
+ /**
24
+ * @return bool
25
+ */
26
+ public function isRaw (): bool
27
+ {
28
+ return $ this ->rawContent !== null ;
29
+ }
20
30
21
31
/**
22
32
* @inheritDoc
23
33
* @throws Exception
24
34
*/
25
35
public function writeContent (Writer $ writer ): static
26
36
{
37
+ if ($ this ->isRaw ()) {
38
+ if ($ this ->rawContentFormat !== $ writer ->getFormat ()) {
39
+ throw new Exception ("Cannot change format of raw compound tag " );
40
+ }
41
+ $ writer ->write ($ this ->rawContent );
42
+ return $ this ;
43
+ }
44
+
27
45
$ writtenNames = [];
28
46
foreach ($ this ->valueArray as $ value ) {
29
47
if (in_array ($ value ->getName (), $ writtenNames )) {
30
48
throw new Exception ("Duplicate key ' " . $ value ->getName () . "' in compound tag " );
31
49
}
32
50
$ value ->writeData ($ writer , true );
33
51
}
34
- (new EndTag ())->writeData ($ writer );
52
+ (new EndTag ($ this -> options ))->writeData ($ writer );
35
53
return $ this ;
36
54
}
37
55
@@ -41,12 +59,31 @@ public function writeContent(Writer $writer): static
41
59
*/
42
60
protected function readContent (Reader $ reader ): static
43
61
{
44
- while (!(($ tag = Tag::load ($ reader )) instanceof EndTag)) {
45
- $ this ->valueArray [] = $ tag ;
62
+ if ($ this ->options ->shouldBeReadRaw ($ this )) {
63
+ $ this ->rawContentFormat = $ reader ->getFormat ();
64
+ $ this ->rawContent = static ::readContentRaw ($ reader , $ this ->options );
65
+ return $ this ;
66
+ }
67
+ while (!(($ tag = Tag::load ($ reader , $ this ->options , $ this )) instanceof EndTag)) {
68
+ $ this ->valueArray [] = $ tag ->setParentTag ($ this );
46
69
}
47
70
return $ this ;
48
71
}
49
72
73
+ /**
74
+ * @inheritDoc
75
+ * @throws Exception
76
+ */
77
+ protected static function readContentRaw (Reader $ reader , TagOptions $ options ): string
78
+ {
79
+ $ result = "" ;
80
+ do {
81
+ $ tag = Tag::loadRaw ($ reader , $ options );
82
+ $ result .= $ tag ->getData ();
83
+ } while ($ tag ->getTagType () !== TagType::TAG_End);
84
+ return $ result ;
85
+ }
86
+
50
87
/**
51
88
* @param Tag $value
52
89
* @inheritDoc
@@ -60,6 +97,9 @@ public function offsetSet($offset, $value): void
60
97
if (!is_string ($ offset ) && !is_null ($ offset )) {
61
98
throw new Exception ("Invalid CompoundTag key " );
62
99
}
100
+ if ($ this ->isRaw ()) {
101
+ throw new Exception ("Raw compound tags cannot be modified " );
102
+ }
63
103
if (is_null ($ offset ) && is_null ($ value ->getName ())) {
64
104
throw new Exception ("Tags inside a CompoundTag must be named. " );
65
105
}
@@ -68,6 +108,7 @@ public function offsetSet($offset, $value): void
68
108
} else {
69
109
$ offset = $ value ->getName ();
70
110
}
111
+ $ value ->setParentTag ($ this );
71
112
$ this ->offsetUnset ($ offset );
72
113
$ this ->valueArray [] = $ value ;
73
114
}
@@ -140,11 +181,16 @@ public function offsetGet($offset)
140
181
141
182
/**
142
183
* @inheritDoc
184
+ * @throws Exception
143
185
*/
144
186
public function offsetUnset ($ offset )
145
187
{
188
+ if ($ this ->isRaw ()) {
189
+ throw new Exception ("Raw compound tags cannot be modified " );
190
+ }
146
191
foreach ($ this ->valueArray as $ i => $ val ) {
147
192
if ($ val ->getName () === $ offset ) {
193
+ $ val ->setParentTag (null );
148
194
unset($ this ->valueArray [$ i ]);
149
195
break ;
150
196
}
@@ -164,6 +210,9 @@ public function count(): int
164
210
*/
165
211
protected function getValueString (): string
166
212
{
213
+ if ($ this ->isRaw ()) {
214
+ return strlen ($ this ->rawContent ) . " bytes " ;
215
+ }
167
216
return $ this ->count () . " entr " . ($ this ->count () === 1 ? "y " : "ies " ) . "\n{ \n" .
168
217
$ this ->indent (implode (", \n" , array_map ("strval " , array_values ($ this ->valueArray )))) .
169
218
"\n} " ;
@@ -199,6 +248,7 @@ public function set(?string $name, Tag $tag): CompoundTag
199
248
/**
200
249
* @param string $name
201
250
* @return $this
251
+ * @throws Exception
202
252
*/
203
253
public function delete (string $ name ): CompoundTag
204
254
{
0 commit comments