summaryrefslogtreecommitdiff
path: root/vendor/miniflux/picofeed/lib/PicoFeed/Syndication/Rss20Helper.php
blob: 72a19e56cfb4582df6e6cb9630761b85484b33cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace PicoFeed\Syndication;

use DateTime;
use DOMDocument;
use DOMElement;

/**
 * Class Rss20Helper
 *
 * @package PicoFeed\Syndication
 * @author  Frederic Guillot
 */
class Rss20Helper
{
    /**
     * @var DOMDocument
     */
    protected $document;

    /**
     * Constructor
     *
     * @param DOMDocument $document
     */
    public function __construct(DOMDocument $document)
    {
        $this->document = $document;
    }

    /**
     * Build node
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $tag
     * @param  string     $value
     * @return $this
     */
    public function buildNode(DOMElement $element, $tag, $value)
    {
        $node = $this->document->createElement($tag);
        $node->appendChild($this->document->createTextNode($value));
        $element->appendChild($node);
        return $this;
    }

    /**
     * Build title
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $title
     * @return $this
     */
    public function buildTitle(DOMElement $element, $title)
    {
        return $this->buildNode($element, 'title', $title);
    }

    /**
     * Build date element
     *
     * @access public
     * @param  DOMElement $element
     * @param  DateTime   $date
     * @param  string     $type
     * @return $this
     */
    public function buildDate(DOMElement $element, DateTime $date, $type = 'pubDate')
    {
        return $this->buildNode($element, $type, $date->format(DateTime::RSS));
    }

    /**
     * Build link element
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $url
     * @return $this
     */
    public function buildLink(DOMElement $element, $url)
    {
        return $this->buildNode($element, 'link', $url);
    }

    /**
     * Build author element
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $tag
     * @param  string     $authorName
     * @param  string     $authorEmail
     * @return $this
     */
    public function buildAuthor(DOMElement $element, $tag, $authorName, $authorEmail)
    {
        if (!empty($authorName)) {
            $value = '';

            if (!empty($authorEmail)) {
                $value .= $authorEmail.' ('.$authorName.')';
            } else {
                $value = $authorName;
            }

            $this->buildNode($element, $tag, $value);
        }

        return $this;
    }
}