1 | <?php |
||
28 | class Parser |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var \Psr\Log\LoggerInterface |
||
33 | */ |
||
34 | protected $logger; |
||
35 | |||
36 | /** |
||
37 | * @var array[FilterInterface] |
||
38 | */ |
||
39 | protected $filters = array(); |
||
40 | |||
41 | /** |
||
42 | * @var StandardAbstract |
||
43 | */ |
||
44 | protected $standard; |
||
45 | |||
46 | /** |
||
47 | * @param StandardAbstract $standard |
||
48 | * @param LoggerInterface $logger |
||
49 | */ |
||
50 | 34 | public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
|
55 | |||
56 | /** |
||
57 | * @return StandardAbstract |
||
58 | */ |
||
59 | 21 | public function getStandard() |
|
63 | |||
64 | /** |
||
65 | * @param $tagName |
||
66 | * @return bool |
||
67 | */ |
||
68 | 7 | public function isItem($tagName) |
|
72 | |||
73 | /** |
||
74 | * @param FilterInterface $filter |
||
75 | * @return $this |
||
76 | */ |
||
77 | 10 | public function addFilter(FilterInterface $filter) |
|
83 | |||
84 | /** |
||
85 | * @return $this |
||
86 | */ |
||
87 | 8 | public function initFilters(FeedInterface $feed) |
|
88 | { |
||
89 | 8 | foreach ($this->filters as $filter) { |
|
90 | 1 | $filter->init($feed); |
|
91 | } |
||
92 | |||
93 | 8 | return $this; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param DOMDocument $document |
||
98 | * @param FeedInterface $feed |
||
99 | * @return \FeedIo\FeedInterface |
||
100 | * @throws Parser\MissingFieldsException |
||
101 | * @throws Parser\UnsupportedFormatException |
||
102 | */ |
||
103 | 8 | public function parse(DOMDocument $document, FeedInterface $feed) |
|
104 | { |
||
105 | 8 | if (!$this->standard->canHandle($document)) { |
|
106 | 1 | throw new UnsupportedFormatException('this is not a supported format'); |
|
107 | } |
||
108 | |||
109 | 7 | $this->initFilters($feed); |
|
110 | 7 | $this->checkBodyStructure($document, $this->standard->getMandatoryFields()); |
|
111 | 7 | $element = $this->standard->getMainElement($document); |
|
112 | |||
113 | 7 | $this->parseNode($feed, $element, $this->standard->getFeedRuleSet()); |
|
114 | |||
115 | 7 | return $feed; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param DOMDocument $document |
||
120 | * @param array $mandatoryFields |
||
121 | * @return $this |
||
122 | * @throws MissingFieldsException |
||
123 | */ |
||
124 | 9 | public function checkBodyStructure(DOMDocument $document, array $mandatoryFields) |
|
144 | |||
145 | /** |
||
146 | * @param NodeInterface $item |
||
147 | * @param \DOMElement $element |
||
148 | * @param RuleSet $ruleSet |
||
149 | * @return NodeInterface |
||
150 | */ |
||
151 | 8 | public function parseNode(NodeInterface $item, \DOMElement $element, RuleSet $ruleSet) |
|
161 | |||
162 | /** |
||
163 | * @param NodeInterface $item |
||
164 | * @param \DOMElement $node |
||
165 | * @param RuleSet $ruleSet |
||
166 | * @return $this |
||
167 | */ |
||
168 | 7 | protected function handleNode(NodeInterface $item, \DOMElement $node, RuleSet $ruleSet) |
|
180 | |||
181 | /** |
||
182 | * @param FeedInterface $feed |
||
183 | * @param NodeInterface $item |
||
184 | * @return $this |
||
185 | */ |
||
186 | 6 | public function addValidItem(FeedInterface $feed, NodeInterface $item) |
|
194 | |||
195 | /** |
||
196 | * @param ItemInterface $item |
||
197 | * @return bool |
||
198 | */ |
||
199 | 8 | public function isValid(ItemInterface $item) |
|
209 | } |
||
210 |