@@ -38,6 +38,8 @@ class Item implements ItemInterface
38
38
/** @var string */
39
39
protected $ author ;
40
40
41
+ protected $ preferCdata = false ;
42
+
41
43
public function title ($ title )
42
44
{
43
45
$ this ->title = $ title ;
@@ -93,6 +95,12 @@ public function author($author)
93
95
return $ this ;
94
96
}
95
97
98
+ public function preferCdata ($ preferCdata )
99
+ {
100
+ $ this ->preferCdata = (bool )$ preferCdata ;
101
+ return $ this ;
102
+ }
103
+
96
104
public function appendTo (ChannelInterface $ channel )
97
105
{
98
106
$ channel ->addItem ($ this );
@@ -102,16 +110,23 @@ public function appendTo(ChannelInterface $channel)
102
110
public function asXML ()
103
111
{
104
112
$ xml = new SimpleXMLElement ('<?xml version="1.0" encoding="UTF-8" ?><item></item> ' , LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL );
105
- $ xml ->addChild ('title ' , $ this ->title );
113
+
114
+ if ($ this ->preferCdata ) {
115
+ $ xml ->addCdataChild ('title ' , $ this ->title );
116
+ } else {
117
+ $ xml ->addChild ('title ' , $ this ->title );
118
+ }
119
+
106
120
$ xml ->addChild ('link ' , $ this ->url );
107
- $ xml ->addChild ('description ' , $ this ->description );
121
+
122
+ if ($ this ->preferCdata ) {
123
+ $ xml ->addCdataChild ('description ' , $ this ->description );
124
+ } else {
125
+ $ xml ->addChild ('description ' , $ this ->description );
126
+ }
108
127
109
128
if ($ this ->contentEncoded ) {
110
- // SimpleXMLElement does not support CDATA transformation
111
- $ element = $ xml ->addChild ('encoded ' , null , 'http://purl.org/rss/1.0/modules/content/ ' );
112
- $ element = dom_import_simplexml ($ element );
113
- $ elementOwner = $ element ->ownerDocument ;
114
- $ element ->appendChild ($ elementOwner ->createCDATASection ($ this ->contentEncoded ));
129
+ $ xml ->addCdataChild ('encoded ' , $ this ->contentEncoded , 'http://purl.org/rss/1.0/modules/content/ ' );
115
130
}
116
131
117
132
foreach ($ this ->categories as $ category ) {
0 commit comments