Completed
Pull Request — master (#257)
by Éloi
01:56
created

Rss::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Language;
18
use FeedIo\Rule\Link;
19
use FeedIo\Rule\PublicId;
20
use FeedIo\Rule\Media;
21
use FeedIo\Rule\Category;
22
use FeedIo\Rule\Logo;
23
use FeedIo\RuleSet;
24
25
class Rss extends XmlAbstract
26
{
27
28
    /**
29
     * Format version
30
     */
31
    const VERSION = '2.0';
32
33
    /**
34
     * RSS document must have a <rss> root node
35
     */
36
    const ROOT_NODE_TAGNAME = 'rss';
37
38
    /**
39
     * <channel> node contains feed's metadata
40
     */
41
    const CHANNEL_NODE_TAGNAME = 'channel';
42
43
    /**
44
     * publication date
45
     */
46
    const DATE_NODE_TAGNAME = 'pubDate';
47
48
    protected $mandatoryFields = ['channel'];
49
50
    /**
51
     * Formats the document according to the standard's specification
52
     * @param  \DOMDocument $document
53
     * @return \DOMDocument
54
     */
55 3
    public function format(\DOMDocument $document) : \DOMDocument
56
    {
57 3
        $rss = $document->createElement(static::ROOT_NODE_TAGNAME);
58 3
        $rss->setAttribute('version', static::VERSION);
59
60 3
        $channel = $document->createElement(static::CHANNEL_NODE_TAGNAME);
61 3
        $rss->appendChild($channel);
62 3
        $document->appendChild($rss);
63
64 3
        return $document;
65
    }
66
67
    /**
68
     * Tells if the parser can handle the feed or not
69
     * @param  Document $document
70
     * @return boolean
71
     */
72 53 View Code Duplication
    public function canHandle(Document $document) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
73
    {
74 53
        if (!isset($document->getDOMDocument()->documentElement->tagName)) {
75
            return false;
76
        }
77 53
        return static::ROOT_NODE_TAGNAME === $document->getDOMDocument()->documentElement->tagName;
78
    }
79
80
    /**
81
     * @param  DOMDocument $document
82
     * @return \DomElement
83
     */
84 53
    public function getMainElement(\DOMDocument $document) : \DOMElement
85
    {
86 53
        return $document->documentElement->getElementsByTagName(static::CHANNEL_NODE_TAGNAME)->item(0);
87
    }
88
89
    /**
90
     * @return \FeedIo\RuleSet
91
     */
92 53
    public function buildFeedRuleSet() : RuleSet
93
    {
94 53
        $ruleSet = $this->buildBaseRuleSet();
95 53
        $ruleSet->add(new Language())
96 53
            ->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME));
97
98 53
        return $ruleSet;
99
    }
100
101
    /**
102
     * @return \FeedIo\RuleSet
103
     */
104 56
    public function buildItemRuleSet() : RuleSet
105
    {
106 56
        $ruleSet = $this->buildBaseRuleSet();
107
        $ruleSet
108 56
            ->add(new Author(), ['dc:creator'])
109 56
            ->add(new PublicId())
110 56
            ->add($this->getModifiedSinceRule(static::DATE_NODE_TAGNAME), ['lastBuildDate', 'lastPubDate'])
111 56
            ->add(new Media(), ['media:group'])
112 56
            ->add(new Media(), ['media:content'])
113
            ;
114
115 56
        return $ruleSet;
116
    }
117
118
    /**
119
     * @return \FeedIo\RuleSet
120
     */
121 57
    protected function buildBaseRuleSet() : RuleSet
122
    {
123 57
        $ruleSet = parent::buildBaseRuleSet();
124
        $ruleSet
125 57
            ->add(new Link())
126 57
            ->add(new Description())
127 57
            ->add(new Category())
128 57
            ->add(new Logo());
129
130 57
        return $ruleSet;
131
    }
132
}
133