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\Parser; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use FeedIo\Feed\Item; |
15
|
|
|
use FeedIo\FeedInterface; |
16
|
|
|
use FeedIo\MissingFieldsException; |
|
|
|
|
17
|
|
|
use FeedIo\ParserAbstract; |
18
|
|
|
use FeedIo\Reader\Document; |
19
|
|
|
use FeedIo\Rule\Json\RuleSet; |
20
|
|
|
use FeedIo\Rule\Json\Title; |
21
|
|
|
|
22
|
|
|
class JsonParser extends ParserAbstract |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Document $document |
27
|
|
|
* @param FeedInterface $feed |
28
|
|
|
* @return FeedInterface |
29
|
|
|
*/ |
30
|
1 |
|
public function parseContent(Document $document, FeedInterface $feed) |
31
|
|
|
{ |
32
|
1 |
|
$data = $document->getJsonAsArray(); |
33
|
1 |
|
$feed->setTitle($this->readOffset($data, 'title')); |
34
|
1 |
|
$feed->setDescription($this->readOffset($data, 'description')); |
35
|
1 |
|
$feed->setLink($this->readOffset($data, 'feed_url')); |
36
|
1 |
|
$feed->setUrl($this->readOffset($data, 'home_page_url')); |
37
|
|
|
|
38
|
1 |
|
if ( array_key_exists('items', $data) ) { |
39
|
1 |
|
$this->parseItems($data['items'], $feed); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
return $feed; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function checkBodyStructure(Document $document, array $mandatoryFields) |
46
|
|
|
{ |
47
|
|
|
// TODO: Implement checkBodyStructure() method. |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $items |
52
|
|
|
* @param FeedInterface $feed |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
1 |
|
public function parseItems(array $items, FeedInterface $feed) |
56
|
|
|
{ |
57
|
1 |
|
foreach( $items as $dataItem ) { |
58
|
1 |
|
$item = new Item(); |
59
|
1 |
|
$item->setPublicId($this->readOffset($dataItem, 'id')); |
60
|
1 |
|
$item->setTitle($this->readOffset($dataItem, 'title')); |
61
|
1 |
|
$item->setLastModified(new \DateTime($this->readOffset($dataItem, 'date_published'))); |
62
|
1 |
|
$contentHtml = $this->readOffset($dataItem, 'content_html'); |
63
|
1 |
|
$item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml)); |
64
|
1 |
|
$feed->add($item); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $data |
72
|
|
|
* @param string $offsetName |
73
|
|
|
* @param mixed $default |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
1 |
|
public function readOffset(array $data, $offsetName, $default = null) |
77
|
|
|
{ |
78
|
1 |
|
if (array_key_exists($offsetName, $data) ){ |
79
|
1 |
|
return $data[$offsetName]; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return $default; |
83
|
|
|
} |
84
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: