Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

SimpleXMLHydrator.php 912 Bytes
Newer Older
Olivier Lequeux's avatar
Olivier Lequeux committed
1 2 3 4
<?php

namespace Lavoisier\Hydrators;

5
use \Lavoisier\Entries\Entries;
Olivier Lequeux's avatar
Olivier Lequeux committed
6 7 8

class SimpleXMLHydrator implements IHydrator
{
9 10 11 12 13 14 15 16
    private $prefix;
    private $ns;

    function __construct()
    {
        $this->prefix = '';
        $this->ns = '';
    }
Olivier Lequeux's avatar
Olivier Lequeux committed
17 18 19

    public function hydrate($str)
    {
20 21 22 23 24 25 26
        $sxObject = simplexml_load_string(
            $str,
            '\SimpleXMLElement',
            0,
            $this->prefix,
            ((empty($this->prefix)) ? false : true)
        );
Olivier Lequeux's avatar
Olivier Lequeux committed
27 28 29
        if ($sxObject === false) {
            throw new \Exception('Unable to parse XML');
        }
30

Olivier Lequeux's avatar
Olivier Lequeux committed
31 32
        return $sxObject;
    }
33 34 35 36 37 38 39 40 41 42 43 44

    public function registerNamespace($prefix, $ns)
    {
        $this->prefix = $prefix;
        $this->ns = $ns;
    }

    public function registerEntriesNamespace($useLavoisierXMLNS = true)
    {
        $this->registerNamespace('e', Entries::getXMLNS($useLavoisierXMLNS));
    }
}