Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

IntegerConverter::toStorageFieldDefinition()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
nc 20
nop 2
dl 0
loc 22
rs 6.9811
c 1
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
    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
        if (isset($fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['maxIntegerValue'])) {
74
            $storageDef->dataInt2 = $fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER]['maxIntegerValue'];
75
        }
76
77
        if ($storageDef->dataInt1 === false || $storageDef->dataInt2 === false) {
78
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
79
                "The IntegerValueValidator constraint value 'false' is deprecated, and will be removed in 7.0. Use 'null' instead.",
80
                E_USER_DEPRECATED
81
            );
82
            $storageDef->dataInt1 = $storageDef->dataInt1 === false ? null : $storageDef->dataInt1;
83
            $storageDef->dataInt2 = $storageDef->dataInt2 === false ? null : $storageDef->dataInt2;
84
        }
85
86
        // Defining dataInt4 which holds the validator state (min value/max value/minMax value)
87
        $storageDef->dataInt4 = $this->getStorageDefValidatorState($storageDef->dataInt1, $storageDef->dataInt2);
88
        $storageDef->dataInt3 = $fieldDef->defaultValue->data;
89
    }
90
91
    /**
92
     * Converts field definition data in $storageDef into $fieldDef.
93
     *
94
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
95
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
96
     */
97
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
98
    {
99
        $validatorParameters = array('minIntegerValue' => null, 'maxIntegerValue' => null);
100
        if ($storageDef->dataInt4 & self::HAS_MIN_VALUE) {
101
            $validatorParameters['minIntegerValue'] = $storageDef->dataInt1;
102
        }
103
104
        if ($storageDef->dataInt4 & self::HAS_MAX_VALUE) {
105
            $validatorParameters['maxIntegerValue'] = $storageDef->dataInt2;
106
        }
107
        $fieldDef->fieldTypeConstraints->validators[self::FLOAT_VALIDATOR_IDENTIFIER] = $validatorParameters;
108
        $fieldDef->defaultValue->data = $storageDef->dataInt3;
109
        $fieldDef->defaultValue->sortKey = ($storageDef->dataInt3 === null ? 0 : $storageDef->dataInt3);
110
    }
111
112
    /**
113
     * Returns the name of the index column in the attribute table.
114
     *
115
     * Returns the name of the index column the datatype uses, which is either
116
     * "sort_key_int" or "sort_key_string". This column is then used for
117
     * filtering and sorting for this type.
118
     *
119
     * @return string
120
     */
121
    public function getIndexColumn()
122
    {
123
        return 'sort_key_int';
124
    }
125
126
    /**
127
     * Returns validator state for storage definition.
128
     * Validator state is a bitfield value composed of:
129
     *   - {@link self::HAS_MAX_VALUE}
130
     *   - {@link self::HAS_MIN_VALUE}.
131
     *
132
     * @param int|null $minValue Minimum int value, or null if not set
133
     * @param int|null $maxValue Maximum int value, or null if not set
134
     *
135
     * @return int
136
     */
137 View Code Duplication
    private function getStorageDefValidatorState($minValue, $maxValue)
138
    {
139
        $state = 0;
140
141
        if ($minValue !== null) {
142
            $state |= self::HAS_MIN_VALUE;
143
        }
144
145
        if ($maxValue !== null) {
146
            $state |= self::HAS_MAX_VALUE;
147
        }
148
149
        return $state;
150
    }
151
}
152