eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageFieldDefinition PHP Method

toStorageFieldDefinition() public method

Converts field definition data in $fieldDef into $storageFieldDef.
public toStorageFieldDefinition ( eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef, StorageFieldDefinition $storageDef )
$fieldDef eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
$storageDef eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
    {
        $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
        if (isset($fieldSettings['isMultiple'])) {
            $storageDef->dataInt1 = (int) $fieldSettings['isMultiple'];
        }
        if (!empty($fieldSettings['options'])) {
            $xml = new DOMDocument('1.0', 'utf-8');
            $xml->appendChild($selection = $xml->createElement('ezselection'));
            $selection->appendChild($options = $xml->createElement('options'));
            foreach ($fieldSettings['options'] as $id => $name) {
                $options->appendChild($option = $xml->createElement('option'));
                $option->setAttribute('id', $id);
                $option->setAttribute('name', $name);
            }
            $storageDef->dataText5 = $xml->saveXML();
        }
    }

Usage Example

    /**
     * @group fieldType
     * @group selection
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageFieldDefinition
     */
    public function testToStorageFieldDefinitionSingle()
    {
        $fieldDefinition = new PersistenceFieldDefinition(array('fieldTypeConstraints' => new FieldTypeConstraints(array('fieldSettings' => new FieldSettings(array('isMultiple' => false, 'options' => array(0 => 'First')))))));
        $expectedStorageFieldDefinition = new StorageFieldDefinition();
        $expectedStorageFieldDefinition->dataInt1 = 0;
        $expectedStorageFieldDefinition->dataText5 = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<ezselection><options><option id="0" name="First"/></options></ezselection>

EOT;
        $actualStorageFieldDefinition = new StorageFieldDefinition();
        $this->converter->toStorageFieldDefinition($fieldDefinition, $actualStorageFieldDefinition);
        $this->assertEquals($expectedStorageFieldDefinition, $actualStorageFieldDefinition);
    }