Suin\RSSWriter\Item::asXML PHP Method

asXML() public method

public asXML ( )
    public function asXML()
    {
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item></item>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
        if ($this->preferCdata) {
            $xml->addCdataChild('title', $this->title);
        } else {
            $xml->addChild('title', $this->title);
        }
        $xml->addChild('link', $this->url);
        if ($this->preferCdata) {
            $xml->addCdataChild('description', $this->description);
        } else {
            $xml->addChild('description', $this->description);
        }
        if ($this->contentEncoded) {
            $xml->addCdataChild('xmlns:content:encoded', $this->contentEncoded);
        }
        foreach ($this->categories as $category) {
            $element = $xml->addChild('category', $category[0]);
            if (isset($category[1])) {
                $element->addAttribute('domain', $category[1]);
            }
        }
        if ($this->guid) {
            $guid = $xml->addChild('guid', $this->guid);
            if ($this->isPermalink === false) {
                $guid->addAttribute('isPermaLink', 'false');
            }
        }
        if ($this->pubDate !== null) {
            $xml->addChild('pubDate', date(DATE_RSS, $this->pubDate));
        }
        if (is_array($this->enclosure) && count($this->enclosure) == 3) {
            $element = $xml->addChild('enclosure');
            $element->addAttribute('url', $this->enclosure['url']);
            $element->addAttribute('type', $this->enclosure['type']);
            if ($this->enclosure['length']) {
                $element->addAttribute('length', $this->enclosure['length']);
            }
        }
        if (!empty($this->author)) {
            $xml->addChild('author', $this->author);
        }
        return $xml;
    }

Usage Example

Example #1
0
    public function test_fail_safe_against_invalid_string()
    {
        $item = new Item();
        $item->title("testtest")->url("urltest")->description("descdesc");
        $expect = '<?xml version="1.0" encoding="UTF-8"?>
<item><title>test</title><link>url</link><description>desc</description></item>
';
        $this->assertSame($expect, $item->asXML()->asXML());
    }