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 |
|
$element = $rule->createElement($document, $node); |
91
|
5 |
|
if ($element instanceof \Traversable) { |
92
|
2 |
|
$elements = array_merge($elements, iterator_to_array($element)); |
93
|
2 |
|
} else { |
94
|
5 |
|
$elements[] = $element; |
95
|
|
|
} |
96
|
5 |
|
} |
97
|
|
|
|
98
|
5 |
|
return array_filter($elements); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param RuleSet $ruleSet |
103
|
|
|
* @param NodeInterface $node |
104
|
|
|
* @return array|\ArrayIterator |
105
|
|
|
*/ |
106
|
6 |
|
public function getAllRules(RuleSet $ruleSet, NodeInterface $node) |
107
|
|
|
{ |
108
|
6 |
|
$rules = $ruleSet->getRules(); |
109
|
6 |
|
$optionalFields = $node->listElements(); |
110
|
6 |
|
foreach ($optionalFields as $optionalField) { |
111
|
3 |
|
$rules[] = new OptionalField($optionalField); |
112
|
6 |
|
} |
113
|
|
|
|
114
|
6 |
|
return $rules; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \DOMDocument |
119
|
|
|
*/ |
120
|
7 |
|
public function getEmptyDocument() |
121
|
|
|
{ |
122
|
7 |
|
return new \DOMDocument('1.0', 'utf-8'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \DOMDocument |
127
|
|
|
*/ |
128
|
6 |
|
public function getDocument() |
129
|
|
|
{ |
130
|
6 |
|
$document = $this->getEmptyDocument(); |
131
|
|
|
|
132
|
6 |
|
return $this->standard->format($document); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param FeedInterface $feed |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
1 |
|
public function toString(FeedInterface $feed) |
140
|
|
|
{ |
141
|
1 |
|
$document = $this->toDom($feed); |
142
|
|
|
|
143
|
1 |
|
return $document->saveXML(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param FeedInterface $feed |
148
|
|
|
* @return \DomDocument |
149
|
|
|
*/ |
150
|
4 |
|
public function toDom(FeedInterface $feed) |
151
|
|
|
{ |
152
|
4 |
|
$document = $this->getDocument(); |
153
|
|
|
|
154
|
4 |
|
$this->setHeaders($document, $feed); |
155
|
4 |
|
$this->setItems($document, $feed); |
156
|
|
|
|
157
|
4 |
|
return $document; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param \DOMDocument $document |
162
|
|
|
* @param FeedInterface $feed |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
5 |
|
public function setItems(\DOMDocument $document, FeedInterface $feed) |
166
|
|
|
{ |
167
|
5 |
|
foreach ($feed as $item) { |
168
|
3 |
|
$this->addItem($document, $item); |
169
|
5 |
|
} |
170
|
|
|
|
171
|
5 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|