eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\PageConverter::generateXmlString PHP Method

generateXmlString() public method

Generates XML string from $page object to be stored in storage engine.
public generateXmlString ( Page $page ) : string
$page eZ\Publish\Core\FieldType\Page\Parts\Page
return string
    public function generateXmlString(Parts\Page $page)
    {
        $dom = new DOMDocument('1.0', 'utf-8');
        $dom->formatOutput = true;
        $dom->loadXML('<page />');
        $pageNode = $dom->documentElement;
        foreach ($page->getState() as $attrName => $attrValue) {
            switch ($attrName) {
                case 'id':
                    $pageNode->setAttribute('id', $attrValue);
                    break;
                case 'zones':
                    foreach ($page->{$attrName} as $zone) {
                        $pageNode->appendChild($this->generateZoneXmlString($zone, $dom));
                    }
                    break;
                case 'layout':
                    $node = $dom->createElement('zone_layout', $attrValue);
                    $pageNode->appendChild($node);
                    break;
                case 'attributes':
                    foreach ($attrValue as $arrayItemKey => $arrayItemValue) {
                        $this->addNewXmlElement($dom, $pageNode, $arrayItemKey, $arrayItemValue);
                    }
                    break;
                case 'zonesById':
                    // Do not store
                    break;
                default:
                    $this->addNewNotEmptyXmlElement($dom, $pageNode, $attrName, $attrValue);
                    break;
            }
        }
        return $dom->saveXML();
    }

Usage Example

Example #1
0
    /**
     * Test converting from XML to storage and back.
     */
    public function testFromStorageAndBack()
    {
        $xml = <<<EOF
<?xml version="1.0"?>
<page>
  <zone id="id_ee94402090bb170600a8dab9e1bd1c5a">
    <block id="id_ef9fee870c65c676b2f7136431b73f37">
      <name>My Block</name>
      <type>my_block_type</type>
      <view>default</view>
      <overflow_id></overflow_id>
      <custom_attributes>
        <copytext></copytext>
      </custom_attributes>
      <rotation>
        <interval>0</interval>
        <type>0</type>
        <value></value>
        <unit></unit>
      </rotation>
      <zone_id>ee94402090bb170600a8dab9e1bd1c5a</zone_id>
    </block>
    <zone_identifier>zone_1</zone_identifier>
  </zone>
  <zone_layout>my_zone_layout</zone_layout>
</page>

EOF;
        $page = $this->converter->restoreValueFromXmlString($xml);
        $newXml = $this->converter->generateXmlString($page);
        $this->assertEquals($xml, $newXml);
    }