1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDecool\JsonFeed\Reader\Version1; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use JDecool\JsonFeed\Attachment; |
8
|
|
|
use JDecool\JsonFeed\Author; |
9
|
|
|
use JDecool\JsonFeed\Feed; |
10
|
|
|
use JDecool\JsonFeed\Hub; |
11
|
|
|
use JDecool\JsonFeed\Item; |
12
|
|
|
use JDecool\JsonFeed\Reader\ReaderInterface; |
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
14
|
|
|
|
15
|
|
|
class FeedReader implements ReaderInterface |
16
|
|
|
{ |
17
|
|
|
/** @var \Symfony\Component\PropertyAccess\PropertyAccessor */ |
18
|
|
|
private $accessor; |
19
|
|
|
|
20
|
|
|
/** @var FeedReader */ |
21
|
|
|
private static $instance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create reader instance |
25
|
|
|
* |
26
|
|
|
* @return FeedReader |
27
|
|
|
*/ |
28
|
|
|
public static function create() |
29
|
|
|
{ |
30
|
|
|
if (null === self::$instance) { |
31
|
|
|
self::$instance = new self; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return self::$instance; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
*/ |
40
|
|
|
private function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->accessor = PropertyAccess::createPropertyAccessor(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function readFromJson($json) |
49
|
|
|
{ |
50
|
|
|
$content = json_decode($json, true); |
51
|
|
|
if (!is_array($content)) { |
52
|
|
|
throw new InvalidArgumentException('Invalid JSONFeed string'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->readFeedNode($content); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Browse feed node |
60
|
|
|
* |
61
|
|
|
* @param array $content |
62
|
|
|
* @return Feed |
63
|
|
|
*/ |
64
|
|
|
private function readFeedNode(array $content) |
65
|
|
|
{ |
66
|
|
|
$feed = new Feed(''); |
67
|
|
|
|
68
|
|
|
foreach ($content as $key => $value) { |
69
|
|
|
if ('version' === $key) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
switch ($key) { |
74
|
|
|
case 'author': |
75
|
|
|
$feed->setAuthor($this->readAuthorNode($value)); |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
case 'hubs': |
79
|
|
|
$feed->setHubs(array_map([$this, 'readHubNode'], $value)); |
80
|
|
|
break; |
81
|
|
|
|
82
|
|
|
case 'items': |
83
|
|
|
$feed->setItems(array_map([$this, 'readItemNode'], $value)); |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
default: |
87
|
|
|
$this->accessor->setValue($feed, $key, $value); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $feed; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Browse item node |
96
|
|
|
* |
97
|
|
|
* @param array $content |
98
|
|
|
* @return Item |
99
|
|
|
*/ |
100
|
|
|
private function readItemNode(array $content) |
101
|
|
|
{ |
102
|
|
|
$id = isset($content['id']) ? $content['id'] : ''; |
103
|
|
|
|
104
|
|
|
$item = new Item($id); |
105
|
|
|
foreach ($content as $key => $value) { |
106
|
|
|
if ('id' === $key) { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
switch ($key) { |
111
|
|
|
case 'attachments': |
112
|
|
|
$item->setAttachments(array_map([$this, 'readAttachmentNode'], $value)); |
113
|
|
|
break; |
114
|
|
|
|
115
|
|
|
case 'author': |
116
|
|
|
$item->setAuthor($this->readAuthorNode($value)); |
117
|
|
|
break; |
118
|
|
|
|
119
|
|
|
case 'date_published': |
120
|
|
|
case 'date_modified': |
121
|
|
|
$this->accessor->setValue($item, $key, new DateTime($value)); |
122
|
|
|
break; |
123
|
|
|
|
124
|
|
|
default: |
125
|
|
|
$this->accessor->setValue($item, $key, $value); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $item; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Browse author node |
134
|
|
|
* |
135
|
|
|
* @param array $content |
136
|
|
|
* @return Author |
137
|
|
|
*/ |
138
|
|
|
private function readAuthorNode(array $content) |
139
|
|
|
{ |
140
|
|
|
$name = (isset($content['name'])) ? $content['name'] : ''; |
141
|
|
|
|
142
|
|
|
$author = new Author($name); |
143
|
|
|
foreach ($content as $key => $value) { |
144
|
|
|
if ('name' === $key) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->accessor->setValue($author, $key, $value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $author; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Browse hub node |
156
|
|
|
* |
157
|
|
|
* @param array $content |
158
|
|
|
* @return Hub |
159
|
|
|
*/ |
160
|
|
|
private function readHubNode(array $content) |
161
|
|
|
{ |
162
|
|
|
$type = isset($content['type']) ? $content['type'] : ''; |
163
|
|
|
$url = isset($content['url']) ? $content['url'] : ''; |
164
|
|
|
|
165
|
|
|
return new Hub($type, $url); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Browse attachment node |
170
|
|
|
* |
171
|
|
|
* @param array $content |
172
|
|
|
* @return Attachment |
173
|
|
|
*/ |
174
|
|
|
private function readAttachmentNode(array $content) |
175
|
|
|
{ |
176
|
|
|
$url = isset($content['url']) ? $content['url'] : ''; |
177
|
|
|
$mimeType = isset($content['mime_type']) ? $content['mime_type'] : ''; |
178
|
|
|
|
179
|
|
|
$attachment = new Attachment($url, $mimeType); |
180
|
|
|
foreach ($content as $key => $value) { |
181
|
|
|
switch ($key) { |
182
|
|
|
case 'size_in_bytes': |
183
|
|
|
$attachment->setSize($value); |
184
|
|
|
break; |
185
|
|
|
|
186
|
|
|
case 'duration_in_seconds': |
187
|
|
|
$attachment->setDuration($value); |
188
|
|
|
break; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $attachment; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|