|
1
|
|
|
<?php declare(strict_types=1); |
|
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\Parser\UnsupportedFormatException; |
|
14
|
|
|
use FeedIo\Reader\Document; |
|
15
|
|
|
use FeedIo\Feed\ItemInterface; |
|
16
|
|
|
use FeedIo\Feed\NodeInterface; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parses a document if its format matches the parser's standard |
|
21
|
|
|
* |
|
22
|
|
|
* Depends on : |
|
23
|
|
|
* - FeedIo\StandardAbstract |
|
24
|
|
|
* - Psr\Log\LoggerInterface |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class ParserAbstract |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Psr\Log\LoggerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $logger; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array[FilterInterface] |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $filters = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var StandardAbstract |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $standard; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param StandardAbstract $standard |
|
47
|
|
|
* @param LoggerInterface $logger |
|
48
|
|
|
*/ |
|
49
|
45 |
|
public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
|
50
|
|
|
{ |
|
51
|
45 |
|
$this->standard = $standard; |
|
52
|
45 |
|
$this->logger = $logger; |
|
53
|
45 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Tries to parse the document |
|
57
|
|
|
* |
|
58
|
|
|
* @param Document $document |
|
59
|
|
|
* @param FeedInterface $feed |
|
60
|
|
|
* @return \FeedIo\FeedInterface |
|
61
|
|
|
* @throws \FeedIo\Parser\UnsupportedFormatException |
|
62
|
|
|
*/ |
|
63
|
16 |
|
public function parse(Document $document, FeedInterface $feed) : FeedInterface |
|
64
|
|
|
{ |
|
65
|
16 |
|
if (!$this->standard->canHandle($document)) { |
|
66
|
1 |
|
throw new UnsupportedFormatException('this is not a supported format'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
15 |
|
$this->checkBodyStructure($document, $this->standard->getMandatoryFields()); |
|
|
|
|
|
|
70
|
14 |
|
$this->parseContent($document, $feed); |
|
71
|
|
|
|
|
72
|
14 |
|
return $feed; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* This method is called by parse() if and only if the checkBodyStructure was successful |
|
77
|
|
|
* |
|
78
|
|
|
* @param Document $document |
|
79
|
|
|
* @param FeedInterface $feed |
|
80
|
|
|
* @return \FeedIo\FeedInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function parseContent(Document $document, FeedInterface $feed) : FeedInterface; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Document $document |
|
86
|
|
|
* @param iterable $mandatoryFields |
|
87
|
|
|
* @throws MissingFieldsException |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function checkBodyStructure(Document $document, iterable $mandatoryFields) : bool; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return StandardAbstract |
|
94
|
|
|
*/ |
|
95
|
29 |
|
public function getStandard() : StandardAbstract |
|
96
|
|
|
{ |
|
97
|
29 |
|
return $this->standard; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param FeedInterface $feed |
|
102
|
|
|
* @param NodeInterface $item |
|
103
|
|
|
* @return ParserAbstract |
|
104
|
|
|
*/ |
|
105
|
11 |
|
public function addValidItem(FeedInterface $feed, NodeInterface $item) : ParserAbstract |
|
106
|
|
|
{ |
|
107
|
11 |
|
if ($item instanceof ItemInterface && $this->isValid($item)) { |
|
108
|
10 |
|
$feed->add($item); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
11 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param ItemInterface $item |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
13 |
|
public function isValid(ItemInterface $item) : bool |
|
119
|
|
|
{ |
|
120
|
13 |
|
foreach ($this->filters as $filter) { |
|
121
|
3 |
|
if (!$filter->isValid($item)) { |
|
122
|
2 |
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
11 |
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param FilterInterface $filter |
|
131
|
|
|
* @return ParserAbstract |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function addFilter(FilterInterface $filter) : ParserAbstract |
|
134
|
|
|
{ |
|
135
|
4 |
|
$this->filters[] = $filter; |
|
136
|
|
|
|
|
137
|
4 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Reset filters |
|
142
|
|
|
* @return ParserAbstract |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function resetFilters() : ParserAbstract |
|
145
|
|
|
{ |
|
146
|
2 |
|
$this->filters = []; |
|
147
|
|
|
|
|
148
|
2 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: