Completed
Push — issue/134 ( d0dc5e...07c20b )
by Alex
01:47
created

JsonFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 123
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 4 1
A toArray() 0 12 1
A itemsToArray() 0 6 2
A itemToArray() 0 9 1
A itemToBaseArray() 0 10 2
A isHtml() 0 4 1
A handleAuthor() 0 11 2
A handleMedia() 0 8 2
A handleDate() 0 8 2
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Formatter;
12
13
use FeedIo\Feed;
14
use FeedIo\FeedInterface;
15
use FeedIo\FormatterInterface;
16
17
class JsonFormatter implements FormatterInterface
18
{
19
20
    /**
21
     * @param FeedInterface $feed
22
     * @return string
23
     */
24 1
    public function toString(FeedInterface $feed)
25
    {
26 1
        return json_encode($this->toArray($feed));
27
    }
28
29
    /**
30
     * @param FeedInterface $feed
31
     * @return array
32
     */
33 1
    public function toArray(FeedInterface $feed)
34
    {
35 1
        return array_filter([
36 1
            'version' => 'https://jsonfeed.org/version/1',
37 1
            'title' => $feed->getTitle(),
38 1
            'description' => $feed->getDescription(),
39 1
            'home_page_url' => $feed->getLink(),
40 1
            'feed_url' => $feed->getUrl(),
41 1
            'id' => $feed->getPublicId(),
42 1
            'items' => iterator_to_array($this->itemsToArray($feed)),
43 1
        ]);
44
    }
45
46
    /**
47
     * @param FeedInterface $feed
48
     * @return \Generator
49
     */
50 1
    public function itemsToArray(FeedInterface $feed)
51
    {
52 1
        foreach ($feed as $item) {
53 1
            yield $this->itemToArray($item);
54 1
        }
55 1
    }
56
57
    /**
58
     * @param Feed\ItemInterface $item
59
     * @return array
60
     */
61 1
    public function itemToArray(Feed\ItemInterface $item)
62
    {
63 1
        $array = $this->itemToBaseArray($item);
64 1
        $this->handleAuthor($item, $array);
65 1
        $this->handleMedia($item, $array);
66 1
        $this->handleDate($item, $array);
67
68 1
        return array_filter($array);
69
    }
70
71
    /**
72
     * @param Feed\ItemInterface $item
73
     * @return array
74
     */
75 1
    public function itemToBaseArray(Feed\ItemInterface $item)
76
    {
77 1
        $offset = $this->isHtml($item->getDescription()) ? 'content_html':'content_txt';
78
        return [
79 1
            'id' => $item->getPublicId(),
80 1
            'title' => $item->getTitle(),
81 1
            $offset => $item->getDescription(),
82 1
            'url' => $item->getLink(),
83 1
        ];
84
    }
85
86
    /**
87
     * @param $string
88
     * @return bool
89
     */
90 2
    public function isHtml($string)
91
    {
92 2
        return $string !== strip_tags($string);
93
    }
94
95
    /**
96
     * @param Feed\ItemInterface $item
97
     * @param array $array
98
     * @return array
99
     */
100 1
    public function handleAuthor(Feed\ItemInterface $item, array &$array)
101
    {
102 1
        if (! is_null($item->getAuthor())) {
103 1
            $array['author'] = array_filter([
104 1
                'name' => $item->getAuthor()->getName(),
105 1
                'url' => $item->getAuthor()->getUri(),
106 1
            ]);
107 1
        }
108
109 1
        return $array;
110
    }
111
112
    /**
113
     * @param Feed\ItemInterface $item
114
     * @param array $array
115
     * @return array
116
     */
117 1
    public function handleMedia(Feed\ItemInterface $item, array &$array)
118
    {
119 1
        if ($item->hasMedia()) {
120 1
            $array['image'] = $item->getMedias()->current()->getUrl();
121 1
        }
122
123 1
        return $array;
124
    }
125
126
    /**
127
     * @param Feed\ItemInterface $item
128
     * @param array $array
129
     * @return array
130
     */
131 1
    public function handleDate(Feed\ItemInterface $item, array &$array)
132
    {
133 1
        if (! is_null($item->getLastModified())) {
134 1
            $array['date_published'] = $item->getLastModified()->format(\DateTime::RFC3339);
135 1
        }
136
137 1
        return $array;
138
    }
139
}
140