forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Type.php
257 lines (232 loc) · 7.59 KB
/
Type.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
<?php
/**
* File containing the TextLine class.
*
* @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\Core\FieldType\TextLine;
use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\Core\FieldType\ValidationError;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\Core\FieldType\Validator\StringLengthValidator;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
/**
* The TextLine field type.
*
* This field type represents a simple string.
*/
class Type extends FieldType
{
protected $validatorConfigurationSchema = array(
'StringLengthValidator' => array(
'minStringLength' => array(
'type' => 'int',
'default' => 0,
),
'maxStringLength' => array(
'type' => 'int',
'default' => null,
),
),
);
/**
* Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* @param mixed $validatorConfiguration
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validateValidatorConfiguration($validatorConfiguration)
{
$validationErrors = array();
$validator = new StringLengthValidator();
foreach ($validatorConfiguration as $validatorIdentifier => $constraints) {
if ($validatorIdentifier !== 'StringLengthValidator') {
$validationErrors[] = new ValidationError(
"Validator '%validator%' is unknown",
null,
array(
'%validator%' => $validatorIdentifier,
)
);
continue;
}
$validationErrors += $validator->validateConstraints($constraints);
}
return $validationErrors;
}
/**
* Validates a field based on the validators in the field definition.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
* @param \eZ\Publish\Core\FieldType\TextLine\Value $fieldValue The field value for which an action is performed
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
{
$validationErrors = array();
if ($this->isEmptyValue($fieldValue)) {
return $validationErrors;
}
$validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
$constraints = isset($validatorConfiguration['StringLengthValidator'])
? $validatorConfiguration['StringLengthValidator']
: array();
if (isset($constraints['maxStringLength']) &&
$constraints['maxStringLength'] !== false &&
$constraints['maxStringLength'] !== 0 &&
mb_strlen($fieldValue->text) > $constraints['maxStringLength']) {
$validationErrors[] = new ValidationError(
'The string can not exceed %size% character.',
'The string can not exceed %size% characters.',
array(
'%size%' => $constraints['maxStringLength'],
),
'text'
);
}
if (isset($constraints['minStringLength']) &&
$constraints['minStringLength'] !== false &&
$constraints['minStringLength'] !== 0 &&
mb_strlen($fieldValue->text) < $constraints['minStringLength']) {
$validationErrors[] = new ValidationError(
'The string cannot be shorter than %size% character.',
'The string cannot be shorter than %size% characters.',
array(
'%size%' => $constraints['minStringLength'],
),
'text'
);
}
return $validationErrors;
}
/**
* Returns the field type identifier for this field type.
*
* @return string
*/
public function getFieldTypeIdentifier()
{
return 'ezstring';
}
/**
* Returns the name of the given field 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.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*
* @return string
*/
public function getName(SPIValue $value)
{
return (string)$value->text;
}
/**
* Returns the fallback default value of field type when no such default
* value is provided in the field definition in content types.
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value
*/
public function getEmptyValue()
{
return new Value();
}
/**
* Returns if the given $value is considered empty by the field type.
*
* @param mixed $value
*
* @return bool
*/
public function isEmptyValue(SPIValue $value)
{
return $value->text === null || trim($value->text) === '';
}
/**
* Inspects given $inputValue and potentially converts it into a dedicated value object.
*
* @param string|\eZ\Publish\Core\FieldType\TextLine\Value $inputValue
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value The potentially converted and structurally plausible value.
*/
protected function createValueFromInput($inputValue)
{
if (is_string($inputValue)) {
$inputValue = new Value($inputValue);
}
return $inputValue;
}
/**
* Throws an exception if value structure is not of expected format.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*/
protected function checkValueStructure(BaseValue $value)
{
if (!is_string($value->text)) {
throw new InvalidArgumentType(
'$value->text',
'string',
$value->text
);
}
}
/**
* Returns information for FieldValue->$sortKey relevant to the field type.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*
* @return array
*/
protected function getSortInfo(BaseValue $value)
{
return $this->transformationProcessor->transformByGroup((string)$value, 'lowercase');
}
/**
* Converts an $hash to the Value defined by the field type.
*
* @param mixed $hash
*
* @return \eZ\Publish\Core\FieldType\TextLine\Value $value
*/
public function fromHash($hash)
{
if ($hash === null) {
return $this->getEmptyValue();
}
return new Value($hash);
}
/**
* Converts a $Value to a hash.
*
* @param \eZ\Publish\Core\FieldType\TextLine\Value $value
*
* @return mixed
*/
public function toHash(SPIValue $value)
{
if ($this->isEmptyValue($value)) {
return null;
}
return $value->text;
}
/**
* Returns whether the field type is searchable.
*
* @return bool
*/
public function isSearchable()
{
return true;
}
}