Completed
Push — master ( a775f9...497487 )
by Alex
07:59
created

XmlFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 142
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setHeaders() 0 10 2
A addItem() 0 14 2
A buildElements() 0 10 2
A getAllRules() 0 10 2
A getEmptyDocument() 0 4 1
A getDocument() 0 6 1
A toString() 0 6 1
A toDom() 0 9 1
A setItems() 0 8 2
1
<?php
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
    public function __construct(XmlAbstract $standard)
39
    {
40
        $this->standard = $standard;
41
    }
42
43
    /**
44
     * @param  \DOMDocument  $document
45
     * @param  FeedInterface $feed
46
     * @return $this
47
     */
48
    public function setHeaders(\DOMDocument $document, FeedInterface $feed)
49
    {
50
        $rules = $this->standard->getFeedRuleSet();
51
        $elements = $this->buildElements($rules, $document, $feed);
52
        foreach ($elements as $element) {
53
            $this->standard->getMainElement($document)->appendChild($element);
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param  \DOMDocument  $document
61
     * @param  NodeInterface $node
62
     * @return $this
63
     */
64
    public function addItem(\DOMDocument $document, NodeInterface $node)
65
    {
66
        $domItem = $document->createElement($this->standard->getItemNodeName());
67
        $rules = $this->standard->getItemRuleSet();
68
        $elements = $this->buildElements($rules, $document, $node);
69
70
        foreach ($elements as $element) {
71
            $domItem->appendChild($element);
72
        }
73
74
        $this->standard->getMainElement($document)->appendChild($domItem);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param  RuleSet       $ruleSet
81
     * @param  \DOMDocument  $document
82
     * @param  NodeInterface $node
83
     * @return array
84
     */
85
    public function buildElements(RuleSet $ruleSet, \DOMDocument $document, NodeInterface $node)
86
    {
87
        $rules = $this->getAllRules($ruleSet, $node);
88
        $elements = array();
89
        foreach ($rules as $rule) {
90
            $elements[] = $rule->createElement($document, $node);
91
        }
92
93
        return array_filter($elements);
94
    }
95
96
    /**
97
     * @param  RuleSet              $ruleSet
98
     * @param  NodeInterface        $node
99
     * @return array|\ArrayIterator
100
     */
101
    public function getAllRules(RuleSet $ruleSet, NodeInterface $node)
102
    {
103
        $rules = $ruleSet->getRules();
104
        $optionalFields = $node->listElements();
105
        foreach ($optionalFields as $optionalField) {
106
            $rules[] = new OptionalField($optionalField);
107
        }
108
109
        return $rules;
110
    }
111
112
    /**
113
     * @return \DOMDocument
114
     */
115
    public function getEmptyDocument()
116
    {
117
        return new \DOMDocument('1.0', 'utf-8');
118
    }
119
120
    /**
121
     * @return \DOMDocument
122
     */
123
    public function getDocument()
124
    {
125
        $document = $this->getEmptyDocument();
126
127
        return $this->standard->format($document);
128
    }
129
130
    /**
131
     * @param  FeedInterface $feed
132
     * @return string
133
     */
134
    public function toString(FeedInterface $feed)
135
    {
136
        $document = $this->toDom($feed);
137
138
        return $document->saveXML();
139
    }
140
141
    /**
142
     * @param  FeedInterface $feed
143
     * @return \DomDocument
144
     */
145
    public function toDom(FeedInterface $feed)
146
    {
147
        $document = $this->getDocument();
148
149
        $this->setHeaders($document, $feed);
150
        $this->setItems($document, $feed);
151
152
        return $document;
153
    }
154
155
    /**
156
     * @param  \DOMDocument  $document
157
     * @param  FeedInterface $feed
158
     * @return $this
159
     */
160
    public function setItems(\DOMDocument $document, FeedInterface $feed)
161
    {
162
        foreach ($feed as $item) {
163
            $this->addItem($document, $item);
164
        }
165
166
        return $this;
167
    }
168
}
169