Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 26, 2017
2 parents 3feb782 + 3a6e0b4 commit 25ed059
Show file tree
Hide file tree
Showing 9 changed files with 422 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
/vendor/
composer.lock
.idea
.idea/
build/
28 changes: 28 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
build:
environment:
php:
version: 7.1
tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover=some-file'
coverage:
file: 'some-file'
format: 'clover'
filter:
excluded_paths: [demo/*, tests/*, docs/*]
checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"bavix/foundation": "^1.0",
"bavix/exceptions": "^1.0"
},
"require-dev": {
"bavix/tests": "^1.0"
},
"autoload": {
"psr-4": {
"Bavix\\": "src/"
Expand Down
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<phpunit bootstrap="vendor/autoload.php">

<testsuites>
<testsuite name="bavix">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist
addUncoveredFilesFromWhitelist="true"
processUncoveredFilesFromWhitelist="true">

<directory suffix=".php">./src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="./build/html/"/>
<log type="coverage-clover" target="./build/logs/clover.xml"/>
</logging>

</phpunit>
205 changes: 177 additions & 28 deletions src/XMLReader/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Bavix\XMLReader;

use Bavix\Exceptions;
use Bavix\Foundation\SharedInstance;
use Bavix\Helpers\File;
use Bavix\Helpers\Arr;
use Bavix\Exceptions;
use DOMElement;

class XMLReader
Expand All @@ -14,7 +16,9 @@ class XMLReader
/**
* @var array
*/
protected $copyright = ['created-with' => 'https://github.com/bavix/xml'];
protected $copyright = [
'created-with' => 'https://github.com/bavix/xml'
];

/**
* @var \DOMDocument
Expand All @@ -30,7 +34,7 @@ protected function document()
{
$this->document = new \DOMDocument('1.0', 'utf-8');
$this->document->formatOutput = true;
$this->copyright['created-at'] = date('Y-m-d H:i:s');
$this->copyright['created-at'] = \date('Y-m-d H:i:s');
}

return $this->document;
Expand All @@ -47,29 +51,173 @@ protected function element($name)
}

/**
* @param string $file
* @param \SimpleXMLElement $element
* @param string $property
*
* @return array
*/
public function asArray($file)
protected function _property(\SimpleXMLElement $element, $property)
{
$reader = \simplexml_load_file($file);
$output = [];

if (method_exists($element, $property))
{

$properties = $element->$property();

if ($properties)
{
$output['@' . $property] = is_array($properties) ?
$properties :
$this->_asArray($properties);

if (empty($output['@' . $property]))
{
Arr::remove($output, '@' . $property);
}
}

}

return $output;
}

/**
* @param \SimpleXMLElement $element
*
* @return array|string
*
* @codeCoverageIgnore
*/
protected function _asData(\SimpleXMLElement $element)
{
$output = $this->_property($element, 'attributes');

if (!$element->count())
{
$output['@value'] = (string)$element;

if (!isset($output['@attributes']))
{
$output = $output['@value'];
}
}

return json_decode(json_encode((array)$reader), true);
return $output;
}

/**
* @param array $storage
* @param \SimpleXMLElement $element
*
* @return array|string
*
* @codeCoverageIgnore
*/
protected function _asArray(\SimpleXMLElement $element)
{
$output = $this->_asData($element);

if (!$element->count())
{
return $output;
}

$first = [];

/**
* @var \SimpleXMLElement $item
*/
foreach ($element as $key => $item)
{
if (!isset($output[$key]))
{
$first[$key] = true;
$output[$key] = $this->_asArray($item);
continue;
}

if (!empty($first[$key]))
{
$output[$key] = [$output[$key]];
}

$output[$key][] = $this->_asArray($item);
$first[$key] = false;
}

return $output;
}

/**
* @param string|\DOMNode $mixed
*
* @return \SimpleXMLElement
*/
protected function _simpleXml($mixed)
{
if ($mixed instanceof \DOMNode)
{
return \simplexml_import_dom($mixed);
}

if (File::isFile($mixed))
{
return \simplexml_load_file($mixed);
}

return \simplexml_load_string($mixed);
}

/**
* @param string|\DOMNode $mixed
*
* @return array
*/
public function asArray($mixed)
{
$data = $this->_simpleXml($mixed);

return $data ?
$this->_asArray($data) :
null;
}

/**
* @return \DOMDocument
*/
public function asObject()
{
return clone $this->document();
}

/**
* @param array|\Traversable $storage
*
* @return array
*/
protected function _convertStorage($storage)
{
if ($storage instanceof \Traversable)
{
return \iterator_to_array($storage);
}

return $storage;
}

/**
* @param array|\Traversable $storage
* @param string $name
*
* @return string
*/
public function asXML(array $storage)
public function asXML($storage, $name = 'bavix')
{
$element = $this->element('bavix');
$element = $this->element($name);

$this->addAttributes($element, $this->copyright);
$this->document()->appendChild($element);
$this->convert($element, $storage);
$this->convert($element, $this->_convertStorage($storage));
$xml = $this->document()->saveXML();

$this->document = null;
Expand All @@ -92,13 +240,13 @@ protected function convert(DOMElement $element, $storage)
return;
}

if (count($storage) === 0)
if (empty($storage))
{
throw new Exceptions\Blank('Array is empty');
}

$isInt = array_map('is_int', array_keys($storage));
$sequential = !in_array(false, $isInt, true);
$isInt = Arr::map(Arr::getKeys($storage), '\is_int');
$sequential = !Arr::in($isInt, false);

foreach ($storage as $key => $data)
{
Expand All @@ -123,9 +271,9 @@ protected function addNodeWithKey($key, DOMElement $element, $storage)
{
$this->addAttributes($element, $storage);
}
elseif ($key === '@value' && is_string($storage))
else if ($key === '@value' && \is_string($storage))
{
$element->nodeValue = htmlspecialchars($storage);
$element->nodeValue = \htmlspecialchars($storage);
}
else
{
Expand All @@ -142,12 +290,11 @@ protected function sequential(DOMElement $element, $storage)
if (is_array($storage))
{
$this->addCollectionNode($element, $storage);
}
else
{

$this->addSequentialNode($element, $storage);
return;
}

$this->addSequentialNode($element, $storage);
}

/**
Expand All @@ -159,7 +306,7 @@ protected function sequential(DOMElement $element, $storage)
*/
protected function addNode(DOMElement $element, $key, $value)
{
$key = str_replace(' ', '_', $key);
$key = \str_replace(' ', '-', $key);
$child = $this->document()->createElement($key);
$element->appendChild($child);
$this->convert($child, $value);
Expand All @@ -183,7 +330,8 @@ protected function addCollectionNode(DOMElement $element, $value)
/**
* @var $child DOMElement
*/
$child = $element->cloneNode();
$child = $this->document()->createElement($element->nodeName);
// $child = $element->cloneNode();
$element->parentNode->appendChild($child);
$this->convert($child, $value);
}
Expand All @@ -196,21 +344,22 @@ protected function addSequentialNode(DOMElement $element, $value)
{
if (empty($element->nodeValue))
{
$element->nodeValue = htmlspecialchars($value);
$element->nodeValue = \htmlspecialchars($value);

return;
}

$child = $element->cloneNode();
$child->nodeValue = htmlspecialchars($value);
$child = $this->document()->createElement($element->nodeName);
// $child = $element->cloneNode();
$child->nodeValue = \htmlspecialchars($value);
$element->parentNode->appendChild($child);
}

/**
* @param DOMElement $element
* @param array $storage
* @param DOMElement $element
* @param array|\Traversable $storage
*/
protected function addAttributes(DOMElement $element, array $storage)
protected function addAttributes(DOMElement $element, $storage)
{
foreach ($storage as $attrKey => $attrVal)
{
Expand Down
Loading

0 comments on commit 25ed059

Please sign in to comment.