forked from ezsystems/ezpublish-kernel
/
Type.php
216 lines (197 loc) · 6.27 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
<?php
/**
* File containing the Url 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\Url;
use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
/**
* The Url field type.
*
* This field type represents a simple string.
*/
class Type extends FieldType
{
/**
* Returns the field type identifier for this field type.
*
* @return string
*/
public function getFieldTypeIdentifier()
{
return 'ezurl';
}
/**
* 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\Url\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\Url\Value
*/
public function getEmptyValue()
{
return new Value();
}
/**
* Inspects given $inputValue and potentially converts it into a dedicated value object.
*
* @param string|\eZ\Publish\Core\FieldType\Url\Value $inputValue
*
* @return \eZ\Publish\Core\FieldType\Url\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\Url\Value $value
*/
protected function checkValueStructure(BaseValue $value)
{
if (null !== $value->link && !is_string($value->link)) {
throw new InvalidArgumentType(
'$value->link',
'string',
$value->link
);
}
if (null !== $value->text && !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\Url\Value $value
*
* @return array
*/
protected function getSortInfo(BaseValue $value)
{
return false;
}
/**
* Converts an $hash to the Value defined by the field type.
*
* @param mixed $hash
*
* @return \eZ\Publish\Core\FieldType\Url\Value $value
*/
public function fromHash($hash)
{
if ($hash === null) {
return $this->getEmptyValue();
}
if (isset($hash['text'])) {
return new Value($hash['link'], $hash['text']);
}
return new Value($hash['link']);
}
/**
* Converts a $Value to a hash.
*
* @param \eZ\Publish\Core\FieldType\Url\Value $value
*
* @return mixed
*/
public function toHash(SPIValue $value)
{
if ($this->isEmptyValue($value)) {
return null;
}
return array('link' => $value->link, 'text' => $value->text);
}
/**
* 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\Core\FieldType\Url\Value $value The value of the field type
*
* @return \eZ\Publish\SPI\Persistence\Content\FieldValue the value processed by the storage engine
*/
public function toPersistenceValue(SPIValue $value)
{
if ($value === null) {
return new FieldValue(
array(
'data' => array(),
'externalData' => null,
'sortKey' => null,
)
);
}
return new FieldValue(
array(
'data' => array(
'urlId' => null,
'text' => $value->text,
),
'externalData' => $value->link,
'sortKey' => $this->getSortInfo($value),
)
);
}
/**
* Converts a persistence $fieldValue 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\Core\FieldType\Url\Value
*/
public function fromPersistenceValue(FieldValue $fieldValue)
{
if ($fieldValue->externalData === null) {
return $this->getEmptyValue();
}
return new Value(
$fieldValue->externalData,
$fieldValue->data['text']
);
}
}