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\Standard; |
12
|
|
|
|
13
|
|
|
use DOMDocument; |
14
|
|
|
use FeedIo\Reader\Document; |
15
|
|
|
use FeedIo\Rule\Author; |
16
|
|
|
use FeedIo\Rule\Description; |
17
|
|
|
use FeedIo\Rule\Image; |
18
|
|
|
use FeedIo\Rule\Language; |
19
|
|
|
use FeedIo\Rule\Link; |
20
|
|
|
use FeedIo\Rule\PublicId; |
21
|
|
|
use FeedIo\Rule\Media; |
22
|
|
|
use FeedIo\Rule\Category; |
23
|
|
|
use FeedIo\Rule\Logo; |
24
|
|
|
use FeedIo\RuleSet; |
25
|
|
|
|
26
|
|
|
class Rss extends XmlAbstract |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Format version |
31
|
|
|
*/ |
32
|
|
|
const VERSION = '2.0'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* RSS document must have a <rss> root node |
36
|
|
|
*/ |
37
|
|
|
const ROOT_NODE_TAGNAME = 'rss'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* <channel> node contains feed's metadata |
41
|
|
|
*/ |
42
|
|
|
const CHANNEL_NODE_TAGNAME = 'channel'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* publication date |
46
|
|
|
*/ |
47
|
|
|
const DATE_NODE_TAGNAME = 'pubDate'; |
48
|
|
|
|
49
|
|
|
protected $mandatoryFields = ['channel']; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Formats the document according to the standard's specification |
53
|
|
|
* @param \DOMDocument $document |
54
|
|
|
* @return \DOMDocument |
55
|
3 |
|
*/ |
56
|
|
|
public function format(\DOMDocument $document) : \DOMDocument |
57
|
3 |
|
{ |
58
|
3 |
|
$rss = $document->createElement(static::ROOT_NODE_TAGNAME); |
59
|
|
|
$rss->setAttribute('version', static::VERSION); |
60
|
3 |
|
|
61
|
3 |
|
$channel = $document->createElement(static::CHANNEL_NODE_TAGNAME); |
62
|
3 |
|
$rss->appendChild($channel); |
63
|
|
|
$document->appendChild($rss); |
64
|
3 |
|
|
65
|
|
|
return $document; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Tells if the parser can handle the feed or not |
70
|
|
|
* @param Document $document |
71
|
|
|
* @return boolean |
72
|
7 |
|
*/ |
73
|
|
View Code Duplication |
public function canHandle(Document $document) : bool |
|
|
|
|
74
|
7 |
|
{ |
75
|
|
|
if (!isset($document->getDOMDocument()->documentElement->tagName)) { |
76
|
|
|
return false; |
77
|
7 |
|
} |
78
|
|
|
return static::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param DOMDocument $document |
83
|
|
|
* @return \DomElement |
84
|
7 |
|
*/ |
85
|
|
|
public function getMainElement(\DOMDocument $document) : \DOMElement |
86
|
7 |
|
{ |
87
|
|
|
return $document->documentElement->getElementsByTagName(static::CHANNEL_NODE_TAGNAME)->item(0); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \FeedIo\RuleSet |
92
|
7 |
|
*/ |
93
|
|
|
public function buildFeedRuleSet() : RuleSet |
94
|
7 |
|
{ |
95
|
7 |
|
$ruleSet = $this->buildBaseRuleSet(); |
96
|
7 |
|
$ruleSet->add(new Language()) |
97
|
|
|
->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate']); |
98
|
7 |
|
|
99
|
|
|
return $ruleSet; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \FeedIo\RuleSet |
104
|
10 |
|
*/ |
105
|
|
|
public function buildItemRuleSet() : RuleSet |
106
|
10 |
|
{ |
107
|
|
|
$ruleSet = $this->buildBaseRuleSet(); |
108
|
10 |
|
$ruleSet |
109
|
10 |
|
->add(new Author(), ['dc:creator']) |
110
|
10 |
|
->add(new PublicId()) |
111
|
10 |
|
->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate']) |
112
|
10 |
|
->add(new Image()) |
113
|
10 |
|
->add(new Media(), ['media:thumbnail']) |
114
|
|
|
->add(new Media(), ['media:group']) |
115
|
|
|
->add(new Media(), ['media:content']) |
116
|
10 |
|
; |
117
|
|
|
|
118
|
|
|
return $ruleSet; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
11 |
|
* @return \FeedIo\RuleSet |
123
|
|
|
*/ |
124
|
11 |
|
protected function buildBaseRuleSet() : RuleSet |
125
|
|
|
{ |
126
|
11 |
|
$ruleSet = parent::buildBaseRuleSet(); |
127
|
11 |
|
$ruleSet |
128
|
11 |
|
->add(new Link()) |
129
|
11 |
|
->add(new Description()) |
130
|
|
|
->add(new Category()) |
131
|
11 |
|
->add(new Logo()); |
132
|
|
|
|
133
|
|
|
return $ruleSet; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.