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

Core/FieldType/Time/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Time;
8
9 use eZ\Publish\Core\FieldType\FieldType;
10 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
11 use eZ\Publish\Core\FieldType\ValidationError;
12 use eZ\Publish\SPI\FieldType\Value as SPIValue;
13 use eZ\Publish\Core\FieldType\Value as BaseValue;
14 use DateTime;
15
16 class Type extends FieldType
17 {
21 const DEFAULT_EMPTY = 0;
22
26 const DEFAULT_CURRENT_TIME = 1;
27
28 protected $settingsSchema = [
29 'useSeconds' => [
30 'type' => 'bool',
31 'default' => false,
32 ],
33 // One of the DEFAULT_* class constants
34 'defaultType' => [
35 'type' => 'choice',
36 'default' => self::DEFAULT_EMPTY,
37 ],
38 ];
39
45 public function getFieldTypeIdentifier()
46 {
47 return 'eztime';
48 }
49
60 public function getName(SPIValue $value)
61 {
62 if ($this->isEmptyValue($value)) {
63 return '';
64 }
65
66 $dateTime = new DateTime("@{$value->time}");
67
68 return $dateTime->format('g:i:s a');
69 }
70
77 public function getEmptyValue()
78 {
79 return new Value();
80 }
81
89 protected function createValueFromInput($inputValue)
90 {
91 if (is_string($inputValue)) {
92 $inputValue = Value::fromString($inputValue);
93 }
94
95 if (is_int($inputValue)) {
96 $inputValue = Value::fromTimestamp($inputValue);
97 }
98
99 if ($inputValue instanceof DateTime) {
100 $inputValue = Value::fromDateTime($inputValue);
101 }
102
103 return $inputValue;
104 }
105
113 protected function checkValueStructure(BaseValue $value)
114 {
115 if (!is_int($value->time)) {
116 throw new InvalidArgumentType(
117 '$value->time',
118 'DateTime',
119 $value->time
120 );
121 }
122 }
123
131 protected function getSortInfo(BaseValue $value)
132 {
133 return $value->time;
134 }
135
143 public function fromHash($hash)
144 {
145 if ($hash === null) {
146 return $this->getEmptyValue();
147 }
148
149 return new Value($hash);
150 }
151
160 public function isEmptyValue(SPIValue $value)
161 {
162 if ($value->time === null) {
163 return true;
164 }
165
166 return false;
167 }
168
176 public function toHash(SPIValue $value)
177 {
178 if ($this->isEmptyValue($value)) {
179 return null;
180 }
181
182 return $value->time;
183 }
184
190 public function isSearchable()
191 {
192 return true;
193 }
194
202 public function validateFieldSettings($fieldSettings)
203 {
204 $validationErrors = [];
205
206 foreach ($fieldSettings as $name => $value) {
207 if (!isset($this->settingsSchema[$name])) {
208 $validationErrors[] = new ValidationError(
209 "Setting '%setting%' is unknown",
210 null,
211 [
212 '%setting%' => $name,
213 ],
214 "[$name]"
215 );
216 continue;
217 }
218
219 switch ($name) {
220 case 'useSeconds':
221 if (!is_bool($value)) {
222 $validationErrors[] = new ValidationError(
223 "Setting '%setting%' value must be of boolean type",
224 null,
225 [
226 '%setting%' => $name,
227 ],
228 "[$name]"
229 );
230 }
231 break;
232 case 'defaultType':
233 $definedTypes = [
234 self::DEFAULT_EMPTY,
235 self::DEFAULT_CURRENT_TIME,
236 ];
237 if (!in_array($value, $definedTypes, true)) {
238 $validationErrors[] = new ValidationError(
239 "Setting '%setting%' is of unknown type",
240 null,
241 [
242 '%setting%' => $name,
243 ],
244 "[$name]"
245 );
246 }
247 break;
248 }
249 }
250
251 return $validationErrors;
252 }
253 }
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\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\Time\Type
Definition: Core/FieldType/Time/Type.php:17
eZ\Publish\Core\FieldType\Time\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Time/Type.php:89
eZ\Publish\Core\FieldType\Time\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/Time/Type.php:28
eZ\Publish\Core\FieldType\Time\Type\DEFAULT_CURRENT_TIME
const DEFAULT_CURRENT_TIME
Default value type current date.
Definition: Core/FieldType/Time/Type.php:26
eZ\Publish\Core\FieldType\Time\Type\DEFAULT_EMPTY
const DEFAULT_EMPTY
Default value type empty.
Definition: Core/FieldType/Time/Type.php:21
eZ\Publish\Core\FieldType\Time\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Time/Type.php:45
eZ\Publish\Core\FieldType\Time\Type\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/Time/Type.php:160
eZ\Publish\Core\FieldType\Time\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Time/Type.php:77
eZ\Publish\Core\FieldType\Time\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/Time/Type.php:190
eZ\Publish\Core\FieldType\Time\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Time/Type.php:113
eZ\Publish\Core\FieldType\Time\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Time/Type.php:143
eZ\Publish\Core\FieldType\Time\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/Time/Type.php:202
eZ\Publish\Core\FieldType\Time\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Time/Type.php:176
eZ\Publish\Core\FieldType\Time\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Time/Type.php:60
eZ\Publish\Core\FieldType\Time\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/Time/Type.php:131
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\Time
Definition: Time/SearchField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.