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 | 29 | public function __construct(string $content) |
|
39 | |||
40 | /** |
||
41 | * @param $character |
||
42 | * @return bool |
||
43 | */ |
||
44 | 23 | public function startWith(string $character) : bool |
|
48 | |||
49 | /** |
||
50 | * @return bool |
||
51 | */ |
||
52 | 5 | public function isJson() : bool |
|
56 | |||
57 | /** |
||
58 | * @return bool |
||
59 | */ |
||
60 | 19 | public function isXml() : bool |
|
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 |
|
126 | } |
||
127 |