diff --git a/src/Common/Standardize.php b/src/Common/Standardize.php index 3c2aaed..966a1f5 100644 --- a/src/Common/Standardize.php +++ b/src/Common/Standardize.php @@ -22,6 +22,7 @@ class Standardize { + private $xml = ''; /** * @var string */ @@ -34,6 +35,11 @@ class Standardize * @var string */ public $key = ''; + /** + @var object + */ + + private object $sxml; /** * @var array */ @@ -61,9 +67,11 @@ class Standardize * Constructor * @param string $xml */ - public function __construct($xml = null) + public function __construct(?string $xml = null) { - $this->toStd($xml); + if (!empty($xml)) { + $this->xml = $xml; + } } /** @@ -72,16 +80,22 @@ public function __construct($xml = null) * @return string identificated node name * @throws InvalidArgumentException */ - public function whichIs($xml) + public function whichIs(?string $xml = null): string { - if (!Validator::isXML($xml)) { + if (empty($xml) && empty($this->xml)) { + throw new DocumentsException("O XML está vazio."); + } + if (!empty($xml)) { + $this->xml = $xml; + } + if (!Validator::isXML($this->xml)) { //invalid document is not a XML throw DocumentsException::wrongDocument(6); } $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->preserveWhiteSpace = false; $dom->formatOutput = false; - $dom->loadXML($xml); + $dom->loadXML($this->xml); foreach ($this->rootTagList as $key) { $node = !empty($dom->getElementsByTagName($key)->item(0)) ? $dom->getElementsByTagName($key)->item(0) @@ -91,7 +105,7 @@ public function whichIs($xml) return $key; } } - //documento does not belong to the SPED-MDFe project + //documento does not belong to the SPED-NFe project throw DocumentsException::wrongDocument(7); } @@ -109,44 +123,73 @@ public function __toString() * @param string $xml * @return stdClass */ - public function toStd($xml = null) + public function toStd(?string $xml = null): stdClass { + if (empty($xml) && empty($this->xml)) { + throw new DocumentsException("O XML está vazio."); + } if (!empty($xml)) { - $this->key = $this->whichIs($xml); + $this->xml = $xml; } - - $sxml = simplexml_load_string($this->node); + $this->key = $this->whichIs(); + $this->sxml = simplexml_load_string($this->node); $this->json = str_replace( '@attributes', 'attributes', - json_encode($sxml, JSON_PRETTY_PRINT) + json_encode($this->sxml, JSON_PRETTY_PRINT) ); - return json_decode($this->json); - } + $std = json_decode($this->json); + return $std; + } /** - * Retruns JSON string form XML - * @param string $xml + * Returns the SimpleXml Object + * @param string|null $xml + * @return object + * @throws DocumentsException + */ + public function simpleXml(?string $xml = null): object + { + $this->checkXml($xml); + return $this->sxml; + } + /** + * Returns JSON string form XML + * @param string|null $xml * @return string + * @throws DocumentsException */ - public function toJson($xml = null) + public function toJson(?string $xml = null): string { - if (!empty($xml)) { - $this->toStd($xml); - } + $this->checkXml($xml); return $this->json; } - /** * Returns array from XML - * @param string $xml + * @param string|null $xml * @return array + * @throws DocumentsException + */ + public function toArray(?string $xml = null): array + { + $this->checkXml($xml); + return json_decode($this->json, true); + } + + /** + * Check and load XML + * @param string|null $xml + * @return void + * @throws DocumentsException */ - public function toArray($xml = null) + private function checkXml(?string $xml = null) { + if (empty($xml) && empty($this->xml)) { + throw new DocumentsException("O XML está vazio."); + } if (!empty($xml)) { - $this->toStd($xml); + $this->xml = $xml; } - return json_decode($this->json, true); + $this->toStd(); } }