Completed
Push — EZP-26494 ( 820e2b...591f1b )
by
unknown
21:34
created

IntegerConverter::toStorageFieldDefinition()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Integer 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
12
13
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
14
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
15
use eZ\Publish\SPI\Persistence\Content\FieldValue;
16
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
17
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
18
19
class IntegerConverter implements Converter
20
{
21
    const FLOAT_VALIDATOR_IDENTIFIER = 'IntegerValueValidator';
22
23
    const HAS_MIN_VALUE = 1;
24
    const HAS_MAX_VALUE = 2;
25
26
    /**
27
     * Factory for current class.
28
     *
29
     * @note Class should instead be configured as service if it gains dependencies.
30
     *
31
     * @return Integer
32
     */
33
    public static function create()
34
    {
35
        return new self();
36
    }
37
38
    /**
39
     * Converts data from $value to $storageFieldValue.
40
     *
41
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
42
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
43
     */
44
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
45
    {
46
        $storageFieldValue->dataInt = $value->data;
47
        $storageFieldValue->sortKeyInt = (int)$value->sortKey;
48
    }
49
50
    /**
51
     * Converts data from $value to $fieldValue.
52
     *
53
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
54
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
55
     */
56
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
57
    {
58
        $fieldValue->data = $value->dataInt;
59
        $fieldValue->sortKey = $value->sortKeyInt;
60
    }
61
62
    /**
63
     * Converts field definition data in $fieldDef into $storageFieldDef.
64
     *
65
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
66
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
67
     */
68 View Code Duplication
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
69
    {
70
        if (isset($fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['minIntegerValue'])) {
71
            $storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['minIntegerValue'];
72
        }
73
74
        if (isset($fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['maxIntegerValue'])) {
75
            $storageDef->dataInt2 = $fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['maxIntegerValue'];
76
        }
77
78
        // Defining dataInt4 which holds the validator state (min value/max value/minMax value)
79
        $storageDef->dataInt4 = $this->getStorageDefValidatorState($storageDef->dataInt1, $storageDef->dataInt2);
80
        $storageDef->dataInt3 = $fieldDef->defaultValue->data;
81
    }
82
83
    /**
84
     * Converts field definition data in $storageDef into $fieldDef.
85
     *
86
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
87
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
88
     */
89
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
90
    {
91
        $validatorParameters = array('minIntegerValue' => null, 'maxIntegerValue' => null);
92
        if ($storageDef->dataInt4 & self::HAS_MIN_VALUE) {
93
            $validatorParameters['minIntegerValue'] = $storageDef->dataInt1;
94
        }
95
96
        if ($storageDef->dataInt4 & self::HAS_MAX_VALUE) {
97
            $validatorParameters['maxIntegerValue'] = $storageDef->dataInt2;
98
        }
99
        $fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER] = $validatorParameters;
100
        $fieldDef->defaultValue->data = $storageDef->dataInt3;
101
        $fieldDef->defaultValue->sortKey = ($storageDef->dataInt3 === null ? 0 : $storageDef->dataInt3);
102
    }
103
104
    /**
105
     * Returns the name of the index column in the attribute table.
106
     *
107
     * Returns the name of the index column the datatype uses, which is either
108
     * "sort_key_int" or "sort_key_string". This column is then used for
109
     * filtering and sorting for this type.
110
     *
111
     * @return string
112
     */
113
    public function getIndexColumn()
114
    {
115
        return 'sort_key_int';
116
    }
117
118
    /**
119
     * Returns validator state for storage definition.
120
     * Validator state is a bitfield value composed of:
121
     *   - {@link self::HAS_MAX_VALUE}
122
     *   - {@link self::HAS_MIN_VALUE}.
123
     *
124
     * @param int|null $minValue Minimum int value, or null if not set
125
     * @param int|null $maxValue Maximum int value, or null if not set
126
     *
127
     * @return int
128
     */
129 View Code Duplication
    private function getStorageDefValidatorState($minValue, $maxValue)
130
    {
131
        $state = 0;
132
133
        if ($minValue !== null) {
134
            $state |= self::HAS_MIN_VALUE;
135
        }
136
137
        if ($maxValue !== null) {
138
            $state |= self::HAS_MAX_VALUE;
139
        }
140
141
        return $state;
142
    }
143
}
144