|
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\Reader; |
|
12
|
|
|
|
|
13
|
|
|
class Document |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $content; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \DOMDocument |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $domDocument; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $jsonArray; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Document constructor. |
|
33
|
|
|
* @param string $content |
|
34
|
|
|
*/ |
|
35
|
40 |
|
public function __construct(string $content) |
|
36
|
|
|
{ |
|
37
|
40 |
|
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/'; |
|
38
|
40 |
|
$content = preg_replace($invalid_characters, '', $content); |
|
39
|
40 |
|
$this->content = trim(str_replace("\xEF\xBB\xBF", '', $content)); |
|
40
|
40 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $character |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
27 |
|
public function startWith(string $character) : bool |
|
47
|
|
|
{ |
|
48
|
27 |
|
return mb_substr($this->content, 0, 1) === $character; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public function isJson() : bool |
|
55
|
|
|
{ |
|
56
|
5 |
|
return $this->startWith('{'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
23 |
|
public function isXml() : bool |
|
63
|
|
|
{ |
|
64
|
23 |
|
return $this->startWith('<'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return \DOMDocument |
|
69
|
|
|
*/ |
|
70
|
22 |
|
public function getDOMDocument() : \DOMDocument |
|
71
|
|
|
{ |
|
72
|
22 |
|
if (is_null($this->domDocument)) { |
|
73
|
22 |
|
$this->domDocument = $this->loadDomDocument(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
21 |
|
return $this->domDocument; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function getJsonAsArray() : array |
|
83
|
|
|
{ |
|
84
|
3 |
|
if (is_null($this->jsonArray)) { |
|
85
|
3 |
|
$this->jsonArray = $this->loadJsonAsArray(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $this->jsonArray; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return \DOMDocument |
|
93
|
|
|
*/ |
|
94
|
22 |
|
protected function loadDomDocument() : \DOMDocument |
|
95
|
|
|
{ |
|
96
|
22 |
|
if (! $this->isXml()) { |
|
97
|
1 |
|
throw new \LogicException('this document is not a XML stream'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
21 |
|
set_error_handler( |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $errno |
|
104
|
|
|
*/ |
|
105
|
21 |
|
function ($errno, $errstr) { |
|
106
|
|
|
throw new \InvalidArgumentException("malformed xml string. parsing error : $errstr ($errno)"); |
|
107
|
21 |
|
} |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
21 |
|
$domDocument = new \DOMDocument(); |
|
111
|
21 |
|
$domDocument->loadXML($this->content); |
|
112
|
21 |
|
restore_error_handler(); |
|
113
|
|
|
|
|
114
|
21 |
|
return $domDocument; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
3 |
|
protected function loadJsonAsArray() : array |
|
121
|
|
|
{ |
|
122
|
3 |
|
if (! $this->isJson()) { |
|
123
|
1 |
|
throw new \LogicException('this document is not a JSON stream'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
return json_decode($this->content, true); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|