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 | 26 | public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
|
51 | { |
||
52 | 26 | $this->standard = $standard; |
|
53 | 26 | $this->logger = $logger; |
|
54 | 26 | } |
|
55 | |||
56 | /** |
||
57 | * @return StandardAbstract |
||
58 | */ |
||
59 | 21 | public function getStandard() |
|
60 | { |
||
61 | 21 | return $this->standard; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $tagName |
||
66 | * @return bool |
||
67 | */ |
||
68 | 6 | public function isItem($tagName) |
|
69 | { |
||
70 | 6 | return (strtolower($this->standard->getItemNodeName()) === strtolower($tagName)); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param FilterInterface $filter |
||
75 | * @return $this |
||
76 | */ |
||
77 | 7 | public function addFilter(FilterInterface $filter) |
|
78 | { |
||
79 | 7 | $this->filters[] = $filter; |
|
80 | |||
81 | 7 | return $this; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return $this |
||
86 | */ |
||
87 | 6 | public function initFilters(FeedInterface $feed) |
|
88 | { |
||
89 | 6 | foreach ($this->filters as $filter) { |
|
90 | $filter->init($feed); |
||
91 | 6 | } |
|
92 | |||
93 | 6 | 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 | 6 | public function parse(DOMDocument $document, FeedInterface $feed) |
|
104 | { |
||
105 | 6 | if (!$this->standard->canHandle($document)) { |
|
106 | throw new UnsupportedFormatException('this is not a supported format'); |
||
107 | } |
||
108 | |||
109 | 6 | $this->initFilters($feed); |
|
110 | 6 | $this->checkBodyStructure($document, $this->standard->getMandatoryFields()); |
|
111 | 6 | $element = $this->standard->getMainElement($document); |
|
112 | |||
113 | 6 | $this->parseNode($feed, $element, $this->standard->getFeedRuleSet()); |
|
114 | |||
115 | 6 | return $feed; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param DOMDocument $document |
||
120 | * @param array $mandatoryFields |
||
121 | * @return $this |
||
122 | * @throws MissingFieldsException |
||
123 | */ |
||
124 | 6 | public function checkBodyStructure(DOMDocument $document, array $mandatoryFields) |
|
125 | { |
||
126 | 6 | $errors = array(); |
|
127 | |||
128 | 6 | $element = $document->documentElement; |
|
129 | 6 | foreach ($mandatoryFields as $field) { |
|
130 | $list = $element->getElementsByTagName($field); |
||
131 | if (0 === $list->length) { |
||
132 | $errors[] = $field; |
||
133 | } |
||
134 | 6 | } |
|
135 | |||
136 | 6 | if (!empty($errors)) { |
|
137 | $message = "missing mandatory field(s) : ".implode(',', $errors); |
||
138 | $this->logger->warning($message); |
||
139 | throw new MissingFieldsException($message); |
||
140 | } |
||
141 | |||
142 | 6 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param NodeInterface $item |
||
147 | * @param \DOMElement $element |
||
148 | * @param RuleSet $ruleSet |
||
149 | * @return NodeInterface |
||
150 | */ |
||
151 | 6 | public function parseNode(NodeInterface $item, \DOMElement $element, RuleSet $ruleSet) |
|
167 | |||
168 | /** |
||
169 | * @param FeedInterface $feed |
||
170 | * @param NodeInterface $item |
||
171 | * @return $this |
||
172 | */ |
||
173 | 6 | public function addValidItem(FeedInterface $feed, NodeInterface $item) |
|
181 | |||
182 | /** |
||
183 | * @param ItemInterface $item |
||
184 | * @return bool |
||
185 | */ |
||
186 | 6 | public function isValid(ItemInterface $item) |
|
187 | { |
||
188 | 6 | foreach ($this->filters as $filter) { |
|
196 | } |
||
197 |