/
Type.php
246 lines (226 loc) · 6.13 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
<?php
/**
* File containing the Page class
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\FieldType\Page;
use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\Core\FieldType\Page\PageService;
use eZ\Publish\Core\FieldType\ValidationError;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
class Type extends FieldType
{
/**
* @var array
*/
protected $settingsSchema = array(
'defaultLayout' => array(
'type' => 'string',
'default' => '',
)
);
/**
* @var \eZ\Publish\Core\FieldType\Page\PageService
*/
protected $pageService;
/**
* @param \eZ\Publish\Core\FieldType\Page\PageService $pageService
*/
public function __construct( PageService $pageService )
{
$this->pageService = $pageService;
}
/**
* Returns the field type identifier for this field type
*
* @return string
*/
public function getFieldTypeIdentifier()
{
return "ezpage";
}
/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct
*
* @param mixed $fieldSettings
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validateFieldSettings( $fieldSettings )
{
$validationErrors = array();
foreach ( $fieldSettings as $name => $value )
{
if ( isset( $this->settingsSchema[$name] ) )
{
switch ( $name )
{
case 'defaultLayout':
if ( !in_array( $value, $this->pageService->getAvailableZoneLayouts() ) )
{
$validationErrors[] = new ValidationError(
"Layout '{$value}' for setting '%setting%' is not available",
null,
array(
'setting' => $name
)
);
}
break;
}
}
else
{
$validationErrors[] = new ValidationError(
"Setting '%setting%' is unknown",
null,
array(
'setting' => $name
)
);
}
}
return $validationErrors;
}
/**
* 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.
*
* @return mixed
*/
public function getEmptyValue()
{
return new Value();
}
/**
* Converts an $hash to the Value defined by the field type
*
* @param mixed $hash
*
* @return mixed
*/
public function fromHash( $hash )
{
if ( $hash === null )
{
return null;
}
return new Value( unserialize( $hash ) );
}
/**
* Converts a Value to a hash
*
* @param mixed $value
*
* @return mixed
*/
public function toHash( $value )
{
if ( $this->isEmptyValue( $value ) )
{
return null;
}
return serialize( $value->page );
}
/**
* Converts a persistence $fieldValue to a Value
*
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
*
* @return mixed
*/
public function fromPersistenceValue( FieldValue $fieldValue )
{
if ( $fieldValue->data === null )
{
return null;
}
return new Value( $fieldValue->data );
}
/**
* Converts a $value to a persistence value
*
* @param mixed $value
*
* @return \eZ\Publish\SPI\Persistence\Content\FieldValue
*/
public function toPersistenceValue( $value )
{
if ( $value === null )
{
return new FieldValue(
array(
"data" => null,
"externalData" => null,
"sortKey" => null
)
);
}
return new FieldValue(
array(
"data" => $value->page,
"externalData" => null,
"sortKey" => $this->getSortInfo( $value )
)
);
}
/**
* Returns information for FieldValue->$sortKey relevant to the field type.
*
* Return value is mixed. It should be something which is sensible for
* sorting.
*
* It is up to the persistence implementation to handle those values.
* Common string and integer values are safe.
*
* For the legacy storage it is up to the field converters to set this
* value in either sort_key_string or sort_key_int.
*
* @param mixed $value
*
* @return mixed
*/
protected function getSortInfo( $value )
{
return false;
}
/**
* 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 mixed $value
*
* @return mixed
*/
public function getName( $value )
{
return '';
}
/**
* Implements the core of {@see acceptValue()}.
*
* @param mixed $inputValue
*
* @return \eZ\Publish\Core\FieldType\Page\Value The potentially converted and structurally plausible value.
*/
protected function internalAcceptValue( $inputValue )
{
if ( !$inputValue instanceof Value )
{
throw new InvalidArgumentType(
'$inputValue',
'eZ\\Publish\\Core\\FieldType\\Page\\Value',
$inputValue
);
}
return $inputValue;
}
}