Completed
Pull Request — master (#230)
by
unknown
02:43 queued 01:26
created

XmlFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Formatter;
12
13
use FeedIo\Feed\NodeInterface;
14
use FeedIo\FeedInterface;
15
use FeedIo\Rule\OptionalField;
16
use FeedIo\RuleSet;
17
use FeedIo\Standard\XmlAbstract;
18
use FeedIo\FormatterInterface;
19
20
/**
21
 * Turns a FeedInterface instance into a XML document.
22
 *
23
 * Depends on :
24
 *  - FeedIo\StandardAbstract
25
 *
26
 */
27
class XmlFormatter implements FormatterInterface
28
{
29
30
    /**
31
     * @var XmlAbstract
32
     */
33
    protected $standard;
34
35
    /**
36
     * @param XmlAbstract $standard
37
     */
38 8
    public function __construct(XmlAbstract $standard)
39
    {
40 8
        $this->standard = $standard;
41 8
    }
42
43
    /**
44
     * @param  \DOMDocument  $document
45
     * @param  FeedInterface $feed
46
     * @return XmlFormatter
47
     */
48 4
    public function setHeaders(\DOMDocument $document, FeedInterface $feed) : XmlFormatter
49
    {
50 4
        $rules = $this->standard->getFeedRuleSet();
51 4
        $mainElement = $this->standard->getMainElement($document);
52 4
        $this->buildElements($rules, $document, $mainElement, $feed);
53
54 4
        return $this;
55
    }
56
57
    /**
58
     * @param  \DOMDocument  $document
59
     * @param  NodeInterface $node
60
     * @return XmlFormatter
61
     */
62 3
    public function addItem(\DOMDocument $document, NodeInterface $node) : XmlFormatter
63
    {
64 3
        $domItem = $document->createElement($this->standard->getItemNodeName());
65 3
        $rules = $this->standard->getItemRuleSet();
66 3
        $this->buildElements($rules, $document, $domItem, $node);
67
68 3
        $this->standard->getMainElement($document)->appendChild($domItem);
69
70 3
        return $this;
71
    }
72
73
    /**
74
     * @param RuleSet $ruleSet
75
     * @param \DOMDocument $document
76
     * @param \DOMElement $rootElement
77
     * @param NodeInterface $node
78
     */
79 5
    public function buildElements(RuleSet $ruleSet, \DOMDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
80
    {
81 5
        $rules = $this->getAllRules($ruleSet, $node);
82
83 5
        foreach ($rules as $rule) {
84 5
            $rule->apply($document, $rootElement, $node);
85
        }
86 5
    }
87
88
    /**
89
     * @param  RuleSet              $ruleSet
90
     * @param  NodeInterface        $node
91
     * @return iterable
92
     */
93 6
    public function getAllRules(RuleSet $ruleSet, NodeInterface $node) : iterable
94
    {
95 6
        $rules = $ruleSet->getRules();
96 6
        $optionalFields = $node->listElements();
97 6
        foreach ($optionalFields as $optionalField) {
98 1
            $rules[] = new OptionalField($optionalField);
99
        }
100
101 6
        return $rules;
102
    }
103
104
    /**
105
     * @return \DOMDocument
106
     */
107 7
    public function getEmptyDocument() : \DOMDocument
108
    {
109 7
        return new \DOMDocument('1.0', 'utf-8');
110
    }
111
112
    /**
113
     * @return \DOMDocument
114
     */
115 6
    public function getDocument() : \DOMDocument
116
    {
117 6
        $document = $this->getEmptyDocument();
118
119 6
        return $this->standard->format($document);
120
    }
121
122
    /**
123
     * @param  FeedInterface $feed
124
     * @return string
125
     */
126 3
    public function toString(FeedInterface $feed) : string
127
    {
128 3
        $document = $this->toDom($feed);
129
130 3
        return $document->saveXML();
131
    }
132
133
    /**
134
     * @param  FeedInterface $feed
135
     * @return \DomDocument
136
     */
137 4
    public function toDom(FeedInterface $feed) : \DOMDocument
138
    {
139 4
        $document = $this->getDocument();
140
141 4
        $this->setHeaders($document, $feed);
142 4
        $this->setItems($document, $feed);
143 4
        $this->setNS($document, $feed);
144
145 4
        return $document;
146
    }
147
148 4
    public function setNS(\DOMDocument $document, FeedInterface $feed)
149
    {
150 4
        $firstChild = $document->firstChild;
151 4
        foreach ($feed->getNS() as $namespace => $dtd) {
152
            $firstChild->setAttributeNS(
153
                'http://www.w3.org/2000/xmlns/', // xmlns namespace URI
154
                'xmlns:'.$namespace,
155
                $dtd
156
            );
157
        }
158 4
    }
159
160
    /**
161
     * @param  \DOMDocument  $document
162
     * @param  FeedInterface $feed
163
     * @return XmlFormatter
164
     */
165 5
    public function setItems(\DOMDocument $document, FeedInterface $feed) : XmlFormatter
166
    {
167 5
        foreach ($feed as $item) {
168 3
            $this->addItem($document, $item);
169
        }
170
171 5
        return $this;
172
    }
173
}
174