1 | <?php declare(strict_types=1); |
||
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 | 32 | public function __construct(string $content) |
|
41 | |||
42 | /** |
||
43 | * @param $character |
||
44 | * @return bool |
||
45 | */ |
||
46 | 26 | public function startWith(string $character) : bool |
|
50 | |||
51 | /** |
||
52 | * @return bool |
||
53 | */ |
||
54 | 5 | public function isJson() : bool |
|
58 | |||
59 | /** |
||
60 | * @return bool |
||
61 | */ |
||
62 | 22 | public function isXml() : bool |
|
66 | |||
67 | /** |
||
68 | * @return \DOMDocument |
||
69 | */ |
||
70 | 21 | public function getDOMDocument() : \DOMDocument |
|
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | 3 | public function getJsonAsArray() : array |
|
90 | |||
91 | /** |
||
92 | * @return \DOMDocument |
||
93 | */ |
||
94 | 21 | protected function loadDomDocument() : \DOMDocument |
|
95 | { |
||
96 | 21 | if (! $this->isXml()) { |
|
97 | 1 | throw new \LogicException('this document is not a XML stream'); |
|
98 | } |
||
99 | |||
100 | 20 | set_error_handler( |
|
101 | |||
102 | /** |
||
103 | * @param string $errno |
||
104 | */ |
||
105 | function ($errno, $errstr) { |
||
106 | throw new \InvalidArgumentException("malformed xml string. parsing error : $errstr ($errno)"); |
||
107 | 20 | } |
|
108 | ); |
||
109 | |||
110 | 20 | $domDocument = new \DOMDocument(); |
|
111 | 20 | $domDocument->loadXML($this->content); |
|
112 | 20 | restore_error_handler(); |
|
113 | |||
114 | 20 | return $domDocument; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return array |
||
119 | */ |
||
120 | 3 | protected function loadJsonAsArray() : array |
|
128 | } |
||
129 |