Completed
Push — 6.7 ( 48c307...92f9bc )
by André
13:05
created

DateConverter::getIndexColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateConverter
30
     */
31
    public static function create()
32
    {
33
        return new self();
34
    }
35
36
    /**
37
     * Converts data from $value to $storageFieldValue.
38
     *
39
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value
40
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue
41
     */
42
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
43
    {
44
        $storageFieldValue->dataInt = ($value->data !== null ? $value->data['timestamp'] : null);
45
        $storageFieldValue->sortKeyInt = (int)$value->sortKey;
46
    }
47
48
    /**
49
     * Converts data from $value to $fieldValue.
50
     *
51
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
52
     * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
53
     */
54 View Code Duplication
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
55
    {
56
        if ($value->dataInt === null || $value->dataInt == 0) {
57
            return;
58
        }
59
60
        $fieldValue->data = array(
61
            'timestamp' => $value->dataInt,
62
            'rfc850' => null,
63
        );
64
        $fieldValue->sortKey = $value->sortKeyInt;
65
    }
66
67
    /**
68
     * Converts field definition data in $fieldDef into $storageFieldDef.
69
     *
70
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
71
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
72
     */
73
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
74
    {
75
        $storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->fieldSettings['defaultType'];
76
    }
77
78
    /**
79
     * Converts field definition data in $storageDef into $fieldDef.
80
     *
81
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef
82
     * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef
83
     */
84
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
85
    {
86
        $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
87
            array(
88
                'defaultType' => $storageDef->dataInt1,
89
            )
90
        );
91
92
        // Building default value
93
        switch ($fieldDef->fieldTypeConstraints->fieldSettings['defaultType']) {
94
            case DateType::DEFAULT_CURRENT_DATE:
95
                $data = array(
96
                    'timestamp' => time(), // @deprecated timestamp is no longer used and will be removed in a future version.
97
                    'rfc850' => null,
98
                    'timestring' => 'now',
99
                );
100
                break;
101
            default:
102
                $data = null;
103
        }
104
105
        $fieldDef->defaultValue->data = $data;
106
    }
107
108
    /**
109
     * Returns the name of the index column in the attribute table.
110
     *
111
     * Returns the name of the index column the datatype uses, which is either
112
     * "sort_key_int" or "sort_key_string". This column is then used for
113
     * filtering and sorting for this type.
114
     *
115
     * @return string
116
     */
117
    public function getIndexColumn()
118
    {
119
        return 'sort_key_int';
120
    }
121
}
122