Commit 982d58a0 authored by Olivier Lequeux's avatar Olivier Lequeux
Browse files

add namespace registration to SimpleXMLHydrator

parent 95537313
...@@ -11,12 +11,12 @@ namespace Lavoisier\Entries; ...@@ -11,12 +11,12 @@ namespace Lavoisier\Entries;
class Entries extends \ArrayObject implements IEntries class Entries extends \ArrayObject implements IEntries
{ {
// original simple entries/entry Lavoisier xsd (it's a subset of the following one)
const XMLNS_entries = "http://software.in2p3.fr/lavoisier/entries.xsd";
// extended recursive entries/entries/.../entries/entry lavoisiercli xsd
const XMLNS_ex_entries = "https://gitlab.in2p3.fr/opsportal/lavoisiercli/raw/master/resources/ex_entries.xsd";
public function init()
{
}
/* /*
* @param bool set true to ask original Lavoisier XMLNS * @param bool set true to ask original Lavoisier XMLNS
* @return string XMLNS * @return string XMLNS
...@@ -24,15 +24,12 @@ class Entries extends \ArrayObject implements IEntries ...@@ -24,15 +24,12 @@ class Entries extends \ArrayObject implements IEntries
static public function getXMLNS($useLavoisierXMLNS = true) static public function getXMLNS($useLavoisierXMLNS = true)
{ {
if (true === $useLavoisierXMLNS) { if (true === $useLavoisierXMLNS) {
return self::XMLNS_entries; return IEntries::XMLNS_entries;
} else { } else {
return self::XMLNS_ex_entries; return IEntries::XMLNS_ex_entries;
} }
} }
public function init()
{
}
public function pop() public function pop()
{ {
......
...@@ -6,6 +6,12 @@ namespace Lavoisier\Entries; ...@@ -6,6 +6,12 @@ namespace Lavoisier\Entries;
*/ */
interface IEntries { interface IEntries {
// original simple entries/entry Lavoisier xsd (it's a subset of the following one)
const XMLNS_entries = "http://software.in2p3.fr/lavoisier/entries.xsd";
// extended recursive entries/entries/.../entries/entry lavoisiercli xsd
const XMLNS_ex_entries = "https://gitlab.in2p3.fr/opsportal/lavoisiercli/raw/master/resources/ex_entries.xsd";
function init(); function init();
} }
...@@ -2,16 +2,43 @@ ...@@ -2,16 +2,43 @@
namespace Lavoisier\Hydrators; namespace Lavoisier\Hydrators;
use \Lavoisier\Entries\Entries;
class SimpleXMLHydrator implements IHydrator class SimpleXMLHydrator implements IHydrator
{ {
private $prefix;
private $ns;
function __construct()
{
$this->prefix = '';
$this->ns = '';
}
public function hydrate($str) public function hydrate($str)
{ {
$sxObject = simplexml_load_string($str, '\SimpleXMLElement'); $sxObject = simplexml_load_string(
$str,
'\SimpleXMLElement',
0,
$this->prefix,
((empty($this->prefix)) ? false : true)
);
if ($sxObject === false) { if ($sxObject === false) {
throw new \Exception('Unable to parse XML'); throw new \Exception('Unable to parse XML');
} }
return $sxObject; return $sxObject;
} }
}
\ No newline at end of file public function registerNamespace($prefix, $ns)
{
$this->prefix = $prefix;
$this->ns = $ns;
}
public function registerEntriesNamespace($useLavoisierXMLNS = true)
{
$this->registerNamespace('e', Entries::getXMLNS($useLavoisierXMLNS));
}
}
...@@ -38,8 +38,7 @@ class EntriesTest extends \PHPUnit_Framework_TestCase ...@@ -38,8 +38,7 @@ class EntriesTest extends \PHPUnit_Framework_TestCase
$input = file_get_contents(dirname(__FILE__) . '/Resources/tickets.xml'); $input = file_get_contents(dirname(__FILE__) . '/Resources/tickets.xml');
$hydrator = new \Lavoisier\Hydrators\EntriesHydrator("\TicketingSystem\Ticket\GgusFields"); $hydrator = new \Lavoisier\Hydrators\EntriesHydrator("\TicketingSystem\Ticket\GgusFields");
$coll = $hydrator->hydrate($input); $coll = $hydrator->hydrate($input);
//print_r($coll);
// print_r($coll);
} }
...@@ -47,7 +46,10 @@ class EntriesTest extends \PHPUnit_Framework_TestCase ...@@ -47,7 +46,10 @@ class EntriesTest extends \PHPUnit_Framework_TestCase
{ {
$this->assertEquals("http://software.in2p3.fr/lavoisier/entries.xsd", Entries::getXMLNS()); $this->assertEquals("http://software.in2p3.fr/lavoisier/entries.xsd", Entries::getXMLNS());
$this->assertEquals("https://gitlab.in2p3.fr/opsportal/lavoisiercli/raw/master/resources/ex_entries.xsd", Entries::getXMLNS(true)); $this->assertEquals(
"https://gitlab.in2p3.fr/opsportal/lavoisiercli/raw/master/resources/ex_entries.xsd",
Entries::getXMLNS(true)
);
} }
} }
\ No newline at end of file
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
class SimpleXMLHydratorTest extends PHPUnit_Framework_TestCase class SimpleXMLHydratorTest extends PHPUnit_Framework_TestCase
{ {
public function testGlobal() public function testXMLnoNS()
{ {
$input = <<<EOF $input = <<<EOF
...@@ -22,7 +22,18 @@ EOF; ...@@ -22,7 +22,18 @@ EOF;
$lh = new \Lavoisier\Hydrators\SimpleXMLHydrator(); $lh = new \Lavoisier\Hydrators\SimpleXMLHydrator();
$simpleXMLObj = $lh->hydrate($input); $simpleXMLObj = $lh->hydrate($input);
$this->assertEquals($simpleXMLObj->child1, '1st child text node'); $this->assertEquals('1st child text node', strval($simpleXMLObj->child1));
}
public function testXMLwithNS()
{
$input = file_get_contents(dirname(__FILE__) . '/Resources/tickets.xml');
$hydrator = new \Lavoisier\Hydrators\SimpleXMLHydrator();
$hydrator->registerEntriesNamespace(false);
$simpleXMLObj = $hydrator->hydrate($input);
$this->assertEquals(50455, strval($simpleXMLObj->entries[0]->entry[0]));
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment