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\Parser; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use FeedIo\Parser; |
15
|
|
|
use FeedIo\RuleSet; |
16
|
|
|
use FeedIo\FeedInterface; |
17
|
|
|
use FeedIo\Feed\NodeInterface; |
18
|
|
|
use FeedIo\ParserAbstract; |
19
|
|
|
use FeedIo\Reader\Document; |
20
|
|
|
use FeedIo\Parser\MissingFieldsException; |
21
|
|
|
use FeedIo\Parser\UnsupportedFormatException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Parses a DOM document if its format matches the parser's standard |
25
|
|
|
* |
26
|
|
|
* Depends on : |
27
|
|
|
* - FeedIo\StandardAbstract |
28
|
|
|
* - Psr\Log\LoggerInterface |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
class XmlParser extends ParserAbstract |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $tagName |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function isItem($tagName) |
39
|
|
|
{ |
40
|
|
|
return (strtolower($this->standard->getItemNodeName()) === strtolower($tagName)); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Document $document |
45
|
|
|
* @param FeedInterface $feed |
46
|
|
|
* @return \FeedIo\FeedInterface |
47
|
|
|
* @throws Parser\MissingFieldsException |
48
|
|
|
* @throws Parser\UnsupportedFormatException |
49
|
|
|
*/ |
50
|
|
|
public function parseContent(Document $document, FeedInterface $feed) |
51
|
|
|
{ |
52
|
|
|
$element = $this->standard->getMainElement($document->getDOMDocument()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->parseNode($feed, $element, $this->standard->getFeedRuleSet()); |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $feed; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Document $document |
61
|
|
|
* @param array $mandatoryFields |
62
|
|
|
* @return $this |
63
|
|
|
* @throws MissingFieldsException |
64
|
|
|
*/ |
65
|
|
|
public function checkBodyStructure(Document $document, array $mandatoryFields) |
66
|
|
|
{ |
67
|
|
|
$errors = array(); |
68
|
|
|
|
69
|
|
|
$element = $document->getDOMDocument()->documentElement; |
70
|
|
|
foreach ($mandatoryFields as $field) { |
71
|
|
|
$list = $element->getElementsByTagName($field); |
72
|
|
|
if (0 === $list->length) { |
73
|
|
|
$errors[] = $field; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!empty($errors)) { |
78
|
|
|
$message = "missing mandatory field(s) : ".implode(',', $errors); |
79
|
|
|
$this->logger->warning($message); |
80
|
|
|
throw new MissingFieldsException($message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param NodeInterface $item |
88
|
|
|
* @param \DOMElement $element |
89
|
|
|
* @param RuleSet $ruleSet |
90
|
|
|
* @return NodeInterface |
91
|
|
|
*/ |
92
|
|
|
public function parseNode(NodeInterface $item, \DOMElement $element, RuleSet $ruleSet) |
93
|
|
|
{ |
94
|
|
|
foreach ($element->childNodes as $node) { |
95
|
|
|
if ($node instanceof \DOMElement) { |
96
|
|
|
$this->handleNode($item, $node, $ruleSet); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $item; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param NodeInterface $item |
105
|
|
|
* @param \DOMElement $node |
106
|
|
|
* @param RuleSet $ruleSet |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
protected function handleNode(NodeInterface $item, \DOMElement $node, RuleSet $ruleSet) |
110
|
|
|
{ |
111
|
|
|
if ($this->isItem($node->tagName) && $item instanceof FeedInterface) { |
112
|
|
|
$newItem = $this->parseNode($item->newItem(), $node, $this->standard->getItemRuleSet()); |
|
|
|
|
113
|
|
|
$this->addValidItem($item, $newItem); |
114
|
|
|
} else { |
115
|
|
|
$rule = $ruleSet->get($node->tagName); |
116
|
|
|
$rule->setProperty($item, $node); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: