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

toFieldDefinition() public method

Converts field definition data in $storageDef into $fieldDef.
public toFieldDefinition ( StorageFieldDefinition $storageDef, eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef )
$storageDef eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
$fieldDef eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
    {
        $options = array();
        $simpleXml = simplexml_load_string($storageDef->dataText5);
        if ($simpleXml !== false) {
            foreach ($simpleXml->options->option as $option) {
                $options[(int) $option['id']] = (string) $option['name'];
            }
        }
        $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(array('isMultiple' => !empty($storageDef->dataInt1) ? (bool) $storageDef->dataInt1 : false, 'options' => $options));
        // @todo: Can Selection store a default value in the DB?
        $fieldDef->defaultValue = new FieldValue();
        $fieldDef->defaultValue->data = array();
        $fieldDef->defaultValue->sortKey = '';
    }

Usage Example

    /**
     * @group fieldType
     * @group selection
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toFieldDefinition
     */
    public function testToFieldDefinitionSingleEmpty()
    {
        $storageFieldDefinition = new StorageFieldDefinition();
        $storageFieldDefinition->dataInt1 = 0;
        $storageFieldDefinition->dataText5 = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<ezselection>
  <options>
  </options>
</ezselection>
EOT;
        $expectedFieldDefinition = new PersistenceFieldDefinition(array('fieldTypeConstraints' => new FieldTypeConstraints(array('fieldSettings' => new FieldSettings(array('isMultiple' => false, 'options' => array())))), 'defaultValue' => new FieldValue(array('data' => array()))));
        $actualFieldDefinition = new PersistenceFieldDefinition();
        $this->converter->toFieldDefinition($storageFieldDefinition, $actualFieldDefinition);
        $this->assertEquals($expectedFieldDefinition, $actualFieldDefinition);
    }