1 | <?php |
||
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 | 9 | public function __construct(StandardAbstract $standard, LoggerInterface $logger) |
|
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 | 1 | public function parse(Document $document, FeedInterface $feed) |
|
64 | { |
||
65 | 1 | if (!$this->standard->canHandle($document)) { |
|
66 | throw new UnsupportedFormatException('this is not a supported format'); |
||
67 | } |
||
68 | |||
69 | 1 | $this->checkBodyStructure($document, $this->standard->getMandatoryFields()); |
|
70 | 1 | $this->parseContent($document, $feed); |
|
71 | |||
72 | 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); |
||
83 | |||
84 | /** |
||
85 | * @param Document $document |
||
86 | * @param array $mandatoryFields |
||
87 | * @return $this |
||
88 | * @throws MissingFieldsException |
||
89 | */ |
||
90 | abstract public function checkBodyStructure(Document $document, array $mandatoryFields); |
||
91 | |||
92 | /** |
||
93 | * @return StandardAbstract |
||
94 | */ |
||
95 | 9 | public function getStandard() |
|
99 | |||
100 | /** |
||
101 | * @param FeedInterface $feed |
||
102 | * @param NodeInterface $item |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function addValidItem(FeedInterface $feed, NodeInterface $item) |
||
106 | { |
||
107 | if ($item instanceof ItemInterface && $this->isValid($item)) { |
||
108 | $feed->add($item); |
||
109 | } |
||
110 | |||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param ItemInterface $item |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function isValid(ItemInterface $item) |
||
119 | { |
||
120 | foreach ($this->filters as $filter) { |
||
121 | if (!$filter->isValid($item)) { |
||
122 | return false; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | return true; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param FilterInterface $filter |
||
131 | * @return $this |
||
132 | */ |
||
133 | 2 | public function addFilter(FilterInterface $filter) |
|
139 | |||
140 | /** |
||
141 | * Reset filters |
||
142 | * @return $this |
||
143 | */ |
||
144 | 1 | public function resetFilters() |
|
150 | } |
||
151 |