Completed
Pull Request — master (#97)
by Alex
05:49 queued 03:13
created

XmlFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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