RelatedItemsList::asXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages;
4
5
/**
6
 * Class RelatedItemsList
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class RelatedItemsList implements RelatedItemsListInterface
10
{
11
    /** @var RelatedItemInterface[] */
12
    protected $relatedItems = [];
13
14
    /** @var bool */
15
    protected $infinity;
16
17
    /**
18
     * Add channel
19
     * @param bool $infinity Use or not infinity scroll of related items
20
     */
21 28
    public function __construct(bool $infinity = false)
22
    {
23 28
        $this->infinity = $infinity;
24 28
    }
25
26 4
    public function appendTo(ItemInterface $item): RelatedItemsListInterface
27
    {
28 4
        $item->addRelatedItemsList($this);
29 4
        return $this;
30
    }
31
32 12
    public function addItem(RelatedItem $relatedItem): RelatedItemsListInterface
33
    {
34 12
        $this->relatedItems[] = $relatedItem;
35 12
        return $this;
36
    }
37
38 16
    public function asXML(): SimpleXMLElement
39
    {
40 16
        $infinity = $this->infinity ? 'type="infinity"' : '';
41
42 16
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:related '
43 16
            . $infinity . '></yandex:related>',
44 16
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
45
46 16
        foreach ($this->relatedItems as $item) {
47 4
            $toDom = dom_import_simplexml($xml);
48 4
            $fromDom = dom_import_simplexml($item->asXML());
49 4
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
50
        }
51
52 16
        return $xml;
53
    }
54
}
55