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\Atom\Author; |
16
|
|
|
use FeedIo\Rule\Atom\LinkNode; |
17
|
|
|
use FeedIo\Rule\Atom\Logo; |
18
|
|
|
use FeedIo\Rule\Description; |
19
|
|
|
use FeedIo\Rule\Language; |
20
|
|
|
use FeedIo\Rule\Media; |
21
|
|
|
use FeedIo\Rule\PublicId; |
22
|
|
|
use FeedIo\Rule\Atom\Category; |
23
|
|
|
use FeedIo\RuleSet; |
24
|
|
|
|
25
|
|
|
class Atom extends XmlAbstract |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Atom document must have a <feed> root node |
29
|
|
|
*/ |
30
|
|
|
const ROOT_NODE_TAGNAME = 'feed'; |
31
|
|
|
|
32
|
|
|
const ITEM_NODE = 'entry'; |
33
|
|
|
|
34
|
|
|
const DATETIME_FORMAT = \DateTime::ATOM; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Formats the document according to the standard's specification |
38
|
|
|
* @param \DOMDocument $document |
39
|
|
|
* @return \DOMDocument |
40
|
|
|
*/ |
41
|
5 |
|
public function format(\DOMDocument $document) : \DOMDocument |
42
|
|
|
{ |
43
|
5 |
|
$element = $document->createElement('feed'); |
44
|
5 |
|
$element->setAttribute('xmlns', 'http://www.w3.org/2005/Atom'); |
45
|
5 |
|
$document->appendChild($element); |
46
|
|
|
|
47
|
5 |
|
return $document; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Tells if the parser can handle the feed or not |
52
|
|
|
* @param Document $document |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
5 |
View Code Duplication |
public function canHandle(Document $document) : bool |
|
|
|
|
56
|
|
|
{ |
57
|
5 |
|
if (!isset($document->getDOMDocument()->documentElement->tagName)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
5 |
|
return self::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param DOMDocument $document |
65
|
|
|
* @return \DomElement |
66
|
|
|
*/ |
67
|
9 |
|
public function getMainElement(\DOMDocument $document) : \DOMElement |
68
|
|
|
{ |
69
|
9 |
|
return $document->documentElement; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Builds and returns a rule set to parse the root node |
74
|
|
|
* @return \FeedIo\RuleSet |
75
|
|
|
*/ |
76
|
10 |
|
public function buildFeedRuleSet() : RuleSet |
77
|
|
|
{ |
78
|
10 |
|
$ruleSet = $this->buildBaseRuleSet(); |
79
|
|
|
$ruleSet |
80
|
10 |
|
->add(new LinkNode()) |
81
|
10 |
|
->add(new PublicId('id')) |
82
|
10 |
|
->add(new Language('lang')) |
83
|
10 |
|
->add($this->getModifiedSinceRule('updated'), ['published']) |
84
|
|
|
; |
85
|
|
|
|
86
|
10 |
|
return $ruleSet; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Builds and returns a rule set to parse an item |
91
|
|
|
* @return \FeedIo\RuleSet |
92
|
|
|
*/ |
93
|
8 |
|
public function buildItemRuleSet() : RuleSet |
94
|
|
|
{ |
95
|
8 |
|
$ruleSet = $this->buildFeedRuleSet(); |
96
|
|
|
$ruleSet |
97
|
8 |
|
->add(new Author()) |
98
|
8 |
|
->add(new Description('content'), ['summary']) |
99
|
8 |
|
->add(new Media(), ['media:group']) |
100
|
8 |
|
->add(new Media(), ['media:content']) |
101
|
|
|
; |
102
|
|
|
|
103
|
8 |
|
return $ruleSet; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return \FeedIo\RuleSet |
108
|
|
|
*/ |
109
|
10 |
|
protected function buildBaseRuleSet() : RuleSet |
110
|
|
|
{ |
111
|
10 |
|
$ruleSet = parent::buildBaseRuleSet(); |
112
|
10 |
|
$ruleSet->add(new Category()) |
113
|
10 |
|
->add(new Logo()); |
114
|
|
|
|
115
|
10 |
|
return $ruleSet; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.