|
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
|
10 |
|
public function __construct(XmlAbstract $standard) |
|
39
|
|
|
{ |
|
40
|
10 |
|
$this->standard = $standard; |
|
41
|
10 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \DOMDocument $document |
|
45
|
|
|
* @param FeedInterface $feed |
|
46
|
|
|
* @return XmlFormatter |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function setHeaders(\DOMDocument $document, FeedInterface $feed) : XmlFormatter |
|
49
|
|
|
{ |
|
50
|
6 |
|
$rules = $this->standard->getFeedRuleSet(); |
|
51
|
6 |
|
$mainElement = $this->standard->getMainElement($document); |
|
52
|
6 |
|
$this->buildElements($rules, $document, $mainElement, $feed); |
|
53
|
|
|
|
|
54
|
6 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \DOMDocument $document |
|
59
|
|
|
* @param NodeInterface $node |
|
60
|
|
|
* @return XmlFormatter |
|
61
|
|
|
*/ |
|
62
|
5 |
|
public function addItem(\DOMDocument $document, NodeInterface $node) : XmlFormatter |
|
63
|
|
|
{ |
|
64
|
5 |
|
$domItem = $document->createElement($this->standard->getItemNodeName()); |
|
65
|
5 |
|
$rules = $this->standard->getItemRuleSet(); |
|
66
|
5 |
|
$this->buildElements($rules, $document, $domItem, $node); |
|
67
|
|
|
|
|
68
|
5 |
|
$this->standard->getMainElement($document)->appendChild($domItem); |
|
69
|
|
|
|
|
70
|
5 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param RuleSet $ruleSet |
|
75
|
|
|
* @param \DOMDocument $document |
|
76
|
|
|
* @param \DOMElement $rootElement |
|
77
|
|
|
* @param NodeInterface $node |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function buildElements(RuleSet $ruleSet, \DOMDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
80
|
|
|
{ |
|
81
|
7 |
|
$rules = $this->getAllRules($ruleSet, $node); |
|
82
|
|
|
|
|
83
|
7 |
|
foreach ($rules as $rule) { |
|
84
|
7 |
|
$rule->apply($document, $rootElement, $node); |
|
85
|
|
|
} |
|
86
|
7 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param RuleSet $ruleSet |
|
90
|
|
|
* @param NodeInterface $node |
|
91
|
|
|
* @return iterable |
|
92
|
|
|
*/ |
|
93
|
8 |
|
public function getAllRules(RuleSet $ruleSet, NodeInterface $node) : iterable |
|
94
|
|
|
{ |
|
95
|
8 |
|
$rules = $ruleSet->getRules(); |
|
96
|
8 |
|
$optionalFields = $node->listElements(); |
|
97
|
8 |
|
foreach ($optionalFields as $optionalField) { |
|
98
|
3 |
|
$rules[] = new OptionalField($optionalField); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
8 |
|
return $rules; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return \DOMDocument |
|
106
|
|
|
*/ |
|
107
|
9 |
|
public function getEmptyDocument() : \DOMDocument |
|
108
|
|
|
{ |
|
109
|
9 |
|
return new \DOMDocument('1.0', 'utf-8'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return \DOMDocument |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function getDocument() : \DOMDocument |
|
116
|
|
|
{ |
|
117
|
8 |
|
$document = $this->getEmptyDocument(); |
|
118
|
|
|
|
|
119
|
8 |
|
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
|
6 |
|
public function toDom(FeedInterface $feed) : \DOMDocument |
|
138
|
|
|
{ |
|
139
|
6 |
|
$document = $this->getDocument(); |
|
140
|
|
|
|
|
141
|
6 |
|
$this->setHeaders($document, $feed); |
|
142
|
6 |
|
$this->setItems($document, $feed); |
|
143
|
6 |
|
$this->setNS($document, $feed); |
|
144
|
|
|
|
|
145
|
6 |
|
return $document; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
6 |
|
public function setNS(\DOMDocument $document, FeedInterface $feed) |
|
149
|
|
|
{ |
|
150
|
6 |
|
$firstChild = $document->firstChild; |
|
151
|
6 |
|
foreach ($feed->getNS() as $namespace => $dtd) { |
|
152
|
2 |
|
$firstChild->setAttributeNS( |
|
153
|
2 |
|
'http://www.w3.org/2000/xmlns/', // xmlns namespace URI |
|
154
|
2 |
|
'xmlns:'.$namespace, |
|
155
|
2 |
|
$dtd |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
6 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param \DOMDocument $document |
|
162
|
|
|
* @param FeedInterface $feed |
|
163
|
|
|
* @return XmlFormatter |
|
164
|
|
|
*/ |
|
165
|
7 |
|
public function setItems(\DOMDocument $document, FeedInterface $feed) : XmlFormatter |
|
166
|
|
|
{ |
|
167
|
7 |
|
foreach ($feed as $item) { |
|
168
|
5 |
|
$this->addItem($document, $item); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
7 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|