Feed::addChannel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages;
4
5
use DOMDocument;
6
7
/**
8
 * Class Feed
9
 * @package sokolnikov911\YandexTurboPages
10
 */
11
class Feed implements FeedInterface
12
{
13
    const ENCODING_UTF_8 = 'UTF-8';
14
    const ENCODING_WINDOWS_1251 = 'windows-1251';
15
    const ENCODING_KOI8_R = 'KOI8-R';
16
17
    /** @var ChannelInterface[] */
18
    protected $channels = [];
19
20
    private $encoding;
21
22 49
    public function __construct($encoding = self::ENCODING_UTF_8)
23
    {
24 49
        $this->encoding = $encoding;
25 49
    }
26
27 21
    public function addChannel(ChannelInterface $channel)
28
    {
29 21
        $this->channels[] = $channel;
30 21
        return $this;
31
    }
32
33 21
    public function render()
34
    {
35 21
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="' . $this->encoding
36 21
            . '" ?><rss version="2.0" xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" />',
37 21
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
38
39 21
        foreach ($this->channels as $channel) {
40 7
            $toDom = dom_import_simplexml($xml);
41 7
            $fromDom = dom_import_simplexml($channel->asXML());
42 7
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
43 9
        }
44
45 21
        $dom = new DOMDocument('1.0', 'UTF-8');
46 21
        $dom->appendChild($dom->importNode(dom_import_simplexml($xml), true));
47 21
        $dom->formatOutput = true;
48 21
        return $dom->saveXML();
49
    }
50
51 14
    public function __toString()
52
    {
53 14
        return $this->render();
54
    }
55
}
56