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