|
1
|
|
|
<?php declare(strict_types=1); |
|
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
|
2 |
|
public function toString(FeedInterface $feed) : string |
|
25
|
|
|
{ |
|
26
|
2 |
|
return json_encode($this->toArray($feed)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param FeedInterface $feed |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function toArray(FeedInterface $feed) : array |
|
34
|
|
|
{ |
|
35
|
2 |
|
return array_filter([ |
|
36
|
2 |
|
'version' => 'https://jsonfeed.org/version/1', |
|
37
|
2 |
|
'title' => $feed->getTitle(), |
|
38
|
2 |
|
'description' => $feed->getDescription(), |
|
39
|
2 |
|
'home_page_url' => $feed->getLink(), |
|
40
|
2 |
|
'feed_url' => $feed->getUrl(), |
|
41
|
2 |
|
'id' => $feed->getPublicId(), |
|
42
|
2 |
|
'icon' => $feed->getLogo(), |
|
43
|
2 |
|
'items' => iterator_to_array($this->itemsToArray($feed)), |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param FeedInterface $feed |
|
49
|
|
|
* @return iterable |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function itemsToArray(FeedInterface $feed) : iterable |
|
52
|
|
|
{ |
|
53
|
2 |
|
foreach ($feed as $item) { |
|
54
|
2 |
|
yield $this->itemToArray($item); |
|
55
|
|
|
} |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Feed\ItemInterface $item |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function itemToArray(Feed\ItemInterface $item) : array |
|
63
|
|
|
{ |
|
64
|
2 |
|
$array = $this->itemToBaseArray($item); |
|
65
|
2 |
|
$this->handleAuthor($item, $array); |
|
66
|
2 |
|
$this->handleMedia($item, $array); |
|
67
|
2 |
|
$this->handleDate($item, $array); |
|
68
|
|
|
|
|
69
|
2 |
|
return array_filter($array); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Feed\ItemInterface $item |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function itemToBaseArray(Feed\ItemInterface $item) : array |
|
77
|
|
|
{ |
|
78
|
2 |
|
$offset = $this->isHtml($item->getDescription()) ? 'content_html':'content_txt'; |
|
79
|
|
|
return [ |
|
80
|
2 |
|
'id' => $item->getPublicId(), |
|
81
|
2 |
|
'title' => $item->getTitle(), |
|
82
|
2 |
|
$offset => $item->getDescription(), |
|
83
|
2 |
|
'url' => $item->getLink(), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $string |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function isHtml(string $string) : bool |
|
92
|
|
|
{ |
|
93
|
3 |
|
return $string !== strip_tags($string); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Feed\ItemInterface $item |
|
98
|
|
|
* @param array $array |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function handleAuthor(Feed\ItemInterface $item, array &$array) : array |
|
102
|
|
|
{ |
|
103
|
2 |
|
if (! is_null($item->getAuthor())) { |
|
104
|
1 |
|
$array['author'] = array_filter([ |
|
105
|
1 |
|
'name' => $item->getAuthor()->getName(), |
|
106
|
1 |
|
'url' => $item->getAuthor()->getUri(), |
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
return $array; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Feed\ItemInterface $item |
|
115
|
|
|
* @param array $array |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function handleMedia(Feed\ItemInterface $item, array &$array) : array |
|
119
|
|
|
{ |
|
120
|
2 |
|
if ($item->hasMedia()) { |
|
121
|
1 |
|
$array['image'] = $item->getMedias()->current()->getUrl(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
2 |
|
return $array; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param Feed\ItemInterface $item |
|
129
|
|
|
* @param array $array |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function handleDate(Feed\ItemInterface $item, array &$array) : array |
|
133
|
|
|
{ |
|
134
|
2 |
|
if (! is_null($item->getLastModified())) { |
|
135
|
2 |
|
$array['date_published'] = $item->getLastModified()->format(\DateTime::RFC3339); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
return $array; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|