Completed
Pull Request — master (#12)
by Jérémy
01:39
created

Renderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JDecool\JsonFeed\Writer\Version1;
4
5
use DateTime;
6
use JDecool\JsonFeed\Attachment;
7
use JDecool\JsonFeed\Author;
8
use JDecool\JsonFeed\Feed;
9
use JDecool\JsonFeed\Hub;
10
use JDecool\JsonFeed\Item;
11
use JDecool\JsonFeed\Versions;
12
use JDecool\JsonFeed\Writer\RendererInterface;
13
14
class Renderer implements RendererInterface
15
{
16
    /** @var int `json_encode` bitmask */
17
    private $flags = 0;
18
19
    /**
20
     * Constructor
21
     *
22
     * @property int $flags
23
     */
24
    public function __construct($flags = JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT)
25
    {
26
        $this->flags = $flags;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function render(Feed $feed)
33
    {
34
        $result = [
35
            'version' => Versions::VERSION_1,
36
            'title' => $feed->getTitle(),
37
        ];
38
39
        if ($homepageUrl = $feed->getHomepageUrl()) {
40
            $result['home_page_url'] = $homepageUrl;
41
        }
42
43
        if ($feedUrl = $feed->getFeedUrl()) {
44
            $result['feed_url'] = $feedUrl;
45
        }
46
47
        if ($description = $feed->getDescription()) {
48
            $result['description'] = $description;
49
        }
50
51
        if ($userComment = $feed->getUserComment()) {
52
            $result['user_comment'] = $userComment;
53
        }
54
55
        if ($nextUrl = $feed->getNextUrl()) {
56
            $result['next_url'] = $nextUrl;
57
        }
58
59
        if ($icon = $feed->getIcon()) {
60
            $result['icon'] = $icon;
61
        }
62
63
        if ($favicon = $feed->getFavicon()) {
64
            $result['favicon'] = $favicon;
65
        }
66
67
        if ($author = $feed->getAuthor()) {
68
            $result['author'] = $this->renderAuthor($author);
69
        }
70
71
        if (null !== $expired = $feed->isExpired()) {
72
            $result['expired'] = (bool) $expired;
73
        }
74
75
        if ($items = $feed->getItems()) {
76
            $result['items'] = array_map(function(Item $item) {
77
                return $this->renderItem($item);
78
            }, $items);
79
        }
80
81
        if ($hubs = $feed->getHubs()) {
82
            $result['hubs'] = array_map(function(Hub $hub) {
83
                return $this->renderHub($hub);
84
            }, $hubs);
85
        }
86
87
        return json_encode($result, $this->flags);
88
    }
89
90
    /**
91
     * Render item
92
     *
93
     * @param Item $item
94
     * @return array
95
     */
96
    private function renderItem(Item $item)
97
    {
98
        $result = [
99
            'id' => $item->getId(),
100
        ];
101
102
        if ($url = $item->getUrl()) {
103
            $result['url'] = $url;
104
        }
105
106
        if ($externalUrl = $item->getExternalUrl()) {
107
            $result['external_url'] = $externalUrl;
108
        }
109
110
        if ($title = $item->getTitle()) {
111
            $result['title'] = $title;
112
        }
113
114
        if ($contentHtml = $item->getContentHtml()) {
115
            $result['content_html'] = $contentHtml;
116
        }
117
118
        if ($contentText = $item->getContentText()) {
119
            $result['content_text'] = $contentText;
120
        }
121
122
        if ($summary = $item->getSummary()) {
123
            $result['summary'] = $summary;
124
        }
125
126
        if ($image = $item->getImage()) {
127
            $result['image'] = $image;
128
        }
129
130
        if ($bannerImage = $item->getBannerImage()) {
131
            $result['banner_image'] = $bannerImage;
132
        }
133
134
        if ($datePublished = $item->getDatePublished()) {
135
            $result['date_published'] = $datePublished->format(DateTime::ATOM);
136
        }
137
138
        if ($dateModified = $item->getDateModified()) {
139
            $result['date_modified'] = $dateModified->format(DateTime::ATOM);
140
        }
141
142
        if ($tags = $item->getTags()) {
143
            $result['tags'] = $tags;
144
        }
145
146
        if ($attachments = $item->getAttachments()) {
147
            $result['attachments'] = array_map(function(Attachment $attachment) {
148
                return $this->renderAttachment($attachment);
149
            }, $attachments);
150
        }
151
152
        if ($author = $item->getAuthor()) {
153
            $result['author'] = $this->renderAuthor($author);
154
        }
155
156
        if ($extensions = $item->getExtensions()) {
157
            foreach ($extensions as $key => $extension) {
158
                $result['_'.$key] = $extension;
159
            }
160
        }
161
162
        return $result;
163
    }
164
165
    /**
166
     * Render attachment
167
     *
168
     * @param Attachment $attachment
169
     * @return array
170
     */
171
    private function renderAttachment(Attachment $attachment)
172
    {
173
        $result = [
174
            'url' => $attachment->getUrl(),
175
            'mime_type' => $attachment->getMimeType(),
176
        ];
177
178
        if ($title = $attachment->getTitle()) {
179
            $result['title'] = $title;
180
        }
181
182
        if ($size = $attachment->getSize()) {
183
            $result['size_in_bytes'] = $size;
184
        }
185
186
        if ($duration = $attachment->getDuration()) {
187
            $result['duration_in_seconds'] = $duration;
188
        }
189
190
        return $result;
191
    }
192
193
    /**
194
     * Render author
195
     *
196
     * @param Author $author
197
     * @return array
198
     */
199
    private function renderAuthor(Author $author)
200
    {
201
        $result = [];
202
203
        if ($name = $author->getName()) {
204
            $result['name'] = $name;
205
        }
206
207
        if ($url = $author->getUrl()) {
208
            $result['url'] = $url;
209
        }
210
211
        if ($avatar = $author->getAvatar()) {
212
            $result['avatar'] = $avatar;
213
        }
214
215
        return $result;
216
    }
217
218
    /**
219
     * Render hub
220
     *
221
     * @param Hub $hub
222
     * @return array
223
     */
224
    private function renderHub(Hub $hub)
225
    {
226
        return [
227
            'type' => $hub->getType(),
228
            'url' => $hub->getUrl(),
229
        ];
230
    }
231
}
232