summaryrefslogtreecommitdiff
path: root/vendor/miniflux/picofeed/lib/PicoFeed/Syndication/AtomHelper.php
blob: def6b0b9a8794d205445cae1f842c6918ad21f40 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace PicoFeed\Syndication;

use DateTime;
use DOMDocument;
use DOMElement;

/**
 * Class AtomHelper
 *
 * @package PicoFeed\Syndication
 * @author  Frederic Guillot
 */
class AtomHelper
{
    /**
     * @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 AtomHelper
     */
    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 AtomHelper
     */
    public function buildTitle(DOMElement $element, $title)
    {
        return $this->buildNode($element, 'title', $title);
    }

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

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

    /**
     * Build link element
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $url
     * @param  string     $rel
     * @param  string     $type
     * @return AtomHelper
     */
    public function buildLink(DOMElement $element, $url, $rel = 'alternate', $type = 'text/html')
    {
        $node = $this->document->createElement('link');
        $node->setAttribute('rel', $rel);
        $node->setAttribute('type', $type);
        $node->setAttribute('href', $url);
        $element->appendChild($node);

        return $this;
    }

    /**
     * Build author element
     *
     * @access public
     * @param  DOMElement $element
     * @param  string     $authorName
     * @param  string     $authorEmail
     * @param  string     $authorUrl
     * @return AtomHelper
     */
    public function buildAuthor(DOMElement $element, $authorName, $authorEmail, $authorUrl)
    {
        if (!empty($authorName)) {
            $author = $this->document->createElement('author');
            $this->buildNode($author, 'name', $authorName);

            if (!empty($authorEmail)) {
                $this->buildNode($author, 'email', $authorEmail);
            }

            if (!empty($authorUrl)) {
                $this->buildNode($author, 'uri', $authorUrl);
            }

            $element->appendChild($author);
        }

        return $this;
    }
}