Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

DateAndTimeConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
8
9 use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
10 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
11 use eZ\Publish\SPI\Persistence\Content\FieldValue;
12 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
13 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
14 use eZ\Publish\Core\FieldType\DateAndTime\Type as DateAndTimeType;
15 use eZ\Publish\Core\FieldType\FieldSettings;
16 use DateTime;
17 use DateInterval;
18 use DOMDocument;
19 use SimpleXMLElement;
20
21 class DateAndTimeConverter implements Converter
22 {
32 public static function create()
33 {
34 return new self();
35 }
36
43 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
44 {
45 // @todo: One should additionally store the timezone here. This could
46 // be done in a backwards compatible way, I think…
47 $storageFieldValue->dataInt = ($value->data !== null ? $value->data['timestamp'] : null);
48 $storageFieldValue->sortKeyInt = (int)$value->sortKey;
49 }
50
57 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
58 {
59 if ($value->dataInt === null) {
60 return;
61 }
62
63 $fieldValue->data = [
64 'rfc850' => null,
65 'timestamp' => $value->dataInt,
66 ];
67 $fieldValue->sortKey = $value->sortKeyInt;
68 }
69
76 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
77 {
78 $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
79 if ($fieldSettings === null) {
80 return;
81 }
82
83 $storageDef->dataInt2 = $fieldSettings['useSeconds'] ? 1 : 0;
84 $storageDef->dataInt1 = $fieldSettings['defaultType'];
85
86 if ($fieldSettings['defaultType'] === DateAndTimeType::DEFAULT_CURRENT_DATE_ADJUSTED) {
87 $storageDef->dataText5 = $this->generateDateIntervalXML($fieldSettings['dateInterval']);
88 }
89 }
90
97 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
98 {
99 $useSeconds = (bool)$storageDef->dataInt2;
100 $dateInterval = $this->getDateIntervalFromXML($storageDef->dataText5);
101
102 $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
103 [
104 'defaultType' => $storageDef->dataInt1,
105 'useSeconds' => $useSeconds,
106 'dateInterval' => $dateInterval,
107 ]
108 );
109
110 // Building default value
111 switch ($fieldDef->fieldTypeConstraints->fieldSettings['defaultType']) {
112 case DateAndTimeType::DEFAULT_CURRENT_DATE:
113 $data = [
114 'rfc850' => null,
115 'timestamp' => time(), // @deprecated timestamp is no longer used and will be removed in a future version.
116 'timestring' => 'now',
117 ];
118 break;
119
120 case DateAndTimeType::DEFAULT_CURRENT_DATE_ADJUSTED:
121 if (!$useSeconds) {
122 $dateInterval->s = 0;
123 }
124 $date = new DateTime();
125 $date->add($dateInterval);
126 $data = [
127 'rfc850' => null,
128 'timestamp' => $date->getTimestamp(), // @deprecated timestamp is no longer used and will be removed in a future version.
129 'timestring' => $dateInterval->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds'),
130 ];
131 break;
132
133 default:
134 $data = null;
135 }
136
137 $fieldDef->defaultValue->data = $data;
138 }
139
149 public function getIndexColumn()
150 {
151 return 'sort_key_int';
152 }
153
161 protected function generateDateIntervalXML(DateInterval $dateInterval)
162 {
163 // Constructing XML structure
164 $doc = new DOMDocument('1.0', 'utf-8');
165 $root = $doc->createElement('adjustment');
166
167 $year = $doc->createElement('year');
168 $year->setAttribute('value', $dateInterval->format('%y'));
169 $root->appendChild($year);
170 unset($year);
171
172 $month = $doc->createElement('month');
173 $month->setAttribute('value', $dateInterval->format('%m'));
174 $root->appendChild($month);
175 unset($month);
176
177 $day = $doc->createElement('day');
178 $day->setAttribute('value', $dateInterval->format('%d'));
179 $root->appendChild($day);
180 unset($day);
181
182 $hour = $doc->createElement('hour');
183 $hour->setAttribute('value', $dateInterval->format('%h'));
184 $root->appendChild($hour);
185 unset($hour);
186
187 $minute = $doc->createElement('minute');
188 $minute->setAttribute('value', $dateInterval->format('%i'));
189 $root->appendChild($minute);
190 unset($minute);
191
192 $second = $doc->createElement('second');
193 $second->setAttribute('value', $dateInterval->format('%s'));
194 $root->appendChild($second);
195 unset($second);
196
197 $doc->appendChild($root);
198
199 return $doc->saveXML();
200 }
201
209 protected function getDateIntervalFromXML($xmlText)
210 {
211 if (empty($xmlText)) {
212 return;
213 }
214
215 $xml = new SimpleXMLElement($xmlText);
216 $aIntervalString = [
217 (int)$xml->year['value'] . ' years',
218 (int)$xml->month['value'] . ' months',
219 (int)$xml->day['value'] . ' days',
220 (int)$xml->hour['value'] . ' hours',
221 (int)$xml->minute['value'] . ' minutes',
222 (int)$xml->second['value'] . ' seconds',
223 ];
224
225 return DateInterval::createFromDateString(implode(', ', $aIntervalString));
226 }
227 }
eZ\Publish\Core\FieldType\DateAndTime\Type
Definition: Core/FieldType/DateAndTime/Type.php:18
eZ\Publish\Core\FieldType\FieldSettings
Container for field type specific properties.
Definition: FieldSettings.php:19
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter
Definition: DateAndTimeConverter.php:22
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\generateDateIntervalXML
generateDateIntervalXML(DateInterval $dateInterval)
Generates the internal XML structure for $dateInterval, used for date adjustment.
Definition: DateAndTimeConverter.php:161
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: DateAndTimeConverter.php:97
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\getDateIntervalFromXML
getDateIntervalFromXML($xmlText)
Generates a DateInterval object from $xmlText.
Definition: DateAndTimeConverter.php:209
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: DateAndTimeConverter.php:43
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\create
static create()
Factory for current class.
Definition: DateAndTimeConverter.php:32
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: DateAndTimeConverter.php:57
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: DateAndTimeConverter.php:76
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateAndTimeConverter\getIndexColumn
getIndexColumn()
Returns the name of the index column in the attribute table.
Definition: DateAndTimeConverter.php:149
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
Definition: StorageFieldDefinition.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue\$data
$data
Definition: FieldValue.php:23
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Converter for field values in legacy storage.
Definition: Persistence/Legacy/Content/FieldValue/Converter.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.