Completed
Push — 6.12 ( 7410cc...aba6b4 )
by André
15:31
created

DateConverter::toFieldValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Date field value converter class.
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\Persistence\Legacy\Content\FieldValue\Converter;
12
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
13
use eZ\Publish\SPI\Persistence\Content\FieldValue;
14
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
15
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
16
use eZ\Publish\Core\FieldType\Date\Type as DateType;
17
use eZ\Publish\Core\FieldType\FieldSettings;
18
19
/**
20
 * Date field value converter class.
21
 */
22
class DateConverter implements Converter
23
{
24
    /**
25
     * Factory for current class.
26
     *
27
     * Note: Class should instead be configured as service if it gains dependencies.
28
     *
29
     * @deprecated since 6.8, will be removed in 7.x, use default constructor instead.
30
     *
31
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateConverter
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 !== null ? $value->data['timestamp'] : null);
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 View Code Duplication
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
57
    {
58
        if ($value->dataInt === null || $value->dataInt == 0) {
59
            return;
60
        }
61
62
        $fieldValue->data = array(
63
            'timestamp' => $value->dataInt,
64
            'rfc850' => null,
65
        );
66
        $fieldValue->sortKey = $value->sortKeyInt;
67
    }
68
69
    /**
70
     * Converts field definition data in $fieldDef into $storageFieldDef.
71
     *
72
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
73
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
74
     */
75
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
76
    {
77
        $storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->fieldSettings['defaultType'];
78
    }
79
80
    /**
81
     * Converts field definition data in $storageDef into $fieldDef.
82
     *
83
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
84
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
85
     */
86
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
87
    {
88
        $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
89
            array(
90
                'defaultType' => $storageDef->dataInt1,
91
            )
92
        );
93
94
        // Building default value
95
        switch ($fieldDef->fieldTypeConstraints->fieldSettings['defaultType']) {
96
            case DateType::DEFAULT_CURRENT_DATE:
97
                $data = array(
98
                    'timestamp' => time(), // @deprecated timestamp is no longer used and will be removed in a future version.
99
                    'rfc850' => null,
100
                    'timestring' => 'now',
101
                );
102
                break;
103
            default:
104
                $data = null;
105
        }
106
107
        $fieldDef->defaultValue->data = $data;
108
    }
109
110
    /**
111
     * Returns the name of the index column in the attribute table.
112
     *
113
     * Returns the name of the index column the datatype uses, which is either
114
     * "sort_key_int" or "sort_key_string". This column is then used for
115
     * filtering and sorting for this type.
116
     *
117
     * @return string
118
     */
119
    public function getIndexColumn()
120
    {
121
        return 'sort_key_int';
122
    }
123
}
124