88use DOMElement ;
99use DragonCode \LaravelFeed \Feeds \Items \FeedItem ;
1010use Illuminate \Container \Attributes \Config ;
11+ use Illuminate \Support \Str ;
1112
1213use function htmlspecialchars ;
1314use function is_array ;
15+ use function is_bool ;
1416use function preg_replace ;
1517use function str_replace ;
18+ use function str_starts_with ;
1619
1720class ConvertToXml
1821{
@@ -54,6 +57,7 @@ protected function performItem(DOMElement $parent, array $items): void
5457 $ this ->isAttributes ($ key ) => $ this ->setAttributes ($ parent , $ value ),
5558 $ this ->isCData ($ key ) => $ this ->setCData ($ parent , $ value ),
5659 $ this ->isMixed ($ key ) => $ this ->setMixed ($ parent , $ value ),
60+ $ this ->isArray ($ key ) => $ this ->setItemsArray ($ parent , $ value , $ key ),
5761 default => $ this ->setItems ($ parent , $ key , $ value ),
5862 };
5963 }
@@ -74,6 +78,11 @@ protected function isMixed(string $key): bool
7478 return $ key === '@mixed ' ;
7579 }
7680
81+ protected function isArray (string $ key ): bool
82+ {
83+ return str_starts_with ($ key , '@ ' );
84+ }
85+
7786 protected function createElement (string $ name , bool |float |int |string |null $ value = '' ): DOMElement
7887 {
7988 return $ this ->document ->createElement ($ name , $ this ->convertValue ($ value ));
@@ -101,6 +110,15 @@ protected function setMixed(DOMElement $element, string $value): void
101110 $ element ->appendChild ($ fragment );
102111 }
103112
113+ protected function setItemsArray (DOMElement $ parent , mixed $ value , string $ key ): void
114+ {
115+ $ key = Str::substr ($ key , 1 );
116+
117+ foreach ($ value as $ item ) {
118+ $ this ->setItems ($ parent , $ key , $ item );
119+ }
120+ }
121+
104122 protected function setItems (DOMElement $ parent , string $ key , mixed $ value ): void
105123 {
106124 $ element = $ this ->createElement ($ key , is_array ($ value ) ? '' : $ this ->convertValue ($ value ));
@@ -117,13 +135,17 @@ protected function toXml(DOMElement $item): string
117135 return $ this ->document ->saveXML ($ item );
118136 }
119137
120- protected function convertKey (string $ key ): string
138+ protected function convertKey (int | string $ key ): string
121139 {
122- return str_replace (' ' , '_ ' , $ key );
140+ return str_replace (' ' , '_ ' , ( string ) $ key );
123141 }
124142
125143 protected function convertValue (bool |float |int |string |null $ value ): string
126144 {
145+ if (is_bool ($ value )) {
146+ return $ value ? 'true ' : 'false ' ;
147+ }
148+
127149 return $ this ->removeControlCharacters (
128150 htmlspecialchars ((string ) $ value )
129151 );
0 commit comments