diff --git a/.gitignore b/.gitignore index 1ee4bf6..4013eac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.php_cs.dist .idea /vendor /composer.lock diff --git a/LSS/Array2XML.php b/LSS/Array2XML.php index 99e0659..3afdd06 100644 --- a/LSS/Array2XML.php +++ b/LSS/Array2XML.php @@ -20,8 +20,8 @@ */ namespace LSS; -use \DomDocument; -use \Exception; +use DOMDocument; +use Exception; /** * Array2XML: A class to convert array in PHP to XML @@ -56,21 +56,29 @@ * $xml = Array2XML::createXML('root_node_name', $php_array); * echo $xml->saveXML(); */ -class Array2XML { - +class Array2XML +{ /** - * @var DOMDocument + * @var DOMDocument|null */ private static $xml = null; + + /** + * @var string + */ private static $encoding = 'UTF-8'; /** * Initialize the root XML node [optional] - * @param $version - * @param $encoding - * @param $format_output + * + * @param string $version + * @param string $encoding + * @param bool $format_output + * + * @return void */ - public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { + public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) + { self::$xml = new DomDocument($version, $encoding); self::$xml->formatOutput = $format_output; self::$encoding = $encoding; @@ -78,11 +86,14 @@ public static function init($version = '1.0', $encoding = 'UTF-8', $format_outpu /** * Convert an Array to XML + * * @param string $node_name - name of the root node to be converted - * @param array $arr - aray to be converterd + * @param array $arr - array to be converted + * * @return DomDocument */ - public static function &createXML($node_name, $arr = array()) { + public static function &createXML($node_name, $arr = array()) + { $xml = self::getXMLRoot(); $xml->appendChild(self::convert($node_name, $arr)); @@ -94,21 +105,22 @@ public static function &createXML($node_name, $arr = array()) { * Convert an Array to XML. * * @param string $node_name - * Name of the root node to be converted. - * @param array $arr - * Array to be converted. + * Name of the root node to be converted + * @param array $arr + * Array to be converted * * @throws \Exception * * @return \DOMNode */ - private static function &convert($node_name, $arr = array()) { + private static function &convert($node_name, $arr = array()) + { //print_arr($node_name); $xml = self::getXMLRoot(); $node = $xml->createElement($node_name); - if (is_array($arr)) { + if (\is_array($arr)) { // get the attributes first.; if (isset($arr['@attributes'])) { foreach ($arr['@attributes'] as $key => $value) { @@ -127,33 +139,34 @@ private static function &convert($node_name, $arr = array()) { unset($arr['@value']); //remove the key from the array once done. //return from recursion, as a note with value cannot have child nodes. return $node; - } else if (isset($arr['@cdata'])) { + } + if (isset($arr['@cdata'])) { $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); unset($arr['@cdata']); //remove the key from the array once done. //return from recursion, as a note with cdata cannot have child nodes. return $node; } - else if (isset($arr['@comment']) && is_string($arr['@comment'])) { + if (isset($arr['@comment']) && \is_string($arr['@comment'])) { $node->appendChild($xml->createComment(self::bool2str($arr['@comment']))); unset($arr['@comment']); - } - else if (isset($arr['@xml'])) { + } elseif (isset($arr['@xml'])) { $fragment = $xml->createDocumentFragment(); $fragment->appendXML($arr['@xml']); $node->appendChild($fragment); unset($arr['@xml']); + return $node; } } //create subnodes using recursion - if (is_array($arr)) { + if (\is_array($arr)) { // recurse to get the node for that key foreach ($arr as $key => $value) { if (!self::isValidTagName($key)) { throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name); } - if (is_array($value) && is_numeric(key($value))) { + if (\is_array($value) && \is_numeric(\key($value))) { // MORE THAN ONE NODE OF ITS KIND; // if the new array is numeric index, means it is array of nodes of the same kind // it should follow the parent key name @@ -170,40 +183,54 @@ private static function &convert($node_name, $arr = array()) { // after we are done with all the keys in the array (if it is one) // we check if it has any text value, if yes, append it. - if (!is_array($arr)) { + if (!\is_array($arr)) { $node->appendChild($xml->createTextNode(self::bool2str($arr))); } return $node; } - /* + /** * Get the root XML node, if there isn't one, create it. + * + * @return DOMDocument */ - private static function getXMLRoot() { + private static function getXMLRoot() + { if (empty(self::$xml)) { self::init(); } + return self::$xml; } - /* + /** * Get string representation of boolean value + * + * @param bool|mixed $v + * + * @return mixed|string */ - private static function bool2str($v) { + private static function bool2str($v) + { //convert boolean to text value. $v = $v === true ? 'true' : $v; - $v = $v === false ? 'false' : $v; - return $v; + + return $v === false ? 'false' : $v; } - /* + /** * Check if the tag name or attribute name contains illegal characters * Ref: http://www.w3.org/TR/xml/#sec-common-syn + * + * @param string $tag + * + * @return bool */ - private static function isValidTagName($tag) { + private static function isValidTagName($tag) + { $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i'; - return preg_match($pattern, $tag, $matches) && $matches[0] == $tag; + + return \preg_match($pattern, $tag, $matches) && $matches[0] === $tag; } } - diff --git a/LSS/XML2Array.php b/LSS/XML2Array.php index ad5dafa..0348908 100644 --- a/LSS/XML2Array.php +++ b/LSS/XML2Array.php @@ -14,13 +14,14 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * - * You should have received a copy of the + * You should have received a copy of the * GNU Lesser General Public License along with OpenLSS. * If not, see . */ namespace LSS; -use \DOMDocument; -use \Exception; + +use DOMDocument; +use Exception; /** * XML2Array: A class to convert XML to array in PHP @@ -40,134 +41,161 @@ * Usage: * $array = XML2Array::createArray($xml); */ - -class XML2Array { - +class XML2Array +{ + /** + * @var DOMDocument|null + */ protected static $xml = null; + + /** + * @var string + */ protected static $encoding = 'UTF-8'; + + /** + * @var string + */ protected static $prefix_attributes = '@'; /** * Initialize the root XML node [optional] - * @param $version - * @param $encoding - * @param $format_output + * + * @param string $version + * @param string $encoding + * @param bool $format_output + * + * @return void */ - public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { + public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) + { self::$xml = new DOMDocument($version, $encoding); self::$xml->formatOutput = $format_output; - self::$encoding = $encoding; + self::$encoding = $encoding; } /** * Convert an XML to Array - * @param string $node_name - name of the root node to be converted - * @param int - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php - * @param array $arr - aray to be converterd - * @param mixed $callback - callback function + * + * @param DOMDocument|string $input_xml - name of the root node to be converted + * @param int $options - Bitwise OR of the libxml option constants see @link http://php.net/manual/libxml.constants.php + * @param callable|null $callback - callback function + * * @return array */ - public static function &createArray($input_xml, $options = 0, $callback = null) { + public static function &createArray($input_xml, $options = 0, $callback = null) + { $xml = self::getXMLRoot(); - if(is_string($input_xml)) { - $parsed = $xml->loadXML($input_xml, $options); - if(!$parsed) { - throw new Exception('[XML2Array] Error parsing the XML string.'); - } - } else { - if(get_class($input_xml) != 'DOMDocument') { - throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); - } - $xml = self::$xml = $input_xml; - } - $array[$xml->documentElement->tagName] = self::convert($xml->documentElement, $callback); + if (\is_string($input_xml)) { + $parsed = $xml->loadXML($input_xml, $options); + if (!$parsed) { + throw new Exception('[XML2Array] Error parsing the XML string.'); + } + } else { + if (\get_class($input_xml) !== 'DOMDocument') { + throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); + } + $xml = self::$xml = $input_xml; + } + $array[$xml->documentElement->tagName] = self::convert($xml->documentElement, $callback); self::$xml = null; // clear the xml node in the class for 2nd time use. return $array; } /** * Convert an Array to XML - * @param mixed $node - XML as a string or as an object of DOMDocument - * @param mixed $callback - callback function + * + * @param mixed $node - XML as a string or as an object of DOMDocument + * @param callable|null $callback - callback function + * * @return mixed */ - protected static function &convert($node, $callback = null) { - $output = array(); - - switch ($node->nodeType) { - case XML_CDATA_SECTION_NODE: - $output[static::$prefix_attributes.'cdata'] = trim($node->textContent); - break; - - case XML_TEXT_NODE: - $output = trim($node->textContent); - break; - - case XML_ELEMENT_NODE: - // for each child node, call the covert function recursively - for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { - if ($callback!==null) { - $callback($m=$node->childNodes->length, $i); - } - $child = $node->childNodes->item($i); - $v = self::convert($child); - if(isset($child->tagName)) { - $t = $child->tagName; + protected static function &convert($node, $callback = null) + { + $output = array(); + + switch ($node->nodeType) { + case \XML_CDATA_SECTION_NODE: + $output[static::$prefix_attributes . 'cdata'] = \trim($node->textContent); + + break; + + case \XML_TEXT_NODE: + $output = \trim($node->textContent); + + break; + + case \XML_ELEMENT_NODE: + // for each child node, call the covert function recursively + for ($i = 0, $m = $node->childNodes->length; $i < $m; ++$i) { + if ($callback !== null) { + $callback($m = $node->childNodes->length, $i); + } + $child = $node->childNodes->item($i); + $v = self::convert($child); + if (isset($child->tagName)) { + $t = $child->tagName; // avoid fatal error if the content looks like 'You are being redirected.' - if(isset($output) && !is_array($output)) { + if (isset($output) && !\is_array($output)) { continue; } - // assume more nodes of same kind are coming - if(!isset($output[$t])) { - $output[$t] = array(); - } - $output[$t][] = $v; - } else { - //check if it is not an empty text node - if($v !== '') { - $output = $v; - } - } - } - - if(is_array($output)) { - // if only one node of its kind, assign it directly instead if array($value); - foreach ($output as $t => $v) { - if(is_array($v) && count($v)==1) { - $output[$t] = $v[0]; - } - } - if(empty($output)) { - //for empty nodes - $output = ''; - } - } - - // loop through the attributes and collect them - if($node->attributes->length) { - $a = array(); - foreach($node->attributes as $attrName => $attrNode) { - $a[$attrName] = (string) $attrNode->value; - } - // if its an leaf node, store the value in @value instead of directly storing it. - if(!is_array($output)) { - $output = array(static::$prefix_attributes.'value' => $output); - } - $output[static::$prefix_attributes.'attributes'] = $a; - } - break; - } - return $output; + // assume more nodes of same kind are coming + if (!isset($output[$t])) { + $output[$t] = array(); + } + $output[$t][] = $v; + } else { + //check if it is not an empty text node + if ($v !== '') { + $output = $v; + } + } + } + + if (\is_array($output)) { + // if only one node of its kind, assign it directly instead if array($value); + foreach ($output as $t => $v) { + if (\is_array($v) && \count($v) === 1) { + $output[$t] = $v[0]; + } + } + if (empty($output)) { + //for empty nodes + $output = ''; + } + } + + // loop through the attributes and collect them + if ($node->attributes->length) { + $a = array(); + foreach ($node->attributes as $attrName => $attrNode) { + $a[$attrName] = (string) $attrNode->value; + } + // if its an leaf node, store the value in @value instead of directly storing it. + if (!\is_array($output)) { + $output = array(static::$prefix_attributes . 'value' => $output); + } + $output[static::$prefix_attributes . 'attributes'] = $a; + } + + break; + } + + return $output; } - /* + /** * Get the root XML node, if there isn't one, create it. + * + * @return DOMDocument */ - protected static function getXMLRoot(){ - if(empty(self::$xml)) { + protected static function getXMLRoot() + { + if (empty(self::$xml)) { self::init(); } + return self::$xml; } } diff --git a/composer.json b/composer.json index 7471231..9b49261 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ } ] ,"require": { - "php": ">=5.3.2" + "php": ">=5.3.2", + "ext-dom": "*" } ,"autoload": { "psr-0": { diff --git a/phpcs.php_cs b/phpcs.php_cs new file mode 100644 index 0000000..707ac8e --- /dev/null +++ b/phpcs.php_cs @@ -0,0 +1,238 @@ +setUsingCache(false) + ->setRiskyAllowed(true) + ->setRules( + [ + 'align_multiline_comment' => [ + 'comment_type' => 'all_multiline', + ], + 'array_indentation' => true, + 'array_syntax' => [ + 'syntax' => 'long', + ], + 'backtick_to_shell_exec' => true, + 'binary_operator_spaces' => [ + 'operators' => ['=>' => 'align_single_space_minimal'], + ], + 'blank_line_after_namespace' => true, + 'blank_line_after_opening_tag' => false, + 'blank_line_before_statement' => true, + 'braces' => true, + 'cast_spaces' => [ + 'space' => 'single', + ], + 'class_attributes_separation' => true, + 'class_keyword_remove' => false, + 'combine_consecutive_issets' => true, + 'combine_consecutive_unsets' => true, + 'combine_nested_dirname' => true, + // 'compact_nullable_typehint' => true, // PHP >= 7.1 + 'concat_space' => [ + 'spacing' => 'one', + ], + 'date_time_immutable' => false, + 'declare_equal_normalize' => true, + 'declare_strict_types' => false, // PHP >= 7.0 + 'dir_constant' => true, + 'elseif' => true, + 'encoding' => true, + 'ereg_to_preg' => true, + 'error_suppression' => false, + 'escape_implicit_backslashes' => false, + 'explicit_indirect_variable' => true, + 'explicit_string_variable' => true, + 'final_internal_class' => true, + 'fopen_flag_order' => true, + 'fopen_flags' => true, + 'full_opening_tag' => true, + 'fully_qualified_strict_types' => true, + 'function_declaration' => true, + 'function_to_constant' => true, + 'function_typehint_space' => true, + 'general_phpdoc_annotation_remove' => [ + 'annotations' => [ + 'author', + 'package', + 'version', + ], + ], + 'heredoc_to_nowdoc' => true, + 'implode_call' => true, + 'include' => true, + 'increment_style' => true, + 'indentation_type' => true, + 'line_ending' => true, + 'linebreak_after_opening_tag' => false, + /* // Requires PHP >= 7.1 + 'list_syntax' => [ + 'syntax' => 'short', + ], + */ + 'logical_operators' => true, + 'lowercase_cast' => true, + 'lowercase_constants' => true, + 'lowercase_keywords' => true, + 'lowercase_static_reference' => true, + 'magic_constant_casing' => true, + 'magic_method_casing' => true, + 'method_argument_space' => [ + 'ensure_fully_multiline' => true, + 'keep_multiple_spaces_after_comma' => false, + ], + 'method_chaining_indentation' => true, + 'modernize_types_casting' => true, + 'multiline_comment_opening_closing' => true, + 'multiline_whitespace_before_semicolons' => [ + 'strategy' => 'no_multi_line', + ], + 'native_constant_invocation' => true, + 'native_function_casing' => true, + 'native_function_invocation' => true, + 'new_with_braces' => true, + 'no_alias_functions' => true, + 'no_alternative_syntax' => true, + 'no_binary_string' => true, + 'no_blank_lines_after_class_opening' => false, + 'no_blank_lines_after_phpdoc' => true, + 'no_blank_lines_before_namespace' => false, + 'no_break_comment' => true, + 'no_closing_tag' => true, + 'no_empty_comment' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_extra_blank_lines' => true, + 'no_homoglyph_names' => true, + 'no_leading_import_slash' => true, + 'no_leading_namespace_whitespace' => true, + 'no_mixed_echo_print' => [ + 'use' => 'echo', + ], + 'no_multiline_whitespace_around_double_arrow' => true, + 'no_null_property_initialization' => true, + 'no_php4_constructor' => true, + 'no_short_bool_cast' => true, + 'no_short_echo_tag' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_spaces_after_function_name' => true, + 'no_spaces_around_offset' => true, + 'no_spaces_inside_parenthesis' => true, + 'no_superfluous_elseif' => true, + 'no_superfluous_phpdoc_tags' => false, // maybe add extra description, so keep it ... + 'no_trailing_comma_in_list_call' => true, + 'no_trailing_comma_in_singleline_array' => true, + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'no_unneeded_control_parentheses' => true, + 'no_unneeded_curly_braces' => true, + 'no_unneeded_final_method' => true, + 'no_unreachable_default_argument_value' => false, // do not changes the logic of the code ... + 'no_unset_on_property' => true, + 'no_unused_imports' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'no_whitespace_before_comma_in_array' => true, + 'no_whitespace_in_blank_line' => true, + 'non_printable_character' => true, + 'normalize_index_brace' => true, + 'not_operator_with_space' => false, + 'not_operator_with_successor_space' => false, + 'object_operator_without_whitespace' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'phpdoc_add_missing_param_annotation' => [ + 'only_untyped' => true, + ], + 'phpdoc_align' => true, + 'phpdoc_annotation_without_dot' => true, + 'phpdoc_indent' => true, + 'phpdoc_inline_tag' => true, + 'phpdoc_no_access' => true, + 'phpdoc_no_alias_tag' => true, + 'phpdoc_no_empty_return' => false, // keep all @return phpdocs + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_order' => true, + 'phpdoc_return_self_reference' => true, + 'phpdoc_scalar' => true, + 'phpdoc_separation' => true, + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_summary' => false, + 'phpdoc_to_comment' => false, + 'phpdoc_to_return_type' => false, + 'phpdoc_trim' => true, + 'phpdoc_trim_consecutive_blank_line_separation' => true, + 'phpdoc_types' => true, + 'phpdoc_types_order' => [ + 'null_adjustment' => 'always_last', + 'sort_algorithm' => 'alpha', + ], + 'phpdoc_var_without_name' => true, + 'php_unit_construct' => true, + 'php_unit_dedicate_assert' => true, + 'php_unit_expectation' => true, + 'php_unit_fqcn_annotation' => true, + 'php_unit_internal_class' => true, + 'php_unit_method_casing' => true, + 'php_unit_mock' => true, + 'php_unit_namespaced' => true, + 'php_unit_no_expectation_annotation' => true, + 'php_unit_ordered_covers' => true, + 'php_unit_set_up_tear_down_visibility' => true, + 'php_unit_strict' => true, + 'php_unit_test_annotation' => true, + 'php_unit_test_case_static_method_calls' => true, + 'php_unit_test_class_requires_covers' => false, + 'pow_to_exponentiation' => true, + 'pre_increment' => true, + 'protected_to_private' => true, + 'return_assignment' => true, + 'return_type_declaration' => true, + 'self_accessor' => false, // not working as expected + 'semicolon_after_instruction' => true, + 'set_type_to_cast' => true, + 'short_scalar_cast' => true, + 'silenced_deprecation_error' => false, + 'simplified_null_return' => true, + 'single_blank_line_at_eof' => true, + 'single_class_element_per_statement' => true, + 'single_import_per_statement' => true, + 'single_line_after_imports' => true, + 'single_line_comment_style' => [ + 'comment_types' => ['hash'], + ], + 'single_quote' => true, + 'space_after_semicolon' => true, + 'standardize_increment' => true, + 'standardize_not_equals' => true, + 'static_lambda' => true, + 'strict_comparison' => true, + 'strict_param' => true, + 'string_line_ending' => true, + 'switch_case_semicolon_to_colon' => true, + 'switch_case_space' => true, + 'ternary_operator_spaces' => true, + 'ternary_to_null_coalescing' => true, + 'trailing_comma_in_multiline_array' => true, + 'trim_array_spaces' => true, + 'unary_operator_spaces' => true, + 'visibility_required' => true, + // 'void_return' => true, // PHP >= 7.1 + 'whitespace_after_comma_in_array' => true, + 'yoda_style' => [ + 'equal' => false, + 'identical' => false, + 'less_and_greater' => false, + ], + ] + ) + ->setIndent(" ") + ->setLineEnding("\n") + ->setFinder( + PhpCsFixer\Finder::create() + ->in(['LSS/']) + ->name('*.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true) + ); \ No newline at end of file