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 DOMDocument; |
15
|
|
|
use FeedIo\Parser; |
16
|
|
|
use FeedIo\RuleSet; |
17
|
|
|
use FeedIo\FeedInterface; |
18
|
|
|
use FeedIo\Feed\ItemInterface; |
19
|
|
|
use FeedIo\Feed\NodeInterface; |
20
|
|
|
use FeedIo\ParserAbstract; |
21
|
|
|
use FeedIo\Reader\Document; |
22
|
|
|
use FeedIo\Parser\MissingFieldsException; |
23
|
|
|
use FeedIo\Parser\UnsupportedFormatException; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Parses a DOM document if its format matches the parser's standard |
28
|
|
|
* |
29
|
|
|
* Depends on : |
30
|
|
|
* - FeedIo\StandardAbstract |
31
|
|
|
* - Psr\Log\LoggerInterface |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
class XmlParser extends ParserAbstract |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $tagName |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
8 |
|
public function isItem($tagName) |
42
|
|
|
{ |
43
|
8 |
|
return (strtolower($this->standard->getItemNodeName()) === strtolower($tagName)); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Document $document |
48
|
|
|
* @param FeedInterface $feed |
49
|
|
|
* @return \FeedIo\FeedInterface |
50
|
|
|
* @throws Parser\MissingFieldsException |
51
|
|
|
* @throws Parser\UnsupportedFormatException |
52
|
|
|
*/ |
53
|
8 |
|
public function parseContent(Document $document, FeedInterface $feed) |
54
|
|
|
{ |
55
|
8 |
|
$element = $this->standard->getMainElement($document->getDOMDocument()); |
|
|
|
|
56
|
|
|
|
57
|
8 |
|
$this->parseNode($feed, $element, $this->standard->getFeedRuleSet()); |
|
|
|
|
58
|
|
|
|
59
|
8 |
|
return $feed; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Document $document |
64
|
|
|
* @param array $mandatoryFields |
65
|
|
|
* @return $this |
66
|
|
|
* @throws MissingFieldsException |
67
|
|
|
*/ |
68
|
11 |
|
public function checkBodyStructure(Document $document, array $mandatoryFields) |
69
|
|
|
{ |
70
|
11 |
|
$errors = array(); |
71
|
|
|
|
72
|
11 |
|
$element = $document->getDOMDocument()->documentElement; |
73
|
11 |
|
foreach ($mandatoryFields as $field) { |
74
|
6 |
|
$list = $element->getElementsByTagName($field); |
75
|
6 |
|
if (0 === $list->length) { |
76
|
2 |
|
$errors[] = $field; |
77
|
2 |
|
} |
78
|
11 |
|
} |
79
|
|
|
|
80
|
11 |
|
if (!empty($errors)) { |
81
|
2 |
|
$message = "missing mandatory field(s) : ".implode(',', $errors); |
82
|
2 |
|
$this->logger->warning($message); |
83
|
2 |
|
throw new MissingFieldsException($message); |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param NodeInterface $item |
91
|
|
|
* @param \DOMElement $element |
92
|
|
|
* @param RuleSet $ruleSet |
93
|
|
|
* @return NodeInterface |
94
|
|
|
*/ |
95
|
9 |
|
public function parseNode(NodeInterface $item, \DOMElement $element, RuleSet $ruleSet) |
96
|
|
|
{ |
97
|
9 |
|
foreach ($element->childNodes as $node) { |
98
|
8 |
|
if ($node instanceof \DOMElement) { |
99
|
8 |
|
$this->handleNode($item, $node, $ruleSet); |
100
|
8 |
|
} |
101
|
9 |
|
} |
102
|
|
|
|
103
|
9 |
|
return $item; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param NodeInterface $item |
108
|
|
|
* @param \DOMElement $node |
109
|
|
|
* @param RuleSet $ruleSet |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
8 |
|
protected function handleNode(NodeInterface $item, \DOMElement $node, RuleSet $ruleSet) |
113
|
|
|
{ |
114
|
8 |
|
if ($this->isItem($node->tagName) && $item instanceof FeedInterface) { |
115
|
7 |
|
$newItem = $this->parseNode($item->newItem(), $node, $this->standard->getItemRuleSet()); |
|
|
|
|
116
|
7 |
|
$this->addValidItem($item, $newItem); |
117
|
7 |
|
} else { |
118
|
8 |
|
$rule = $ruleSet->get($node->tagName); |
119
|
8 |
|
$rule->setProperty($item, $node); |
120
|
|
|
} |
121
|
|
|
|
122
|
8 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
} |
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: