Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualizacao da classe Standardize para ficar igual ao do projeto da NFE #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 67 additions & 24 deletions src/Common/Standardize.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class Standardize
{
private $xml = '';
/**
* @var string
*/
Expand All @@ -34,6 +35,11 @@ class Standardize
* @var string
*/
public $key = '';
/**
@var object
*/

private object $sxml;
/**
* @var array
*/
Expand Down Expand Up @@ -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;
}
}

/**
Expand All @@ -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)
Expand All @@ -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);
}

Expand All @@ -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();
}
}