/
FieldType.php
383 lines (357 loc) · 14.6 KB
/
FieldType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* File containing the FieldType interface.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\SPI\FieldType;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
/**
* The field type interface which all field types have to implement.
*
*
* Hashes:
*
* The {@link toHash()} method in this class is meant to generate a simple
* representation of a value of this field type. Hash does here not refer to
* MD5 or similar hashing algorithms, but rather to hash-map (associative array)
* type representation. This representation must be
* usable, to transfer the value over plain text encoding formats, like e.g.
* XML. As a result, the returned "hash" must either be a scalar value, a hash
* array (associative array) a pure numeric array or a nested combination of
* these. It must by no means contain objects, resources or cyclic references.
* The corresponding {@link fromHash()} method must convert such a
* representation back into a value, which is understood by the FieldType.
*/
abstract class FieldType
{
/**
* Returns the field type identifier for this field type.
*
* This identifier should be globally unique and the implementer of a
* FieldType must take care for the uniqueness. It is therefore recommended
* to prefix the field-type identifier by a unique string that identifies
* the implementer. A good identifier could for example take your companies main
* domain name as a prefix in reverse order.
*
* @return string
*/
abstract public function getFieldTypeIdentifier();
/**
* Returns a human readable string representation from a given value.
*
* It will be used to generate content name and url alias if current field
* is designated to be used in the content name/urlAlias pattern.
*
* The used $value can be assumed to be already accepted by {@link FieldType::acceptValue()}.
*
* @param \eZ\Publish\SPI\FieldType\Value $value
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
* @param string $languageCode
*
* @return string
*/
abstract public function getName(Value $value, FieldDefinition $fieldDefinition, string $languageCode): string;
/**
* Returns a schema for the settings expected by the FieldType.
*
* Returns an arbitrary value, representing a schema for the settings of
* the FieldType.
*
* Explanation: There are no possible generic schemas for defining settings
* input, which is why no schema for the return value of this method is
* defined. It is up to the implementer to define and document a schema for
* the return value and document it. In addition, it is necessary that all
* consumers of this interface (e.g. Public API, REST API, GUIs, ...)
* provide plugin mechanisms to hook adapters for the specific FieldType
* into. These adapters then need to be either shipped with the FieldType
* or need to be implemented by a third party. If there is no adapter
* available for a specific FieldType, it will not be usable with the
* consumer.
*
* @return mixed
*/
abstract public function getSettingsSchema();
/**
* Returns a schema for the validator configuration expected by the FieldType.
*
* Returns an arbitrary value, representing a schema for the validator
* configuration of the FieldType.
*
* Explanation: There are no possible generic schemas for defining settings
* input, which is why no schema for the return value of this method is
* defined. It is up to the implementer to define and document a schema for
* the return value and document it. In addition, it is necessary that all
* consumers of this interface (e.g. Public API, REST API, GUIs, ...)
* provide plugin mechanisms to hook adapters for the specific FieldType
* into. These adapters then need to be either shipped with the FieldType
* or need to be implemented by a third party. If there is no adapter
* available for a specific FieldType, it will not be usable with the
* consumer.
*
* Best practice:
*
* It is considered best practice to return a hash map, which contains
* rudimentary settings structures, like e.g. for the "ezstring" FieldType
*
* <code>
* array(
* 'stringLength' => array(
* 'minStringLength' => array(
* 'type' => 'int',
* 'default' => 0,
* ),
* 'maxStringLength' => array(
* 'type' => 'int'
* 'default' => null,
* )
* ),
* );
* </code>
*
* @return mixed
*/
abstract public function getValidatorConfigurationSchema();
/**
* Validates a field based on the validator configuration in the field definition.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDef The field definition of the field
* @param \eZ\Publish\SPI\FieldType\Value $value The field value for which an action is performed
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
abstract public function validate(FieldDefinition $fieldDef, Value $value);
/**
* Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* This methods determines if the given $validatorConfiguration is
* structurally correct and complies to the validator configuration schema
* returned by {@link getValidatorConfigurationSchema()}.
*
* @param mixed $validatorConfiguration
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
abstract public function validateValidatorConfiguration($validatorConfiguration);
/**
* Applies the default values to the given $validatorConfiguration of a FieldDefinitionCreateStruct.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param mixed $validatorConfiguration
*/
abstract public function applyDefaultValidatorConfiguration(&$validatorConfiguration);
/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* This methods determines if the given $fieldSettings are structurally
* correct and comply to the settings schema returned by {@link * getSettingsSchema()}.
*
* @param mixed $fieldSettings
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
abstract public function validateFieldSettings($fieldSettings);
/**
* Applies the default values to the fieldSettings of a FieldDefinitionCreateStruct.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param mixed $fieldSettings
*/
abstract public function applyDefaultSettings(&$fieldSettings);
/**
* Indicates if the field type supports indexing and sort keys for searching.
*
* @return bool
*/
abstract public function isSearchable();
/**
* Indicates if the field definition of this type can appear only once in the same ContentType.
*
* @return bool
*/
abstract public function isSingular();
/**
* Indicates if the field definition of this type can be added to a ContentType with Content instances.
*
* @return bool
*/
abstract public function onlyEmptyInstance();
/**
* Returns the empty value for this field type.
*
* This value will be used, if no value was provided for a field of this
* type and no default value was specified in the field definition. It is
* also used to determine that a user intentionally (or unintentionally) did not
* set a non-empty value.
*
* @return \eZ\Publish\SPI\FieldType\Value
*/
abstract public function getEmptyValue();
/**
* Returns if the given $value is considered empty by the field type.
*
* Usually, only the value returned by {@link getEmptyValue()} is
* considered empty. The given $value can be safely assumed to have already
* been processed by {@link acceptValue()}.
*
* @param \eZ\Publish\SPI\FieldType\Value $value
*
* @return bool
*/
abstract public function isEmptyValue(Value $value);
/**
* Potentially builds and checks the type and structure of the $inputValue.
*
* This method first inspects $inputValue and convert it into a dedicated
* value object.
*
* After that, the value is checked for structural validity.
* Note that this does not include validation after the rules
* from validators, but only plausibility checks for the general data
* format.
*
* Note that this method must also cope with the empty value for the field
* type as e.g. returned by {@link getEmptyValue()}.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the parameter is not of the supported value sub type
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the value does not match the expected structure
*
* @param mixed $inputValue
*
* @return \eZ\Publish\SPI\FieldType\Value The potentially converted and structurally plausible value.
*/
abstract public function acceptValue($inputValue);
/**
* Converts an $hash to the Value defined by the field type.
*
* This is the reverse operation to {@link toHash()}. At least the hash
* format generated by {@link toHash()} must be converted in reverse.
* Additional formats might be supported in the rare case that this is
* necessary. See the class description for more details on a hash format.
*
* @param mixed $hash
*
* @return \eZ\Publish\SPI\FieldType\Value
*/
abstract public function fromHash($hash);
/**
* Converts the given $value into a plain hash format.
*
* Converts the given $value into a plain hash format, which can be used to
* transfer the value through plain text formats, e.g. XML, which do not
* support complex structures like objects. See the class level doc block
* for additional information. See the class description for more details on a hash format.
*
* @param \eZ\Publish\SPI\FieldType\Value $value
*
* @return mixed
*/
abstract public function toHash(Value $value);
/**
* Converts the given $fieldSettings to a simple hash format.
*
* See the class description for more details on a hash format.
*
* @param mixed $fieldSettings
*
* @return array|hash|scalar|null
*/
abstract public function fieldSettingsToHash($fieldSettings);
/**
* Converts the given $fieldSettingsHash to field settings of the type.
*
* This is the reverse operation of {@link fieldSettingsToHash()}.
* See the class description for more details on a hash format.
*
* @param array|hash|scalar|null $fieldSettingsHash
*
* @return mixed
*/
abstract public function fieldSettingsFromHash($fieldSettingsHash);
/**
* Converts the given $validatorConfiguration to a simple hash format.
*
* See the class description for more details on a hash format.
*
* @param mixed $validatorConfiguration
*
* @return array|hash|scalar|null
*/
abstract public function validatorConfigurationToHash($validatorConfiguration);
/**
* Converts the given $validatorConfigurationHash to a validator
* configuration of the type.
*
* See the class description for more details on a hash format.
*
* @param array|hash|scalar|null $validatorConfigurationHash
*
* @return mixed
*/
abstract public function validatorConfigurationFromHash($validatorConfigurationHash);
/**
* Converts a $value to a persistence value.
*
* In this method the field type puts the data which is stored in the field of content in the repository
* into the property FieldValue::data. The format of $data is a primitive, an array (map) or an object, which
* is then canonically converted to e.g. json/xml structures by future storage engines without
* further conversions. For mapping the $data to the legacy database an appropriate Converter
* (implementing eZ\Publish\Core\Persistence\Legacy\FieldValue\Converter) has implemented for the field
* type. Note: $data should only hold data which is actually stored in the field. It must not
* hold data which is stored externally.
*
* The $externalData property in the FieldValue is used for storing data externally by the
* FieldStorage interface method storeFieldData.
*
* The FieldValuer::sortKey is build by the field type for using by sort operations.
*
* @see \eZ\Publish\SPI\Persistence\Content\FieldValue
*
* @param \eZ\Publish\SPI\FieldType\Value $value The value of the field type
*
* @return \eZ\Publish\SPI\Persistence\Content\FieldValue the value processed by the storage engine
*/
abstract public function toPersistenceValue(Value $value);
/**
* Converts a persistence $value to a Value.
*
* This method builds a field type value from the $data and $externalData properties.
*
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
*
* @return \eZ\Publish\SPI\FieldType\Value
*/
abstract public function fromPersistenceValue(FieldValue $fieldValue);
/**
* Returns relation data extracted from value.
*
* Not intended for \eZ\Publish\API\Repository\Values\Content\Relation::COMMON type relations,
* there is an API for handling those.
*
* @param \eZ\Publish\SPI\FieldType\Value $value
*
* @return array Hash with relation type as key and array of destination content ids as value.
*
* Example:
* <code>
* array(
* \eZ\Publish\API\Repository\Values\Content\Relation::LINK => array(
* "contentIds" => array( 12, 13, 14 ),
* "locationIds" => array( 24 )
* ),
* \eZ\Publish\API\Repository\Values\Content\Relation::EMBED => array(
* "contentIds" => array( 12 ),
* "locationIds" => array( 24, 45 )
* ),
* \eZ\Publish\API\Repository\Values\Content\Relation::FIELD => array( 12 )
* )
* </code>
*/
abstract public function getRelations(Value $value);
}