Skip to content

Commit

Permalink
Merge pull request #2 from phillipsdata/update-namespace
Browse files Browse the repository at this point in the history
Changed namespace from `minphp\Xml` to `Minphp\Xml`
  • Loading branch information
clphillips committed Dec 11, 2015
2 parents 3ea3cc8 + da4aca0 commit 436f210
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# minphp/Xml
# Minphp/Xml

[![Build Status](https://travis-ci.org/phillipsdata/minphp-xml.svg?branch=master)](https://travis-ci.org/phillipsdata/minphp-xml) [![Coverage Status](https://coveralls.io/repos/phillipsdata/minphp-xml/badge.svg)](https://coveralls.io/r/phillipsdata/minphp-xml)

Expand All @@ -9,7 +9,7 @@ XML helper.
Install via composer:

```sh
composer require minphp/xml:dev-master
composer require minphp/xml
```

## Basic Usage
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {"minphp\\Xml\\": "src"}
"psr-4": {"Minphp\\Xml\\": "src"}
},
"autoload-dev": {
"psr-4": {"Minphp\\Xml\\Tests\\": "tests"}
}
}
30 changes: 15 additions & 15 deletions src/Xml.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace minphp\Xml;
namespace Minphp\Xml;

/**
* Provides helper methods for creating XML documents from arrays and objects
Expand All @@ -14,7 +14,7 @@ class Xml
* @var string The default node for an element
*/
public $root_node = "result";

/**
* Converts special characters with special meaning in XML to their entity
* equivalent.
Expand All @@ -26,7 +26,7 @@ public function xmlEntities($str)
{
static $search_chars = array();
static $replace_chars = array();

if (empty($search_chars)) {
// Replace accepted whitespace characters. All other low ordered bytes are
// invalid in XML 1.0
Expand All @@ -41,14 +41,14 @@ public function xmlEntities($str)
$replace_chars[] = null;
}
}

$search_chars = array_merge(array("&", "<", ">", "\"", "`"), $search_chars);
$replace_chars = array_merge(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;"), $replace_chars);
}

return str_replace($search_chars, $replace_chars, $str);
}

/**
* Convert the given array into an XML document
*
Expand All @@ -60,10 +60,10 @@ public function makeXml($vars, $encoding = "UTF-8")
{
$xml = "<?xml version=\"1.0\" encoding=\"" . $encoding . "\" ?>";
$xml .= $this->buildXMLSegment($vars, $this->root_node);

return $xml;
}

/**
* Recursively convert the array into an XML structure segment
*
Expand All @@ -76,26 +76,26 @@ private function buildXmlSegment($value, $root_node = "result", $tab_count = -1)
{
$xml = "";
$tab = $this->tab;

if (is_numeric($root_node)) {
$root_node = $this->root_node;
}

if (is_object($value)) {
$value = (array)$value;
}

if (is_array($value)) {
foreach ($value as $key => $value2) {
// Remove any illegal tag characters
$key = preg_replace("/[^a-z0-9_:.-]/i", "", $key);
// Recurse
$sub = $this->buildXmlSegment($value2, $key, ++$tab_count);

$xml .= "\n";

$break = is_array($value2) || is_object($value2);

// If numeric, wrap element with parent's tag
if (is_numeric($key)) {
$xml .= str_repeat($tab, $tab_count-1) . "<" . $root_node . ">"
Expand Down Expand Up @@ -126,7 +126,7 @@ private function buildXmlSegment($value, $root_node = "result", $tab_count = -1)
} else {
$xml .= $this->xmlEntities($value);
}

return $xml;
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/nested_array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<section>
<contents>
<item1>1</item1>
<item2>two</item2>
<item4 />
</contents>
<other>

<item>1</item>
<item>2</item>
<item>3</item>
</other>
</section>
32 changes: 10 additions & 22 deletions tests/XmlTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
namespace minphp\Xml;
namespace Minphp\Xml\Tests;

use \PHPUnit_Framework_TestCase;
use PHPUnit_Framework_TestCase;
use Minphp\Xml\Xml;

/**
* @coversDefaultClass \minphp\Xml\Xml
* @coversDefaultClass \Minphp\Xml\Xml
*/
class XmlTest extends PHPUnit_Framework_TestCase
{
private $xml;

public function setUp()
{
$this->xml = new Xml();
Expand All @@ -27,7 +28,7 @@ public function testXmlEntities()
$this->assertEquals('&quot;', $this->xml->xmlEntities('"'));
$this->assertEquals('&apos;', $this->xml->xmlEntities('`'));
}

/**
* @covers ::makeXml
* @covers ::buildXmlSegment
Expand All @@ -44,22 +45,9 @@ public function testMakeXml()
'other' => array('item' => array(1, 2, 3)),
)
);
$result =<<< XML
<?xml version="1.0" encoding="UTF-8" ?>
<section>
<contents>
<item1>1</item1>
<item2>two</item2>
<item4 />
</contents>
<other>
<item>1</item>
<item>2</item>
<item>3</item>
</other>
</section>
XML;
$this->assertEquals($result, $this->xml->makeXml($data));
$this->assertEquals(
file_get_contents(__DIR__ . '/Fixtures/nested_array.xml'),
$this->xml->makeXml($data)
);
}
}

0 comments on commit 436f210

Please sign in to comment.