Skip to content

Commit

Permalink
Merge branch '1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mleczek committed Jan 22, 2017
2 parents f577878 + 9e97d21 commit 3360020
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage_clover: tests/logs/clover.xml
json_path: tests/logs/coveralls-upload.json
service_name: travis-ci
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ php:
- 7.0
- 7.1

install: composer install --no-interaction --no-suggest
before_install:
- composer require satooshi/php-coveralls

script: vendor/bin/phpunit --verbose
install:
- composer install --no-interaction --no-suggest

script:
- vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml --verbose

after_script:
- php vendor/bin/coveralls -v
63 changes: 36 additions & 27 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
{
"name": "mleczek/xml",
"description": "Transform PHP object to XML",
"type": "library",
"keywords": ["php", "xml", "object", "transform", "convert"],
"homepage": "https://github.com/mleczek/xml",
"license": "MIT",
"authors": [
{
"name": "Wojciech Mleczek",
"email": "mleczek.wojtek@gmail.com"
}
],
"require-dev": {
"phpunit/phpunit": "~5.7",
"mockery/mockery": "~0.9"
},
"autoload": {
"psr-4": {
"Mleczek\\Xml\\": "src/"
},
"files": [
"src/helpers.php"
]
"name": "mleczek/xml",
"description": "Transform PHP object to XML",
"type": "library",
"keywords": [
"php",
"xml",
"object",
"transform",
"convert"
],
"homepage": "https://github.com/mleczek/xml",
"license": "MIT",
"authors": [
{
"name": "Wojciech Mleczek",
"email": "mleczek.wojtek@gmail.com"
}
],
"require-dev": {
"phpunit/phpunit": "~5.7",
"mockery/mockery": "~0.9"
},
"autoload": {
"psr-4": {
"Mleczek\\Xml\\": "src/"
},
"autoload-dev": {
"psr-4": {
"Mleczek\\Xml\\Tests\\": "tests/"
}
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Mleczek\\Xml\\Tests\\": "tests/"
}
},
"bin": [
"bin/coveralls"
]
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Convert PHP objects to XML

[![Build Status](https://travis-ci.org/mleczek/xml.svg)](https://travis-ci.org/mleczek/xml)
[![Latest Stable Version](https://poser.pugx.org/mleczek/xml/v/stable)](https://packagist.org/packages/mleczek/xml)
[![Build Status](https://travis-ci.org/mleczek/xml.svg)](https://travis-ci.org/mleczek/xml)
[![Coverage Status](https://coveralls.io/repos/github/mleczek/xml/badge.svg?branch=1.0)](https://coveralls.io/github/mleczek/xml?branch=1.0)
[![License](https://poser.pugx.org/mleczek/xml/license)](https://packagist.org/packages/mleczek/xml)

The goal of the this library is to provide an easy way to respond XML by REST API.
Expand Down
12 changes: 9 additions & 3 deletions src/StructureAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,27 @@ protected function parse($data, $root_name, $prefix = '')
*/
private function parseKeyValue($key, $value, $prefix = '')
{
// Nested elements of Xmlable interfaces
if ($value instanceof Xmlable) {
$inner_xml = '=' . toXml($value, null, false);
return is_string($key) ? [$key => $inner_xml] : [$inner_xml];
}

// Array inside array ([[...], [...], ...])
// and nested elements (array or object).
if(is_array($value) || is_object($value)) {
if (is_array($value) || is_object($value)) {
$root_name = is_string($key) ? $key : self::ROOT_NAME;
return $this->parse($value, $root_name, "$prefix$key.");
}

// Support self-closing elements
if(is_int($key)) {
if (is_int($key)) {
$key = $value;
$value = null;
}

// Skip parsing
if(!is_string($key)) {
if (!is_string($key)) {
return [];
}

Expand Down
10 changes: 8 additions & 2 deletions src/XmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ protected function buildAttrFor(XmlElement $root, $attr_name, $value)
/**
* If $value is:
* 1. string: constant or property text value
* 2. null: self-closing element
* 2a. null: raw xml string
* 2b. null: self-closing element
* 3. boolean (true): same as null
* 4. boolean (false): skip element
* 5. array/Traversable: build nested element
Expand All @@ -179,8 +180,13 @@ protected function buildNodeFor(XmlElement $root, $node_name, $value)
return $root->addChild($element);
}

// 2. null: self-closing element
if (is_null($value)) {
// 2a. null: raw xml string
if(isset($node_name[0]) && $node_name[0] == self::CONST_PREFIX) {
return $root->setText($root->getText() . substr($node_name, 1));
}

// 2b. null: self-closing element
return $root->addChild($element);
}

Expand Down
16 changes: 12 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@
*
* @param Xmlable|object|array $object
* @param array|null $meta
* @param boolean $xml_declaration
* @return string
*/
function toXml($object, array $meta = null)
function toXml($object, array $meta = null, $xml_declaration = true)
{
if (!($object instanceof Xmlable) && is_null($meta)) {
$meta = (new StructureAnalyser())->analyse($object);
}

return XmlElement::XmlDeclaration . (string)(new XmlConverter($object, $meta));
$converter = new XmlConverter($object, $meta);
if($xml_declaration) {
return XmlElement::XmlDeclaration . $converter->asString();
}

return $converter->asString();
}

/**
* Dynamic converts object to XML.
*
* @param object|array $object
* @param string $root_name
* @param boolean $xml_declaration
* @return string
*/
function toXmlAs($object, $root_name)
function toXmlAs($object, $root_name, $xml_declaration = true)
{
$meta = (new StructureAnalyser())->analyse($object, $root_name);
return XmlElement::XmlDeclaration . (string)(new XmlConverter($object, $meta));

return toXml($object, $meta, $xml_declaration);
}
}
15 changes: 15 additions & 0 deletions tests/StructureAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Mleczek\Xml\StructureAnalyser;
use Mleczek\Xml\Xmlable;
use Mleczek\Xml\XmlConverter;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -60,6 +61,20 @@ public function validInputProvider()
[0, 1 => null, 'test'],
'<result><test/></result>'
],

'array of Xmlable' => [
[
\Mockery::mock(Xmlable::class)
->shouldReceive('xml')
->andReturn('<mock/>')
->getMock(),
'test' => \Mockery::mock(Xmlable::class)
->shouldReceive('xml')
->andReturn('<mock2/>')
->getMock()
],
'<result><mock/><test><mock2/></test></result>',
]
];
}

Expand Down
6 changes: 6 additions & 0 deletions tests/XmlConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public function validXmlProvider()
'<test attr_1 attr_2/>'
],

'merging raw xml' => [
[],
['=<test/>'],
'<test/>'
],

'merging empty arrays' => [
[],
[
Expand Down

0 comments on commit 3360020

Please sign in to comment.