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; |
12
|
|
|
|
13
|
|
|
use FeedIo\Reader\Document; |
14
|
|
|
use FeedIo\Feed\ItemInterface; |
15
|
|
|
use FeedIo\Feed\NodeInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Parses a document if its format matches the parser's standard |
20
|
|
|
* |
21
|
|
|
* Depends on : |
22
|
|
|
* - FeedIo\StandardAbstract |
23
|
|
|
* - Psr\Log\LoggerInterface |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
abstract class ParserAbstract |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Psr\Log\LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array[FilterInterface] |
36
|
|
|
*/ |
37
|
|
|
protected $filters = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var StandardAbstract |
41
|
|
|
*/ |
42
|
|
|
protected $standard; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param StandardAbstract $standard |
46
|
|
|
* @param LoggerInterface $logger |
47
|
|
|
*/ |
48
|
35 |
|
public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
49
|
|
|
{ |
50
|
35 |
|
$this->standard = $standard; |
51
|
35 |
|
$this->logger = $logger; |
52
|
35 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Document $document |
56
|
|
|
* @param FeedInterface $feed |
57
|
|
|
* @return \FeedIo\FeedInterface |
58
|
|
|
*/ |
59
|
|
|
abstract function parse(Document $document, FeedInterface $feed); |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Document $document |
63
|
|
|
* @param array $mandatoryFields |
64
|
|
|
* @return $this |
65
|
|
|
* @throws MissingFieldsException |
66
|
|
|
*/ |
67
|
|
|
abstract public function checkBodyStructure(Document $document, array $mandatoryFields); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return StandardAbstract |
71
|
|
|
*/ |
72
|
21 |
|
public function getStandard() |
73
|
|
|
{ |
74
|
21 |
|
return $this->standard; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param FeedInterface $feed |
79
|
|
|
* @param NodeInterface $item |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
6 |
|
public function addValidItem(FeedInterface $feed, NodeInterface $item) |
83
|
|
|
{ |
84
|
6 |
|
if ($item instanceof ItemInterface && $this->isValid($item)) { |
85
|
6 |
|
$feed->add($item); |
86
|
6 |
|
} |
87
|
|
|
|
88
|
6 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ItemInterface $item |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
8 |
|
public function isValid(ItemInterface $item) |
96
|
|
|
{ |
97
|
8 |
|
foreach ($this->filters as $filter) { |
98
|
2 |
|
if (!$filter->isValid($item)) { |
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
7 |
|
} |
102
|
|
|
|
103
|
7 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param FilterInterface $filter |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
4 |
|
public function addFilter(FilterInterface $filter) |
111
|
|
|
{ |
112
|
4 |
|
$this->filters[] = $filter; |
113
|
|
|
|
114
|
4 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Reset filters |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
1 |
|
public function resetFilters() |
122
|
|
|
{ |
123
|
1 |
|
$this->filters = []; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.