xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/JSONCreator.php (revision c13ef3ba1de57be630245f8ce5de354bdf7dc966)
1*c13ef3baSAndreas Gohr<?php
2*c13ef3baSAndreas Gohr
3*c13ef3baSAndreas Gohr/**
4*c13ef3baSAndreas Gohr * JSONCreator is a FeedCreator that implements the JSON Feed specification,
5*c13ef3baSAndreas Gohr * as in https://jsonfeed.org/version/1.1
6*c13ef3baSAndreas Gohr *
7*c13ef3baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
8*c13ef3baSAndreas Gohr */
9*c13ef3baSAndreas Gohrclass JSONCreator extends FeedCreator
10*c13ef3baSAndreas Gohr{
11*c13ef3baSAndreas Gohr    /** @inheritdoc */
12*c13ef3baSAndreas Gohr    public function createFeed()
13*c13ef3baSAndreas Gohr    {
14*c13ef3baSAndreas Gohr        $data = array();
15*c13ef3baSAndreas Gohr
16*c13ef3baSAndreas Gohr        $data['version'] = 'https://jsonfeed.org/version/1.1';
17*c13ef3baSAndreas Gohr        $data['title'] = (string)$this->title;
18*c13ef3baSAndreas Gohr        $data['home_page_url'] = (string)$this->link;
19*c13ef3baSAndreas Gohr        $data['feed_url'] = (string)$this->syndicationURL;
20*c13ef3baSAndreas Gohr        $data['description'] = (string)$this->description;
21*c13ef3baSAndreas Gohr        $data['user_comment'] = 'Created by ' . FEEDCREATOR_VERSION;
22*c13ef3baSAndreas Gohr        if ($this->image != null) {
23*c13ef3baSAndreas Gohr            $data['icon'] = $this->image->url;
24*c13ef3baSAndreas Gohr        }
25*c13ef3baSAndreas Gohr        if ($this->language != '') {
26*c13ef3baSAndreas Gohr            $data['language'] = $this->language;
27*c13ef3baSAndreas Gohr        }
28*c13ef3baSAndreas Gohr
29*c13ef3baSAndreas Gohr        $data['items'] = array();
30*c13ef3baSAndreas Gohr        foreach ($this->items as $item) {
31*c13ef3baSAndreas Gohr            $entry = array();
32*c13ef3baSAndreas Gohr            $entry['id'] = $item->guid ? (string)$item->guid : (string)$item->link;
33*c13ef3baSAndreas Gohr            $entry['url'] = (string)$item->link;
34*c13ef3baSAndreas Gohr            if ($item->source) {
35*c13ef3baSAndreas Gohr                $entry['external_url'] = (string)$item->source;
36*c13ef3baSAndreas Gohr            }
37*c13ef3baSAndreas Gohr            $entry['title'] = strip_tags((string)$item->title);
38*c13ef3baSAndreas Gohr            $entry['content_text'] = strip_tags((string)$item->description);
39*c13ef3baSAndreas Gohr            $entry['content_html'] = (string)$item->description;
40*c13ef3baSAndreas Gohr            $entry['date_published'] = (new FeedDate($item->date))->iso8601();
41*c13ef3baSAndreas Gohr            if ($item->author) {
42*c13ef3baSAndreas Gohr                // We only support one author, JSONFeed 1.1 accepts multiple
43*c13ef3baSAndreas Gohr                $entry['authors'] = array(array('name' => (string)$item->author));
44*c13ef3baSAndreas Gohr                // 1.0 only supported one, for compatibility we set it as well
45*c13ef3baSAndreas Gohr                $entry['author'] = array('name' => (string)$item->author);
46*c13ef3baSAndreas Gohr            }
47*c13ef3baSAndreas Gohr            if ($item->category) {
48*c13ef3baSAndreas Gohr                $entry['tags'] = (array)$item->category;
49*c13ef3baSAndreas Gohr            }
50*c13ef3baSAndreas Gohr            if ($item->enclosure) {
51*c13ef3baSAndreas Gohr                // We only support one enclosure, JSONFeed 1.1 accepts multiple
52*c13ef3baSAndreas Gohr                $entry['attachments'] = array(
53*c13ef3baSAndreas Gohr                    array(
54*c13ef3baSAndreas Gohr                        'url' => $item->enclosure['url'],
55*c13ef3baSAndreas Gohr                        'mime_type' => $item->enclosure['type'],
56*c13ef3baSAndreas Gohr                        'size_in_bytes' => $item->enclosure['length']
57*c13ef3baSAndreas Gohr                    )
58*c13ef3baSAndreas Gohr                );
59*c13ef3baSAndreas Gohr            }
60*c13ef3baSAndreas Gohr
61*c13ef3baSAndreas Gohr            $data['items'][] = $entry;
62*c13ef3baSAndreas Gohr        }
63*c13ef3baSAndreas Gohr
64*c13ef3baSAndreas Gohr        return json_encode($data);
65*c13ef3baSAndreas Gohr    }
66*c13ef3baSAndreas Gohr}
67