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