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; |
12
|
|
|
|
13
|
|
|
use FeedIo\Feed\NodeInterface; |
14
|
|
|
use FeedIo\Rule\OptionalField; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Turns a FeedInterface instance into a XML document. |
19
|
|
|
* |
20
|
|
|
* Depends on : |
21
|
|
|
* - FeedIo\StandardAbstract |
22
|
|
|
* - Psr\Log\LoggerInterface |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class Formatter |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StandardAbstract |
30
|
|
|
*/ |
31
|
|
|
protected $standard; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param StandardAbstract $standard |
40
|
|
|
* @param LoggerInterface $logger |
41
|
|
|
*/ |
42
|
2 |
|
public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
43
|
|
|
{ |
44
|
2 |
|
$this->standard = $standard; |
45
|
2 |
|
$this->logger = $logger; |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \DOMDocument $document |
50
|
|
|
* @param FeedInterface $feed |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
2 |
|
public function setHeaders(\DOMDocument $document, FeedInterface $feed) |
54
|
|
|
{ |
55
|
2 |
|
$rules = $this->standard->getFeedRuleSet(); |
56
|
2 |
|
$elements = $this->buildElements($rules, $document, $feed); |
57
|
2 |
|
foreach ($elements as $element) { |
58
|
2 |
|
$this->standard->getMainElement($document)->appendChild($element); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
2 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \DOMDocument $document |
66
|
|
|
* @param NodeInterface $node |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
2 |
|
public function addItem(\DOMDocument $document, NodeInterface $node) |
70
|
|
|
{ |
71
|
2 |
|
$domItem = $document->createElement($this->standard->getItemNodeName()); |
72
|
2 |
|
$rules = $this->standard->getItemRuleSet(); |
73
|
2 |
|
$elements = $this->buildElements($rules, $document, $node); |
74
|
|
|
|
75
|
2 |
|
foreach ($elements as $element) { |
76
|
2 |
|
$domItem->appendChild($element); |
77
|
2 |
|
} |
78
|
|
|
|
79
|
2 |
|
$this->standard->getMainElement($document)->appendChild($domItem); |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param RuleSet $ruleSet |
86
|
|
|
* @param \DOMDocument $document |
87
|
|
|
* @param NodeInterface $node |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
2 |
|
public function buildElements(RuleSet $ruleSet, \DOMDocument $document, NodeInterface $node) |
91
|
|
|
{ |
92
|
2 |
|
$rules = $this->getAllRules($ruleSet, $node); |
93
|
2 |
|
$elements = array(); |
94
|
2 |
|
foreach ($rules as $rule) { |
95
|
2 |
|
$elements[] = $rule->createElement($document, $node); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
2 |
|
return array_filter($elements); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param RuleSet $ruleSet |
103
|
|
|
* @param NodeInterface $node |
104
|
|
|
* @return array|\ArrayIterator |
105
|
|
|
*/ |
106
|
2 |
|
public function getAllRules(RuleSet $ruleSet, NodeInterface $node) |
107
|
|
|
{ |
108
|
2 |
|
$rules = $ruleSet->getRules(); |
109
|
2 |
|
$optionalFields = $node->listElements(); |
110
|
2 |
|
foreach ($optionalFields as $optionalField) { |
111
|
2 |
|
$rules[] = new OptionalField($optionalField); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
2 |
|
return $rules; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return \DOMDocument |
119
|
|
|
*/ |
120
|
2 |
|
public function getEmptyDocument() |
121
|
|
|
{ |
122
|
2 |
|
return new \DOMDocument('1.0', 'utf-8'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \DOMDocument |
127
|
|
|
*/ |
128
|
2 |
|
public function getDocument() |
129
|
|
|
{ |
130
|
2 |
|
$document = $this->getEmptyDocument(); |
131
|
|
|
|
132
|
2 |
|
return $this->standard->format($document); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param FeedInterface $feed |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function toString(FeedInterface $feed) |
140
|
|
|
{ |
141
|
|
|
$document = $this->toDom($feed); |
142
|
|
|
|
143
|
|
|
return $document->saveXML(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param FeedInterface $feed |
148
|
|
|
* @return \DomDocument |
149
|
|
|
*/ |
150
|
2 |
|
public function toDom(FeedInterface $feed) |
151
|
|
|
{ |
152
|
2 |
|
$document = $this->getDocument(); |
153
|
|
|
|
154
|
2 |
|
$this->setHeaders($document, $feed); |
155
|
2 |
|
$this->setItems($document, $feed); |
156
|
|
|
|
157
|
2 |
|
return $document; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param \DOMDocument $document |
162
|
|
|
* @param FeedInterface $feed |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
2 |
|
public function setItems(\DOMDocument $document, FeedInterface $feed) |
166
|
|
|
{ |
167
|
2 |
|
foreach ($feed as $item) { |
168
|
2 |
|
$this->addItem($document, $item); |
169
|
2 |
|
} |
170
|
|
|
|
171
|
2 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|