summaryrefslogtreecommitdiffstats
path: root/vendor/fguillot/picofeed/lib/PicoFeed/Syndication/Atom.php
blob: a379056b4f27881b653480f3cd0f0582efc83a24 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php

namespace PicoFeed\Syndication;

use DomDocument;
use DomElement;
use DomAttr;

/**
 * Atom writer class
 *
 * @author  Frederic Guillot
 * @package Syndication
 */
class Atom extends Writer
{
    /**
     * List of required properties for each feed
     *
     * @access private
     * @var array
     */
    private $required_feed_properties = array(
        'title',
        'site_url',
        'feed_url',
    );

    /**
     * List of required properties for each item
     *
     * @access private
     * @var array
     */
    private $required_item_properties = array(
        'title',
        'url',
    );

    /**
     * Get the Atom document
     *
     * @access public
     * @param  string   $filename   Optional filename
     * @return string
     */
    public function execute($filename = '')
    {
        $this->checkRequiredProperties($this->required_feed_properties, $this);

        $this->dom = new DomDocument('1.0', 'UTF-8');
        $this->dom->formatOutput = true;

        // <feed/>
        $feed = $this->dom->createElement('feed');
        $feed->setAttributeNodeNS(new DomAttr('xmlns', 'http://www.w3.org/2005/Atom'));

        // <generator/>
        $generator = $this->dom->createElement('generator', 'PicoFeed');
        $generator->setAttribute('uri', 'https://github.com/fguillot/picoFeed');
        $feed->appendChild($generator);

        // <title/>
        $title = $this->dom->createElement('title');
        $title->appendChild($this->dom->createTextNode($this->title));
        $feed->appendChild($title);

        // <id/>
        $id = $this->dom->createElement('id');
        $id->appendChild($this->dom->createTextNode($this->site_url));
        $feed->appendChild($id);

        // <updated/>
        $this->addUpdated($feed, $this->updated);

        // <link rel="alternate" type="text/html" href="http://example.org/"/>
        $this->addLink($feed, $this->site_url);

        // <link rel="self" type="application/atom+xml" href="http://example.org/feed.atom"/>
        $this->addLink($feed, $this->feed_url, 'self', 'application/atom+xml');

        // <author/>
        if (isset($this->author)) $this->addAuthor($feed, $this->author);

        // <entry/>
        foreach ($this->items as $item) {
            $this->checkRequiredProperties($this->required_item_properties, $item);
            $feed->appendChild($this->createEntry($item));
        }

        $this->dom->appendChild($feed);

        if ($filename) {
            $this->dom->save($filename);
        }
        else {
            return $this->dom->saveXML();
        }
    }

    /**
     * Create item entry
     *
     * @access public
     * @param  arrray    $item    Item properties
     * @return DomElement
     */
    public function createEntry(array $item)
    {
        $entry = $this->dom->createElement('entry');

        // <title/>
        $title = $this->dom->createElement('title');
        $title->appendChild($this->dom->createTextNode($item['title']));
        $entry->appendChild($title);

        // <id/>
        $id = $this->dom->createElement('id');
        $id->appendChild($this->dom->createTextNode(isset($item['id']) ? $item['id'] : $item['url']));
        $entry->appendChild($id);

        // <updated/>
        $this->addUpdated($entry, isset($item['updated']) ? $item['updated'] : '');

        // <published/>
        if (isset($item['published'])) {
            $entry->appendChild($this->dom->createElement('published', date(DATE_ATOM, $item['published'])));
        }

        // <link rel="alternate" type="text/html" href="http://example.org/"/>
        $this->addLink($entry, $item['url']);

        // <summary/>
        if (isset($item['summary'])) {
            $summary = $this->dom->createElement('summary');
            $summary->appendChild($this->dom->createTextNode($item['summary']));
            $entry->appendChild($summary);
        }

        // <content/>
        if (isset($item['content'])) {
            $content = $this->dom->createElement('content');
            $content->setAttribute('type', 'html');
            $content->appendChild($this->dom->createCDATASection($item['content']));
            $entry->appendChild($content);
        }

        // <author/>
        if (isset($item['author'])) {
            $this->addAuthor($entry, $item['author']);
        }

        return $entry;
    }

    /**
     * Add Link
     *
     * @access public
     * @param  DomElement   $xml     XML node
     * @param  string       $url     URL
     * @param  string       $rel     Link rel attribute
     * @param  string       $type    Link type attribute
     */
    public function addLink(DomElement $xml, $url, $rel = 'alternate', $type = 'text/html')
    {
        $link = $this->dom->createElement('link');
        $link->setAttribute('rel', $rel);
        $link->setAttribute('type', $type);
        $link->setAttribute('href', $url);
        $xml->appendChild($link);
    }

    /**
     * Add publication date
     *
     * @access public
     * @param  DomElement   $xml     XML node
     * @param  integer      $value   Timestamp
     */
    public function addUpdated(DomElement $xml, $value = 0)
    {
        $xml->appendChild($this->dom->createElement(
            'updated',
            date(DATE_ATOM, $value ?: time())
        ));
    }

    /**
     * Add author
     *
     * @access public
     * @param  DomElement   $xml     XML node
     * @param  array        $values  Author name and email
     */
    public function addAuthor(DomElement $xml, array $values)
    {
        $author = $this->dom->createElement('author');

        if (isset($values['name'])) {
            $name = $this->dom->createElement('name');
            $name->appendChild($this->dom->createTextNode($values['name']));
            $author->appendChild($name);
        }

        if (isset($values['email'])) {
            $email = $this->dom->createElement('email');
            $email->appendChild($this->dom->createTextNode($values['email']));
            $author->appendChild($email);
        }

        if (isset($values['url'])) {
            $uri = $this->dom->createElement('uri');
            $uri->appendChild($this->dom->createTextNode($values['url']));
            $author->appendChild($uri);
        }

        $xml->appendChild($author);
    }
}