Completed
Push — master ( 6d15b1...706182 )
by Jérémy
01:26
created

Renderer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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