Skip to content

Improve code quality #6

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

Merged
2 commits merged into from
Apr 21, 2018
Merged
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
63 changes: 31 additions & 32 deletions src/PhpArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function getFormatOutput()
public function setCustomRootName($value)
{
if (!$this->isValidXmlTag($value)) {
throw new \Exception('Not a valid root name: ' . $value);
throw new \Exception('Not a valid root name: '.$value);
}

$this->_custom_root_name = $value;
Expand Down Expand Up @@ -154,7 +154,7 @@ public function getDefaultRootName()
public function setCustomTagName($value)
{
if (!$this->isValidXmlTag($value)) {
throw new \Exception('Not a valid tag name: ' . $value);
throw new \Exception('Not a valid tag name: '.$value);
}

$this->_custom_tag_name = $value;
Expand Down Expand Up @@ -217,14 +217,14 @@ public function getSeparator()
*/
public function setTransformTags($value = null)
{
switch($value) {
switch ($value) {
case self::LOWERCASE:
case self::UPPERCASE: {
$this->_transform_tags = $value;
break;
}
default: {
if($value === null) {
if ($value === null) {
$this->_transform_tags = null;
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public function setNumericTagSuffix($value = null)
{
$this->_numeric_tag_suffix = $value;

if($value === true || $value === false) {
if ($value === true || $value === false) {
$this->_numeric_tag_suffix = '';
}
return $this;
Expand Down Expand Up @@ -340,7 +340,7 @@ public function getCastNullValue()
*/
public static function hasValidXmlTagStartingChar($value = null)
{
if(preg_match(self::getValidXmlTagStartPattern(), $value) === 1) {
if (preg_match(self::getValidXmlTagStartPattern(), $value) === 1) {
return true;
}
return false;
Expand All @@ -354,7 +354,7 @@ public static function hasValidXmlTagStartingChar($value = null)
*/
public static function isValidXmlTagChar($value = null)
{
if(preg_match(self::getValidXmlTagNameChar(), $value) === 1) {
if (preg_match(self::getValidXmlTagNameChar(), $value) === 1) {
return true;
}
return false;
Expand All @@ -368,11 +368,11 @@ public static function isValidXmlTagChar($value = null)
*/
public static function isValidXmlTag($value = null)
{
if(empty($value) || is_int($value)) {
if (empty($value) || is_int($value)) {
return false;
}

if(preg_match(self::getValidXmlTagNamePattern(), $value) === 1) {
if (preg_match(self::getValidXmlTagNamePattern(), $value) === 1) {
return true;
}
return false;
Expand Down Expand Up @@ -458,19 +458,19 @@ protected function addArrayElements(DOMElement $parent, $array = [])
$parent->appendChild($node);
} else {

if(array_key_exists('@value', $value)) {
if (array_key_exists('@value', $value)) {
$cdata = array_key_exists('@cdata', $value) && $value['@cdata'] === true ? true : false;
$attributes = array_key_exists('@attr', $value) && is_array($value['@attr']) ? $value['@attr'] : [];

if(!is_array($value['@value'])) {
if (!is_array($value['@value'])) {
// Create an XML element
$node = $this->createElement($name, $value['@value'], $cdata, $attributes);
$parent->appendChild($node);
} else {
// Create an empty XML element 'container'
$node = $this->createElement($name, null);

foreach($attributes as $attribute_name => $attribute_value) {
foreach ($attributes as $attribute_name => $attribute_value) {
$node->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
}

Expand All @@ -479,8 +479,7 @@ protected function addArrayElements(DOMElement $parent, $array = [])
// Add all the elements within the array to the 'container'
$this->addArrayElements($node, $value['@value']);
}
}
else {
} else {
// Create an empty XML element 'container'
$node = $this->createElement($name, null);
$parent->appendChild($node);
Expand All @@ -501,15 +500,15 @@ protected function addArrayElements(DOMElement $parent, $array = [])
*/
protected function normalizeValue($value)
{
if($value === true) {
if ($value === true) {
return $this->getCastBooleanValueTrue();
}

if($value === false) {
if ($value === false) {
return $this->getCastBooleanValueFalse();
}

if($value === null) {
if ($value === null) {
return $this->getCastNullValue();
}

Expand All @@ -524,11 +523,11 @@ protected function normalizeValue($value)
*/
protected function normalizeAttributeValue($value)
{
if($value === true) {
if ($value === true) {
return 'true';
}

if($value === false) {
if ($value === false) {
return 'false';
}

Expand Down Expand Up @@ -561,11 +560,11 @@ protected function createElement($name, $value = null, $cdata = false, $attribut
{
$name = $this->createValidTagName($name);

if($cdata === true) {
if ($cdata === true) {
$element = $this->_doc->createElement($name);
$element->appendChild($this->_doc->createCDATASection($value));

foreach($attributes as $attribute_name => $attribute_value) {
foreach ($attributes as $attribute_name => $attribute_value) {
$element->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
}

Expand All @@ -574,7 +573,7 @@ protected function createElement($name, $value = null, $cdata = false, $attribut

$element = $this->_doc->createElement($name, $this->normalizeValue($value));

foreach($attributes as $attribute_name => $attribute_value) {
foreach ($attributes as $attribute_name => $attribute_value) {
$element->setAttribute($attribute_name, $this->normalizeAttributeValue($attribute_value));
}

Expand All @@ -589,7 +588,7 @@ protected function createElement($name, $value = null, $cdata = false, $attribut
*/
protected function createValidTagName($name = null)
{
if(empty($name) || $this->isNumericKey($name)) {
if (empty($name) || $this->isNumericKey($name)) {
$key = $name;

if ($this->isValidXmlTag($this->getCustomTagName())) {
Expand All @@ -598,16 +597,16 @@ protected function createValidTagName($name = null)
$name = $this->transformTagName($this->getDefaultTagName());
}

if($this->getNumericTagSuffix() !== null) {
$name = $name . (string)$this->getNumericTagSuffix() . $key;
if ($this->getNumericTagSuffix() !== null) {
$name = $name.(string) $this->getNumericTagSuffix().$key;
}
return $name;
}

if(!$this->isValidXmlTag($name)) {
if (!$this->isValidXmlTag($name)) {
$name = $this->replaceInvalidTagChars($name);

if(!self::hasValidXmlTagStartingChar($name)) {
if (!self::hasValidXmlTagStartingChar($name)) {
$name = $this->prefixInvalidTagStartingChar($name);
}
}
Expand All @@ -622,7 +621,7 @@ protected function createValidTagName($name = null)
*/
protected function prefixInvalidTagStartingChar($value)
{
return '_' . substr($value, 1);
return '_'.substr($value, 1);
}

/**
Expand All @@ -634,13 +633,13 @@ protected function prefixInvalidTagStartingChar($value)
protected function replaceInvalidTagChars($value)
{
$pattern = '';
for($i=0; $i < strlen($value); $i++) {
if(!self::isValidXmlTagChar($value[$i])) {
for ($i = 0; $i < strlen($value); $i++) {
if (!self::isValidXmlTagChar($value[$i])) {
$pattern .= "\\$value[$i]";
}
}

if(!empty($pattern)) {
if (!empty($pattern)) {
$value = preg_replace("/[{$pattern}]/", $this->getSeparator(), $value);
}
return $value;
Expand Down Expand Up @@ -671,7 +670,7 @@ protected function createValidRootName($name = null)
*/
protected function transformTagName($name = null)
{
switch($this->getTransformTags()) {
switch ($this->getTransformTags()) {
case self::LOWERCASE: {
return strtolower($name);
}
Expand Down