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