Completed
Push — ezp-29724 ( 1025e5...495423 )
by
unknown
30:13 queued 07:05
created

SelectionConverter::toFieldDefinition()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 2
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Selection converter.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
10
11
use eZ\Publish\Core\FieldType\FieldSettings;
12
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
13
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
15
use eZ\Publish\SPI\Persistence\Content\FieldValue;
16
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
17
use DOMDocument;
18
19
class SelectionConverter implements Converter
20
{
21
    /**
22
     * Factory for current class.
23
     *
24
     * Note: Class should instead be configured as service if it gains dependencies.
25
     *
26
     * @deprecated since 6.8, will be removed in 7.x, use default constructor instead.
27
     *
28
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter
29
     */
30
    public static function create()
31
    {
32
        return new self();
33
    }
34
35
    /**
36
     * Converts data from $value to $storageFieldValue.
37
     *
38
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
39
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
40
     */
41
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
42
    {
43
        $storageFieldValue->sortKeyString = $storageFieldValue->dataText = $value->sortKey;
44
    }
45
46
    /**
47
     * Converts data from $value to $fieldValue.
48
     *
49
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
50
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
51
     */
52
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
53
    {
54
        if ($value->dataText !== '') {
55
            $fieldValue->data = array_map(
56
                'intval',
57
                explode('-', $value->dataText)
58
            );
59
        } else {
60
            $fieldValue->data = array();
61
        }
62
        $fieldValue->sortKey = $value->sortKeyString;
63
    }
64
65
    /**
66
     * Converts field definition data in $fieldDef into $storageFieldDef.
67
     *
68
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
69
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
70
     */
71
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
72
    {
73
        $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
74
75
        if (isset($fieldSettings['isMultiple'])) {
76
            $storageDef->dataInt1 = (int)$fieldSettings['isMultiple'];
77
        }
78
79
        if (!empty($fieldSettings['options'])) {
80
            $xml = new DOMDocument('1.0', 'utf-8');
81
            $xml->appendChild(
82
                $selection = $xml->createElement('ezselection')
83
            );
84
            $selection->appendChild(
85
                $options = $xml->createElement('options')
86
            );
87
            foreach ($fieldSettings['options'] as $id => $name) {
88
                $options->appendChild(
89
                    $option = $xml->createElement('option')
90
                );
91
                $option->setAttribute('id', $id);
92
                $option->setAttribute('name', $name);
93
            }
94
            $storageDef->dataText5 = $xml->saveXML();
95
        }
96
    }
97
98
    /**
99
     * Converts field definition data in $storageDef into $fieldDef.
100
     *
101
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
102
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
103
     */
104
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
105
    {
106
        $options = array();
107
        $simpleXml = simplexml_load_string($storageDef->dataText5);
108
109
        if ($simpleXml !== false) {
110
            foreach ($simpleXml->options->option as $option) {
111
                $options[(int)$option['id']] = (string)$option['name'];
112
            }
113
        }
114
115
        $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
116
            array(
117
                'isMultiple' => !empty($storageDef->dataInt1) ? (bool)$storageDef->dataInt1 : false,
118
                'options' => $options,
119
            )
120
        );
121
122
        // @todo: Can Selection store a default value in the DB?
123
        $fieldDef->defaultValue = new FieldValue();
124
        $fieldDef->defaultValue->data = array();
125
        $fieldDef->defaultValue->sortKey = '';
126
    }
127
128
    /**
129
     * Returns the name of the index column in the attribute table.
130
     *
131
     * Returns the name of the index column the datatype uses, which is either
132
     * "sort_key_int" or "sort_key_string". This column is then used for
133
     * filtering and sorting for this type.
134
     *
135
     * @return string
136
     */
137
    public function getIndexColumn()
138
    {
139
        return 'sort_key_string';
140
    }
141
}
142