diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c70af76 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,25 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{md,htm,html,blade.php}] +insert_final_newline = false +trim_trailing_whitespace = false + +[*.{min.css,min.js}] +insert_final_newline = false + +[*.{yml,yaml}] +indent_size = 2 + +[docker-compose.yml] +indent_size = 4 diff --git a/.gitattributes b/.gitattributes index dfe0770..ba7b5d2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,11 @@ # Auto detect text files and perform LF normalization * text=auto + +/.github export-ignore +/tests export-ignore +.editorconfig export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.php-cs-fixer.dist.php export-ignore +.travis.yml export-ignore +phpunit.xml export-ignore diff --git a/.gitignore b/.gitignore index 61220af..9b50ea1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ /vendor -composer.phar -composer.lock +.phpunit.result.cache .php_cs.cache +.php-cs-fixer.cache .DS_Store +composer.phar +composer.lock diff --git a/README.md b/README.md index 1cc2ff3..9091146 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ [![Build Status](https://api.travis-ci.org/JackieDo/Xml-Array.svg?branch=master)](https://travis-ci.org/JackieDo/Xml-Array) [![Total Downloads](https://poser.pugx.org/jackiedo/xml-array/downloads)](https://packagist.org/packages/jackiedo/xml-array) [![Latest Stable Version](https://poser.pugx.org/jackiedo/xml-array/v/stable)](https://packagist.org/packages/jackiedo/xml-array) -[![Latest Unstable Version](https://poser.pugx.org/jackiedo/xml-array/v/unstable)](https://packagist.org/packages/jackiedo/xml-array) [![License](https://poser.pugx.org/jackiedo/xml-array/license)](https://packagist.org/packages/jackiedo/xml-array) The conversion between xml and array becomes easier than ever. This package provides some very simple classes to convert XML to array and back. @@ -15,30 +14,38 @@ The conversion between xml and array becomes easier than ever. This package prov # Overview Look at one of the following sessions to learn more about this package. -* [Installation](#installation) -* [Basic usage](#basic-usage) +- [Xml-Array](#xml-array) +- [Features of this package](#features-of-this-package) +- [Overview](#overview) + - [Installation](#installation) + - [Basic usage](#basic-usage) - [Convert XML to array](#convert-xml-to-array) - [Convert XML to Json](#convert-xml-to-json) - [Convert array to XML](#convert-array-to-xml) - [Convert array to DOM](#convert-array-to-dom) -* [Advanced usage](#advanced-usage) + - [Advanced usage](#advanced-usage) - [Set configuration](#set-configuration) + - [Method 1](#method-1) + - [Method 2](#method-2) + - [Method 3](#method-3) - [Get configuration](#get-configuration) - [Default configuration](#default-configuration) -* [License](#license) + - [For Xml2Array](#for-xml2array) + - [For Array2Xml](#for-array2xml) +- [License](#license) ## Installation You can install this package through [Composer](https://getcomposer.org). ```shell -composer require jackiedo/xml-array +$ composer require jackiedo/xml-array ``` ## Basic usage ### Convert XML to array -###### Syntax: +**Syntax**: ``` array Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toArray(); @@ -46,7 +53,7 @@ array Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toArray > **Note:** The input XML can be one of types DOMDocument object, SimpleXMLElement object or well-formed XML string. -###### Example 1 (Convert from XML string): +**Example 1** - _(Convert from XML string)_: ```php use Jackiedo\XmlArray\Xml2Array; @@ -135,7 +142,7 @@ $array = [ ] ``` -###### Example 2 (Convert form XML object, such as SimpleXMLElement): +**Example 2** - _(Convert form XML object, such as SimpleXMLElement)_: ```php use Jackiedo\XmlArray\Xml2Array; @@ -331,13 +338,13 @@ $array = [ ### Convert XML to Json -###### Syntax: +**Syntax**: ``` string Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toJson([int $options = 0]); ``` -###### Example 3: +**Example 3**: ```php $jsonString = Xml2Array::convert($xmlString)->toJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); @@ -345,13 +352,13 @@ $jsonString = Xml2Array::convert($xmlString)->toJson(JSON_UNESCAPED_UNICODE | JS ### Convert array to XML -###### Syntax: +**Syntax**: ``` string Array2Xml::convert(array $array)->toXml([bool $prettyOutput = false]); ``` -###### Example 4: +**Example 4**: ```php use Jackiedo\XmlArray\Array2Xml; @@ -363,13 +370,13 @@ $xmlString = Array2Xml::convert($array)->toXml(true); ### Convert array to DOM -###### Syntax: +**Syntax**: ``` DOMDocument Array2Xml::convert(array $array)->toDom(); ``` -###### Example 5: +**Example 5**: ```php $domObject = Array2Xml::convert($array)->toDom(); @@ -380,7 +387,7 @@ $domObject = Array2Xml::convert($array)->toDom(); ### Set configuration You can set configuration for conversion process with one of following methods: -###### Method 1: +#### Method 1 ```php ... @@ -398,14 +405,14 @@ $xml = Array2Xml::convert($inputArray, $config)->toXml(); > **Note**: Configuration is an array of parameters. For more details, see section [Default configuration](#default-configuration). -###### Method 2: +#### Method 2 ```php $converter = new Xml2Array($config); $array = $converter->convertFrom($inputXml)->toArray(); ``` -###### Method 3: +#### Method 3 ```php $converter = new Xml2Array; @@ -421,7 +428,7 @@ $config = $converter->getConfig(); ### Default configuration -###### For Xml2Array +#### For Xml2Array ```php $defaultConfig = [ @@ -434,7 +441,7 @@ $defaultConfig = [ ]; ``` -###### For Array2Xml +#### For Array2Xml ```php $defaultConfig = [ @@ -447,5 +454,5 @@ $defaultConfig = [ ]; ``` -## License +# License [MIT](LICENSE) © Jackie Do diff --git a/composer.json b/composer.json index 39aea89..fb2ad52 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ }, "autoload": { "psr-4": { - "Jackiedo\\XmlArray\\": "src" + "Jackiedo\\XmlArray\\": "src/" } }, "minimum-stability": "stable" diff --git a/src/Array2Xml.php b/src/Array2Xml.php index 5d31a32..e8857e9 100644 --- a/src/Array2Xml.php +++ b/src/Array2Xml.php @@ -1,42 +1,37 @@ -toXml(); - * $xml = Array2Xml::convert($array, ['rootElement' => 'root_node'])->toXml(); - * $dom = Array2Xml::convert($array)->toDom(); + * @see https://github.com/JackieDo/Xml-Array/blob/master/README.md Documentation. * - * @package xml-array * @author Jackie Do - * @copyright 2018 - * @version $Id$ - * @access public + * @license MIT */ class Array2Xml { /** - * The configuration of the conversion + * The configuration of the conversion. * * @var array */ protected $config = []; /** - * The working XML document + * The working XML document. * * @var DOMDocument */ - protected $xml = null; + protected $xml; /** - * Constructor + * Constructor. * * @param array $config The configuration to use for this instance */ @@ -48,9 +43,9 @@ public function __construct(array $config = []) } /** - * Set configuration for converter + * Set configuration for converter. * - * @param array $config The configuration to use for conversion + * @param array $config The configuration to use for conversion * * @return $this */ @@ -71,7 +66,7 @@ public function setConfig(array $config = []) } /** - * Return configuration of converter + * Return configuration of converter. * * @return array */ @@ -82,12 +77,12 @@ public function getConfig() /** * Convert an array to an XML document - * A static facade for ease of use and backwards compatibility + * A static facade for ease of use and backwards compatibility. * - * @param array $array The input array - * @param array $config The configuration to use for the conversion + * @param array $array The input array + * @param array $config The configuration to use for the conversion * - * @return DOMDocument The XML representation of the input array + * @return $this */ public static function convert(array $array = [], array $config = []) { @@ -97,9 +92,11 @@ public static function convert(array $array = [], array $config = []) } /** - * Convert an array to an XML document + * Convert an array to an XML document. * - * @param array $array The input array + * @param array $array The input array + * + * @throws DOMException * * @return $this */ @@ -107,7 +104,7 @@ public function convertFrom(array $array = []) { if (isset($this->config['rootElement'])) { $rootElement = $this->config['rootElement']; - } elseif (count($array) == 1) { + } elseif (1 == count($array)) { $rootElement = array_keys($array)[0]; $array = $array[$rootElement]; } else { @@ -122,7 +119,7 @@ public function convertFrom(array $array = []) /** * Return as XML. * - * @param $prettyOutput Format output for DOMDocument + * @param $prettyOutput Format output for DOMDocument * * @return string */ @@ -144,14 +141,14 @@ public function toDom() } /** - * Build XML node + * Build XML node. * - * @param string $nodeName The name of the node that the data will be stored under - * @param mixed $data The value to be build + * @param string $nodeName The name of the node that the data will be stored under + * @param mixed $data The value to be build * * @throws DOMException * - * @return DOMElement The XML representation of the input data + * @return DOMElement The XML representation of the input data */ protected function buildNode($nodeName, $data) { @@ -162,7 +159,7 @@ protected function buildNode($nodeName, $data) $node = $this->xml->createElement($nodeName); if (is_array($data)) { - $this->parseArray($node, $nodeName, $data); + $this->parseArray($node, $data); } else { $node->appendChild($this->xml->createTextNode($this->normalizeValues($data))); } @@ -171,20 +168,19 @@ protected function buildNode($nodeName, $data) } /** - * Parse array to build node + * Parse array to build node. * - * @param DOMElement $node - * @param string $nodeName - * @param array $array + * @param DOMElement $node + * @param array $array * * @throws DOMException * * @return void */ - protected function parseArray(DOMElement $node, $nodeName, array $array) + protected function parseArray(DOMElement $node, array $array) { // get the attributes first.; - $array = $this->parseAttributes($node, $nodeName, $array); + $array = $this->parseAttributes($node, $array); // get value stored in @value $array = $this->parseValue($node, $array); @@ -215,17 +211,16 @@ protected function parseArray(DOMElement $node, $nodeName, array $array) } /** - * Build attributes of node + * Build attributes of node. * - * @param DOMElement $node - * @param string $nodeName - * @param array $array + * @param DOMElement $node + * @param array $array * * @throws DOMException * * @return array */ - protected function parseAttributes(DOMElement $node, $nodeName, array $array) + protected function parseAttributes(DOMElement $node, array $array) { $attributesKey = $this->config['attributesKey']; @@ -245,12 +240,12 @@ protected function parseAttributes(DOMElement $node, $nodeName, array $array) } /** - * Build value of node + * Build value of node. * - * @param DOMElement $node - * @param array $array + * @param DOMElement $node + * @param array $array * - * @return DOMElement|array + * @return array */ protected function parseValue(DOMElement $node, array $array) { @@ -266,12 +261,12 @@ protected function parseValue(DOMElement $node, array $array) } /** - * Build CDATA of node + * Build CDATA of node. * - * @param DOMElement $node - * @param array $array + * @param DOMElement $node + * @param array $array * - * @return DOMElement|array + * @return array */ protected function parseCdata(DOMElement $node, array $array) { @@ -287,35 +282,35 @@ protected function parseCdata(DOMElement $node, array $array) } /** - * Get string representation of values + * Get string representation of values. * - * @param mixed $value + * @param mixed $value * * @return string */ protected function normalizeValues($value) { - $value = $value === true ? 'true' : $value; - $value = $value === false ? 'false' : $value; - $value = $value === null ? '' : $value; + $value = true === $value ? 'true' : $value; + $value = false === $value ? 'false' : $value; + $value = null === $value ? '' : $value; return (string) $value; } /** - * Check if the tag name or attribute name contains illegal characters + * Check if the tag name or attribute name contains illegal characters. + * * @see: http://www.w3.org/TR/xml/#sec-common-syn * * @param string + * @param mixed $tag * - * @return boolean + * @return bool */ protected function isValidTagName($tag) { $pattern = '/^[a-zA-Z_][\w\:\-\.]*$/'; - return preg_match($pattern, $tag, $matches) && $matches[0] == $tag && substr($tag, -1, 1) != ':'; + return preg_match($pattern, $tag, $matches) && $matches[0] == $tag && ':' != substr($tag, -1, 1); } - } - diff --git a/src/Xml2Array.php b/src/Xml2Array.php index ce36f25..bc2ed83 100644 --- a/src/Xml2Array.php +++ b/src/Xml2Array.php @@ -1,4 +1,6 @@ -toArray(); - * $array = Xml2Array::convert($xml, ['useNamespaces' => true])->toArray(); - * $json = Xml2Array::convert($xml)->toJson(); + * @see https://github.com/JackieDo/Xml-Array/blob/master/README.md Documentation. * - * @package xml-array * @author Jackie Do - * @copyright 2018 - * @version $Id$ - * @access public + * @license MIT */ class Xml2Array { /** - * The name of the XML attribute that indicates a namespace definition + * The name of the XML attribute that indicates a namespace definition. */ - const ATTRIBUTE_NAMESPACE = 'xmlns'; + public const ATTRIBUTE_NAMESPACE = 'xmlns'; /** - * The string that separates the namespace attribute from the prefix for the namespace + * The string that separates the namespace attribute from the prefix for the namespace. */ - const ATTRIBUTE_NAMESPACE_SEPARATOR = ':'; + public const ATTRIBUTE_NAMESPACE_SEPARATOR = ':'; /** - * The configuration of the current instance + * The configuration of the current instance. * * @var array */ protected $config = []; /** - * The working XML document + * The working XML document. * * @var DOMDocument */ - protected $xml = null; + protected $xml; /** - * The working list of XML namespaces + * The working list of XML namespaces. * * @var array */ protected $namespaces = []; /** - * The result of this conversion + * The result of this conversion. * * @var array */ protected $array = []; /** - * Constructor + * Constructor. * * @param array $config The configuration to use for this instance */ @@ -71,9 +67,9 @@ public function __construct(array $config = []) } /** - * Set configuration for converter + * Set configuration for converter. * - * @param array $config The configuration to use for conversion + * @param array $config The configuration to use for conversion * * @return $this */ @@ -85,7 +81,7 @@ public function setConfig(array $config = []) 'attributesKey' => '@attributes', 'cdataKey' => '@cdata', 'valueKey' => '@value', - 'namespacesOnRoot' => true + 'namespacesOnRoot' => true, ]; $this->config = array_merge($defaultConfig, $config); @@ -94,7 +90,7 @@ public function setConfig(array $config = []) } /** - * Return configuration of converter + * Return configuration of converter. * * @return array */ @@ -105,12 +101,12 @@ public function getConfig() /** * Convert an XML DOMDocument or XML string to an array - * A static facade for ease of use and backwards compatibility + * A static facade for ease of use and backwards compatibility. * - * @param DOMDocument|SimpleXMLElement|string $xml The XML to convert to an array - * @param array $config The configuration to use for the conversion + * @param DOMDocument|SimpleXMLElement|string $xml The XML to convert to an array + * @param array $config The configuration to use for the conversion * - * @return array An array representation of the input XML + * @return array An array representation of the input XML */ public static function convert($xml, array $config = []) { @@ -120,11 +116,11 @@ public static function convert($xml, array $config = []) } /** - * Convert input XML to an array + * Convert input XML to an array. * - * @param DOMDocument|SimpleXMLElement|string $inputXml The XML to convert to an array + * @param DOMDocument|SimpleXMLElement|string $inputXml The XML to convert to an array * - * @return array An array representation of the input XML + * @return array An array representation of the input XML */ public function convertFrom($inputXml) { @@ -156,7 +152,7 @@ public function convertFrom($inputXml) } /** - * Export result as array + * Export result as array. * * @return array */ @@ -166,21 +162,21 @@ public function toArray() } /** - * Get result as json string + * Get result as json string. * - * @param integer $options + * @param int $flags * * @return string */ - public function toJson($options = 0) + public function toJson($flags = 0) { - return json_encode($this->array, $options); + return json_encode($this->array, $flags); } /** - * Load input into DOMDocument + * Load input into DOMDocument. * - * @param DOMDocument|SimpleXMLElement|string $inputXml The XML to convert to an array + * @param DOMDocument|SimpleXMLElement|string $inputXml The XML to convert to an array * * @throws DOMException * @@ -193,7 +189,7 @@ protected function loadXml($inputXml) if (is_string($inputXml)) { $parse = @$this->xml->loadXML($inputXml); - if ($parse === false) { + if (false === $parse) { throw new DOMException('Error parsing XML string, input is not a well-formed XML string.'); } } elseif ($inputXml instanceof SimpleXMLElement) { @@ -206,9 +202,9 @@ protected function loadXml($inputXml) } /** - * Parse an XML DOMNode + * Parse an XML DOMNode. * - * @param DOMNode $node A single XML DOMNode + * @param DOMNode $node A single XML DOMNode * * @return mixed */ @@ -237,17 +233,17 @@ protected function parseNode(DOMNode $node) } /** - * Parse child nodes of DOMNode + * Parse child nodes of DOMNode. * - * @param DOMNode $node - * @param mixed $output + * @param DOMNode $node + * @param mixed $output * * @return mxied */ protected function parseChildNodes(DOMNode $node, $output) { foreach ($node->childNodes as $child) { - if ($child->nodeType === XML_CDATA_SECTION_NODE) { + if (XML_CDATA_SECTION_NODE === $child->nodeType) { if (!is_array($output)) { if (!empty($output)) { $output = [$this->config['valueKey'] => $output]; @@ -260,15 +256,15 @@ protected function parseChildNodes(DOMNode $node, $output) } else { $value = $this->parseNode($child); - if ($child->nodeType == XML_TEXT_NODE) { - if ($value != '') { + if (XML_TEXT_NODE == $child->nodeType) { + if ('' != $value) { if (!empty($output)) { $output[$this->config['valueKey']] = $value; } else { $output = $value; } } - } elseif ($child->nodeType !== XML_COMMENT_NODE) { + } elseif (XML_COMMENT_NODE !== $child->nodeType) { $nodeName = $child->nodeName; if (!isset($output[$nodeName])) { @@ -284,9 +280,9 @@ protected function parseChildNodes(DOMNode $node, $output) } /** - * Clean text content of text node + * Clean text content of text node. * - * @param string $textContent + * @param string $textContent * * @return string */ @@ -296,14 +292,14 @@ protected function normalizeTextContent($textContent) '/\n+\s+/', '/\r+\s+/', '/\n+\t+/', - '/\r+\t+/' + '/\r+\t+/', ], ' ', $textContent)); } /** - * Normalize values of node + * Normalize values of node. * - * @param mixed $values + * @param mixed $values * * @return mixed */ @@ -312,7 +308,7 @@ protected function normalizeNodeValues($values) if (is_array($values)) { // if only one node of its kind, assign it directly instead if array($value); foreach ($values as $key => $value) { - if (is_array($value) && count($value) === 1) { + if (is_array($value) && 1 === count($value)) { $keyName = array_keys($value)[0]; if (is_numeric($keyName)) { @@ -330,10 +326,10 @@ protected function normalizeNodeValues($values) } /** - * Parse DOMNode to get its attributes + * Parse DOMNode to get its attributes. * - * @param DOMNode $node - * @param mixed $output + * @param DOMNode $node + * @param mixed $output * * @return mixed */ @@ -375,10 +371,10 @@ protected function collectAttributes(DOMNode $node, $output) } /** - * Collect namespaces for special DOMNode + * Collect namespaces for special DOMNode. * - * @param DOMNode $node - * @param array $output + * @param DOMNode $node + * @param array $output * * @return array */ @@ -394,9 +390,9 @@ protected function collectNodeNamespaces(DOMNode $node, array $output) } /** - * Get the namespace of the supplied node, and add it to the list of known namespaces for this document + * Get the namespace of the supplied node, and add it to the list of known namespaces for this document. * - * @param DOMNode $node + * @param DOMNode $node * * @return mixed */ diff --git a/tests/.gitkeep b/tests/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Array2XmlTest.php b/tests/Array2XmlTest.php index eb487b0..136a245 100644 --- a/tests/Array2XmlTest.php +++ b/tests/Array2XmlTest.php @@ -3,67 +3,70 @@ use Jackiedo\XmlArray\Array2Xml; use PHPUnit\Framework\TestCase; +/** + * @internal + * @coversNothing + */ class Array2XmlTest extends TestCase { /** - * Input for test + * Input for test. * * @var array */ protected $input_array = [ - "root_node" => [ - "tag" => "Example tag", - "attribute_tag" => [ - "@value" => "Another tag with attributes", - "@attributes" => [ - "description" => "This is a tag with attribute" - ] + 'root_node' => [ + 'tag' => 'Example tag', + 'attribute_tag' => [ + '@value' => 'Another tag with attributes', + '@attributes' => [ + 'description' => 'This is a tag with attribute', + ], ], - "cdata_section" => [ - "@cdata" => "This is CDATA section" + 'cdata_section' => [ + '@cdata' => 'This is CDATA section', ], - "tag_with_subtag" => [ - "sub_tag" => ["Sub tag 1", "Sub tag 2"] + 'tag_with_subtag' => [ + 'sub_tag' => ['Sub tag 1', 'Sub tag 2'], ], - "mixed_section" => [ - "@value" => "Hello", - "@cdata" => "This is another CDATA section", - "section" => [ + 'mixed_section' => [ + '@value' => 'Hello', + '@cdata' => 'This is another CDATA section', + 'section' => [ [ - "@value" => "Section number 1", - "@attributes" => [ - "id" => "sec_1" - ] + '@value' => 'Section number 1', + '@attributes' => [ + 'id' => 'sec_1', + ], ], [ - "@value" => "Section number 2", - "@attributes" => [ - "id" => "sec_2" - ] + '@value' => 'Section number 2', + '@attributes' => [ + 'id' => 'sec_2', + ], ], [ - "@value" => "Section number 3", - "@attributes" => [ - "id" => "sec_3" - ] - ] - ] + '@value' => 'Section number 3', + '@attributes' => [ + 'id' => 'sec_3', + ], + ], + ], ], - "example:with_namespace" => [ - "@attributes" => [ - "xmlns:example" => "http://example.com" + 'example:with_namespace' => [ + '@attributes' => [ + 'xmlns:example' => 'http://example.com', ], - "example:sub" => "Content" + 'example:sub' => 'Content', ], - ] + ], ]; /** - * Throw DOMException when there are more than one root node + * Throw DOMException when there are more than one root node. * + * @testdox Throw DOMException when there are more than one root node. * @test - * - * @return void */ public function throw_dom_exception_when_there_are_more_than_one_root_node() { @@ -71,17 +74,16 @@ public function throw_dom_exception_when_there_are_more_than_one_root_node() $this->expectExceptionMessage('XML documents are allowed only one root element. Wrap your elements in a key or set the `rootElement` parameter in the configuration.'); $process = Array2Xml::convert([ - 'root' => 'content', - 'another_root' => 'Another content' + 'root' => 'content', + 'another_root' => 'Another content', ]); } /** - * Throw DOMException when node name is invalid + * Throw DOMException when node name is invalid. * + * @testdox Throw DOMException when node name is invalid. * @test - * - * @return void */ public function throw_dom_exception_when_node_name_is_invalid() { @@ -92,11 +94,10 @@ public function throw_dom_exception_when_node_name_is_invalid() } /** - * Throw DOMException when attribute name is invalid + * Throw DOMException when attribute name is invalid. * + * @testdox Throw DOMException when attribute name is invalid. * @test - * - * @return void */ public function throw_dom_exception_when_attaribute_name_is_invalid() { @@ -107,19 +108,18 @@ public function throw_dom_exception_when_attaribute_name_is_invalid() 'root' => [ 'sub' => [ '@attributes' => [ - 'invalid attribute' => 'Attribute value' - ] - ] - ] + 'invalid attribute' => 'Attribute value', + ], + ], + ], ]); } /** - * Convert array to XML string + * Convert array to XML string. * + * @testdox Convert array to XML string. * @test - * - * @return void */ public function convert_array_to_xml_string() { @@ -127,11 +127,10 @@ public function convert_array_to_xml_string() } /** - * Convert array to DOM + * Convert array to DOM. * + * @testdox Convert array to DOM. * @test - * - * @return void */ public function convert_array_to_dom() { diff --git a/tests/Xml2ArrayTest.php b/tests/Xml2ArrayTest.php index ff62907..6909030 100644 --- a/tests/Xml2ArrayTest.php +++ b/tests/Xml2ArrayTest.php @@ -3,115 +3,115 @@ use Jackiedo\XmlArray\Xml2Array; use PHPUnit\Framework\TestCase; +/** + * @internal + * @coversNothing + */ class Xml2ArrayTest extends TestCase { /** - * Store expected result for full test + * Store expected result for full test. * * @var string */ protected $fulltest_expected_result = [ - "root_node" => [ - "tag" => "Example tag", - "attribute_tag" => [ - "@value" => "Another tag with attributes", - "@attributes" => [ - "description" => "This is a tag with attribute" - ] + 'root_node' => [ + 'tag' => 'Example tag', + 'attribute_tag' => [ + '@value' => 'Another tag with attributes', + '@attributes' => [ + 'description' => 'This is a tag with attribute', + ], ], - "cdata_section" => [ - "@cdata" => "This is CDATA section" + 'cdata_section' => [ + '@cdata' => 'This is CDATA section', ], - "tag_with_subtag" => [ - "sub_tag" => ["Sub tag 1", "Sub tag 2"] + 'tag_with_subtag' => [ + 'sub_tag' => ['Sub tag 1', 'Sub tag 2'], ], - "mixed_section" => [ - "@value" => "Hello", - "@cdata" => "This is another CDATA section", - "section" => [ + 'mixed_section' => [ + '@value' => 'Hello', + '@cdata' => 'This is another CDATA section', + 'section' => [ [ - "@value" => "Section number 1", - "@attributes" => [ - "id" => "sec_1" - ] + '@value' => 'Section number 1', + '@attributes' => [ + 'id' => 'sec_1', + ], ], [ - "@value" => "Section number 2", - "@attributes" => [ - "id" => "sec_2" - ] + '@value' => 'Section number 2', + '@attributes' => [ + 'id' => 'sec_2', + ], ], [ - "@value" => "Section number 3", - "@attributes" => [ - "id" => "sec_3" - ] - ] - ] + '@value' => 'Section number 3', + '@attributes' => [ + 'id' => 'sec_3', + ], + ], + ], ], - "example:with_namespace" => [ - "@attributes" => [ - "xmlns:example" => "http://example.com" + 'example:with_namespace' => [ + '@attributes' => [ + 'xmlns:example' => 'http://example.com', ], - "example:sub" => "Content" + 'example:sub' => 'Content', ], - ] + ], ]; /** - * Convert to array from an XML string containing only one empty node + * Convert to array from an XML string containing only one empty node. * + * @testdox Convert to array from an XML string containing only one empty node. * @test - * - * @return void */ public function convert_to_array_from_an_xml_string_containing_only_one_empty_node() { $string = ''; $this->assertEquals([ - 'root' => '' + 'root' => '', ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from an XML string containing one node with empty value + * Convert to array from an XML string containing one node with empty value. * + * @testdox Convert to array from an XML string containing one node with empty value. * @test - * - * @return void */ public function convert_to_array_from_an_xml_string_containing_one_node_with_empty_value() { $string = ''; $this->assertEquals([ - 'root' => '' + 'root' => '', ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string containing one node that whose value is on one line + * Convert to array from XML string containing one node that whose value is on one line. * + * @testdox Convert to array from XML string containing one node that whose value is on one line. * @test - * - * @return void */ public function convert_to_array_from_xml_string_containing_one_node_that_whose_value_is_on_one_line() { $string = 'Welcome to Xml2Array Converter'; $this->assertEquals([ - 'root' => 'Welcome to Xml2Array Converter' + 'root' => 'Welcome to Xml2Array Converter', ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string containing one node that whose value is on multilines + * Convert to array from XML string containing one node that whose value is on multilines. * + * @testdox Convert to array from XML string containing one node that whose value is on multilines. * @test - * - * @return void */ public function convert_to_array_from_xml_string_containing_one_node_that_whose_value_is_on_multilines() { @@ -123,16 +123,15 @@ public function convert_to_array_from_xml_string_containing_one_node_that_whose_ '; $this->assertEquals([ - 'root' => "Welcome to Xml2Array Converter" + 'root' => 'Welcome to Xml2Array Converter', ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string containing one node that whose value is Cdata Section + * Convert to array from XML string containing one node that whose value is Cdata Section. * + * @testdox Convert to array from XML string containing one node that whose value is Cdata Section. * @test - * - * @return void */ public function convert_to_array_from_xml_string_containing_one_node_that_whose_value_is_cdata_section() { @@ -140,17 +139,16 @@ public function convert_to_array_from_xml_string_containing_one_node_that_whose_ $this->assertEquals([ 'root' => [ - '@cdata' => 'This is CDATA section' - ] + '@cdata' => 'This is CDATA section', + ], ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string that root node has sub node + * Convert to array from XML string that root node has sub node. * + * @testdox Convert to array from XML string that root node has sub node. * @test - * - * @return void */ public function convert_to_array_from_xml_string_that_root_node_has_sub_node() { @@ -162,17 +160,16 @@ public function convert_to_array_from_xml_string_that_root_node_has_sub_node() $this->assertEquals([ 'root' => [ 'subnode_1' => 'Node value', - 'subnode_2' => 'Node value' - ] + 'subnode_2' => 'Node value', + ], ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string that node only has attributes + * Convert to array from XML string that node only has attributes. * + * @testdox Convert to array from XML string that node only has attributes. * @test - * - * @return void */ public function convert_to_array_from_xml_string_that_node_only_has_attributes() { @@ -185,24 +182,23 @@ public function convert_to_array_from_xml_string_that_node_only_has_attributes() 'root' => [ 'subnode_1' => [ '@attributes' => [ - 'description' => 'Subnode #1' - ] + 'description' => 'Subnode #1', + ], ], 'subnode_2' => [ '@attributes' => [ - 'description' => 'Subnode #2' - ] - ] - ] + 'description' => 'Subnode #2', + ], + ], + ], ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string that node has value and attributes + * Convert to array from XML string that node has value and attributes. * + * @testdox Convert to array from XML string that node has value and attributes. * @test - * - * @return void */ public function convert_to_array_from_xml_string_that_node_has_value_and_attributes() { @@ -214,27 +210,26 @@ public function convert_to_array_from_xml_string_that_node_has_value_and_attribu $this->assertEquals([ 'root' => [ 'subnode_1' => [ - '@value' => 'Node value', + '@value' => 'Node value', '@attributes' => [ - 'description' => 'Subnode #1' - ] + 'description' => 'Subnode #1', + ], ], 'subnode_2' => [ - '@value' => 'Node value', + '@value' => 'Node value', '@attributes' => [ - 'description' => 'Subnode #2' - ] - ] - ] + 'description' => 'Subnode #2', + ], + ], + ], ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string containing has namespaces + * Convert to array from XML string containing has namespaces. * + * @testdox Convert to array from XML string containing has namespaces. * @test - * - * @return void */ public function convert_to_array_from_xml_string_containing_has_namespaces() { @@ -249,25 +244,24 @@ public function convert_to_array_from_xml_string_containing_has_namespaces() 'root_node' => [ 'example:node_with_namespace' => [ 'example:sub' => [ - '@value' => 'Content', + '@value' => 'Content', '@attributes' => [ - 'example:description' => 'An attribute with namespace' - ] - ] + 'example:description' => 'An attribute with namespace', + ], + ], ], '@attributes' => [ - 'xmlns:example' => 'http://example.com' - ] - ] + 'xmlns:example' => 'http://example.com', + ], + ], ], Xml2Array::convert($string)->toArray()); } /** - * Convert to array from XML string with special config + * Convert to array from XML string with special config. * + * @testdox Convert to array from XML string with special config. * @test - * - * @return void */ public function convert_to_array_from_xml_string_with_special_config() { @@ -279,21 +273,22 @@ public function convert_to_array_from_xml_string_with_special_config() $this->assertEquals([ 'root_node' => [ 'sub_node' => [ - '#text' => 'Content', + '#text' => 'Content', '#attributes' => [ - 'description' => 'An attribute' - ] - ] - ] + 'description' => 'An attribute', + ], + ], + ], ], Xml2Array::convert($string, [ - 'valueKey' => '#text', - 'attributesKey' => '#attributes' + 'valueKey' => '#text', + 'attributesKey' => '#attributes', ])->toArray()); } /** - * Convert from XML string to array with all cases + * Convert from XML string to array with all cases. * + * @testdox Convert from XML string to array with all cases. * @test */ public function convert_from_xml_string_to_array_with_all_cases() @@ -301,15 +296,16 @@ public function convert_from_xml_string_to_array_with_all_cases() $string = file_get_contents(__DIR__ . '/resources/example.xml'); $result = Xml2Array::convert($string, [ - 'namespacesOnRoot' => false + 'namespacesOnRoot' => false, ])->toArray(); $this->assertEquals($this->fulltest_expected_result, $result); } /** - * Convert from XML object to array with all cases + * Convert from XML object to array with all cases. * + * @testdox Convert from XML object to array with all cases. * @test */ public function convert_from_xml_object_to_array() @@ -317,15 +313,16 @@ public function convert_from_xml_object_to_array() $xmlObject = simplexml_load_file(__DIR__ . '/resources/example.xml'); $result = Xml2Array::convert($xmlObject, [ - 'namespacesOnRoot' => false + 'namespacesOnRoot' => false, ])->toArray(); $this->assertEquals($this->fulltest_expected_result, $result); } /** - * Convert from DOM object to array with all cases + * Convert from DOM object to array with all cases. * + * @testdox Convert from DOM object to array with all cases. * @test */ public function convert_from_dom_object_to_array() @@ -334,18 +331,17 @@ public function convert_from_dom_object_to_array() $domObject->load(__DIR__ . '/resources/example.xml'); $result = Xml2Array::convert($domObject, [ - 'namespacesOnRoot' => false + 'namespacesOnRoot' => false, ])->toArray(); $this->assertEquals($this->fulltest_expected_result, $result); } /** - * Throw DOMException when input XML string is not well-formed XML + * Throw DOMException when input XML string is not well-formed XML. * + * @testdox Throw DOMException when input XML string is not well-formed XML. * @test - * - * @return void */ public function throw_dom_exception_when_input_xml_string_not_well_formed() { @@ -356,11 +352,10 @@ public function throw_dom_exception_when_input_xml_string_not_well_formed() } /** - * Throw DOMException when invalid input + * Throw DOMException when invalid input. * + * @testdox Throw DOMException when invalid input. * @test - * - * @return void */ public function throw_dom_exception_when_invalid_input() { @@ -371,8 +366,9 @@ public function throw_dom_exception_when_invalid_input() } /** - * Convert from XML string to Json + * Convert from XML string to Json. * + * @testdox Convert from XML string to Json. * @test */ public function convert_from_xml_string_to_json()