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

Core/FieldType/DateAndTime/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\DateAndTime;
8
9 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
10 use eZ\Publish\Core\FieldType\FieldType;
11 use eZ\Publish\Core\FieldType\ValidationError;
12 use eZ\Publish\Core\FieldType\Value as BaseValue;
13 use eZ\Publish\SPI\FieldType\Value as SPIValue;
14 use DateInterval;
15 use DateTime;
16
17 class Type extends FieldType
18 {
22 const DEFAULT_EMPTY = 0;
23 const DEFAULT_CURRENT_DATE = 1;
24 const DEFAULT_CURRENT_DATE_ADJUSTED = 2;
25
26 protected $settingsSchema = [
27 'useSeconds' => [
28 'type' => 'bool',
29 'default' => false,
30 ],
31 // One of the DEFAULT_* class constants
32 'defaultType' => [
33 'type' => 'choice',
34 'default' => self::DEFAULT_EMPTY,
35 ],
36 /*
37 * @var DateInterval
38 * Used only if defaultValueType is set to DEFAULT_CURRENT_DATE_ADJUSTED
39 */
40 'dateInterval' => [
41 'type' => 'dateInterval',
42 'default' => null,
43 ],
44 ];
45
51 public function getFieldTypeIdentifier()
52 {
53 return 'ezdatetime';
54 }
55
66 public function getName(SPIValue $value)
67 {
68 if ($this->isEmptyValue($value)) {
69 return '';
70 }
71
72 return $value->value->format('D Y-d-m H:i:s');
73 }
74
81 public function getEmptyValue()
82 {
83 return new Value();
84 }
85
93 protected function createValueFromInput($inputValue)
94 {
95 if (is_string($inputValue)) {
96 $inputValue = Value::fromString($inputValue);
97 }
98
99 if (is_int($inputValue)) {
100 $inputValue = Value::fromTimestamp($inputValue);
101 }
102
103 if ($inputValue instanceof DateTime) {
104 $inputValue = new Value($inputValue);
105 }
106
107 return $inputValue;
108 }
109
117 protected function checkValueStructure(BaseValue $value)
118 {
119 if (!$value->value instanceof DateTime) {
120 throw new InvalidArgumentType(
121 '$value->value',
122 'DateTime',
123 $value->value
124 );
125 }
126 }
127
135 protected function getSortInfo(BaseValue $value)
136 {
137 if ($value->value === null) {
138 return null;
139 }
140
141 return $value->value->getTimestamp();
142 }
143
154 public function fromHash($hash)
155 {
156 if ($hash === null) {
157 return $this->getEmptyValue();
158 }
159
160 if (isset($hash['rfc850']) && $hash['rfc850']) {
161 return Value::fromString($hash['rfc850']);
162 }
163
164 if (isset($hash['timestring']) && is_string($hash['timestring'])) {
165 return Value::fromString($hash['timestring']);
166 }
167
168 return Value::fromTimestamp((int)$hash['timestamp']);
169 }
170
178 public function toHash(SPIValue $value)
179 {
180 if ($this->isEmptyValue($value)) {
181 return null;
182 }
183
184 if ($value->value instanceof DateTime) {
185 return [
186 'timestamp' => $value->value->getTimestamp(),
187 'rfc850' => $value->value->format(DateTime::RFC850),
188 ];
189 }
190
191 return [
192 'timestamp' => 0,
193 'rfc850' => null,
194 ];
195 }
196
202 public function isSearchable()
203 {
204 return true;
205 }
206
214 public function validateFieldSettings($fieldSettings)
215 {
216 $validationErrors = [];
217
218 foreach ($fieldSettings as $name => $value) {
219 if (isset($this->settingsSchema[$name])) {
220 switch ($name) {
221 case 'useSeconds':
222 if (!is_bool($value)) {
223 $validationErrors[] = new ValidationError(
224 "Setting 'Use seconds' value must be of boolean type",
225 null,
226 [],
227 "[$name]"
228 );
229 }
230 break;
231 case 'defaultType':
232 $definedTypes = [
233 self::DEFAULT_EMPTY,
234 self::DEFAULT_CURRENT_DATE,
235 self::DEFAULT_CURRENT_DATE_ADJUSTED,
236 ];
237 if (!in_array($value, $definedTypes, true)) {
238 $validationErrors[] = new ValidationError(
239 "Setting 'Default value' is of unknown type",
240 null,
241 [],
242 "[$name]"
243 );
244 }
245 break;
246 case 'dateInterval':
247 if (isset($value)) {
248 if ($value instanceof DateInterval) {
249 // String conversion of $value, because DateInterval objects cannot be compared directly
250 if (
251 isset($fieldSettings['defaultType'])
252 && $fieldSettings['defaultType'] !== self::DEFAULT_CURRENT_DATE_ADJUSTED
253 && $value->format('%y%m%d%h%i%s') !== '000000'
254 ) {
255 $validationErrors[] = new ValidationError(
256 "Setting 'Current date and time adjusted by' can be used only when setting 'Default value' is set to 'Adjusted current datetime'",
257 null,
258 [],
259 "[$name]"
260 );
261 }
262 } else {
263 $validationErrors[] = new ValidationError(
264 "Setting 'Current date and time adjusted by' value must be an instance of 'DateInterval' class",
265 null,
266 [],
267 "[$name]"
268 );
269 }
270 }
271 break;
272 }
273 } else {
274 $validationErrors[] = new ValidationError(
275 "Setting '%setting%' is unknown",
276 null,
277 ['%setting%' => $name],
278 "[$name]"
279 );
280 }
281 }
282
283 return $validationErrors;
284 }
285
297 public function fieldSettingsToHash($fieldSettings)
298 {
299 $fieldSettingsHash = parent::fieldSettingsToHash($fieldSettings);
300
301 if (isset($fieldSettingsHash['dateInterval'])) {
302 $fieldSettingsHash['dateInterval'] = $fieldSettingsHash['dateInterval']->format(
303 'P%r%yY%r%mM%r%dDT%r%hH%iM%r%sS'
304 );
305 }
306
307 return $fieldSettingsHash;
308 }
309
324 public function fieldSettingsFromHash($fieldSettingsHash)
325 {
326 $fieldSettings = parent::fieldSettingsFromHash($fieldSettingsHash);
327
328 if (isset($fieldSettings['dateInterval'])) {
329 $fieldSettings['dateInterval'] = new DateInterval($fieldSettings['dateInterval']);
330 }
331
332 return $fieldSettings;
333 }
334 }
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value
Struct that stores extra value information for a Criterion object.
Definition: API/Repository/Values/Content/Query/Criterion/Value.php:13
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
eZ\Publish\Core\FieldType\DateAndTime\Type
Definition: Core/FieldType/DateAndTime/Type.php:18
eZ\Publish\Core\FieldType\DateAndTime\Type\DEFAULT_CURRENT_DATE_ADJUSTED
const DEFAULT_CURRENT_DATE_ADJUSTED
Definition: Core/FieldType/DateAndTime/Type.php:24
eZ\Publish\Core\FieldType\DateAndTime\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/DateAndTime/Type.php:117
eZ\Publish\Core\FieldType\DateAndTime\Type\DEFAULT_CURRENT_DATE
const DEFAULT_CURRENT_DATE
Definition: Core/FieldType/DateAndTime/Type.php:23
eZ\Publish\Core\FieldType\DateAndTime\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/DateAndTime/Type.php:26
eZ\Publish\Core\FieldType\DateAndTime\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/DateAndTime/Type.php:214
eZ\Publish\Core\FieldType\DateAndTime\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/DateAndTime/Type.php:154
eZ\Publish\Core\FieldType\DateAndTime\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/DateAndTime/Type.php:135
eZ\Publish\Core\FieldType\DateAndTime\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/DateAndTime/Type.php:202
eZ\Publish\Core\FieldType\DateAndTime\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/DateAndTime/Type.php:51
eZ\Publish\Core\FieldType\DateAndTime\Type\fieldSettingsToHash
fieldSettingsToHash($fieldSettings)
Converts the given $fieldSettings to a simple hash format.
Definition: Core/FieldType/DateAndTime/Type.php:297
eZ\Publish\Core\FieldType\DateAndTime\Type\DEFAULT_EMPTY
const DEFAULT_EMPTY
Default value types.
Definition: Core/FieldType/DateAndTime/Type.php:22
eZ\Publish\Core\FieldType\DateAndTime\Type\fieldSettingsFromHash
fieldSettingsFromHash($fieldSettingsHash)
Converts the given $fieldSettingsHash to field settings of the type.
Definition: Core/FieldType/DateAndTime/Type.php:324
eZ\Publish\Core\FieldType\DateAndTime\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/DateAndTime/Type.php:81
eZ\Publish\Core\FieldType\DateAndTime\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/DateAndTime/Type.php:66
eZ\Publish\Core\FieldType\DateAndTime\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/DateAndTime/Type.php:178
eZ\Publish\Core\FieldType\DateAndTime\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/DateAndTime/Type.php:93
eZ\Publish\Core\FieldType\FieldType
Base class for field types, the most basic storage unit of data inside eZ Publish.
Definition: Core/FieldType/FieldType.php:35
eZ\Publish\Core\FieldType\FieldType\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/FieldType.php:350
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\FieldType\Value
Abstract class for all field value classes.
Definition: Core/FieldType/Value.php:17
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\DateAndTime
Definition: DateAndTime/SearchField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.