1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the FieldDefinitionUpdate parser class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Input\Parser; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
12
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
13
|
|
|
use eZ\Publish\Core\REST\Common\Input\FieldTypeParser; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParserTools; |
15
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
16
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Parser for FieldDefinitionUpdate. |
20
|
|
|
*/ |
21
|
|
|
class FieldDefinitionUpdate extends BaseParser |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* ContentType service. |
25
|
|
|
* |
26
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
27
|
|
|
*/ |
28
|
|
|
protected $contentTypeService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* FieldType parser. |
32
|
|
|
* |
33
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser |
34
|
|
|
*/ |
35
|
|
|
protected $fieldTypeParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Parser tools. |
39
|
|
|
* |
40
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\ParserTools |
41
|
|
|
*/ |
42
|
|
|
protected $parserTools; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Construct. |
46
|
|
|
* |
47
|
|
|
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService |
48
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools |
49
|
|
|
*/ |
50
|
|
|
public function __construct(ContentTypeService $contentTypeService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools) |
51
|
|
|
{ |
52
|
|
|
$this->contentTypeService = $contentTypeService; |
53
|
|
|
$this->fieldTypeParser = $fieldTypeParser; |
54
|
|
|
$this->parserTools = $parserTools; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Parse input structure. |
59
|
|
|
* |
60
|
|
|
* @param array $data |
61
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
62
|
|
|
* |
63
|
|
|
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct |
64
|
|
|
*/ |
65
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
66
|
|
|
{ |
67
|
|
|
$fieldDefinitionUpdate = $this->contentTypeService->newFieldDefinitionUpdateStruct(); |
68
|
|
|
|
69
|
|
|
if (array_key_exists('identifier', $data)) { |
70
|
|
|
$fieldDefinitionUpdate->identifier = $data['identifier']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// @todo XSD says that descriptions is mandatory, but field definition can be updated without it |
74
|
|
|
if (array_key_exists('names', $data)) { |
75
|
|
|
if (!is_array($data['names']) || !array_key_exists('value', $data['names']) || !is_array($data['names']['value'])) { |
76
|
|
|
throw new Exceptions\Parser("Invalid 'names' element for FieldDefinitionUpdate."); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$fieldDefinitionUpdate->names = $this->parserTools->parseTranslatableList($data['names']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// @todo XSD says that descriptions is mandatory, but field definition can be updated without it |
83
|
|
|
if (array_key_exists('descriptions', $data)) { |
84
|
|
|
if (!is_array($data['descriptions']) || !array_key_exists('value', $data['descriptions']) || !is_array($data['descriptions']['value'])) { |
85
|
|
|
throw new Exceptions\Parser("Invalid 'descriptions' element for FieldDefinitionUpdate."); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$fieldDefinitionUpdate->descriptions = $this->parserTools->parseTranslatableList($data['descriptions']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// @todo XSD says that fieldGroup is mandatory, but field definition can be updated without it |
92
|
|
|
if (array_key_exists('fieldGroup', $data)) { |
93
|
|
|
$fieldDefinitionUpdate->fieldGroup = $data['fieldGroup']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// @todo XSD says that position is mandatory, but field definition can be updated without it |
97
|
|
|
if (array_key_exists('position', $data)) { |
98
|
|
|
$fieldDefinitionUpdate->position = (int)$data['position']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// @todo XSD says that isTranslatable is mandatory, but field definition can be updated without it |
102
|
|
|
if (array_key_exists('isTranslatable', $data)) { |
103
|
|
|
$fieldDefinitionUpdate->isTranslatable = $this->parserTools->parseBooleanValue($data['isTranslatable']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// @todo XSD says that isRequired is mandatory, but field definition can be updated without it |
107
|
|
|
if (array_key_exists('isRequired', $data)) { |
108
|
|
|
$fieldDefinitionUpdate->isRequired = $this->parserTools->parseBooleanValue($data['isRequired']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// @todo XSD says that isInfoCollector is mandatory, but field definition can be updated without it |
112
|
|
|
if (array_key_exists('isInfoCollector', $data)) { |
113
|
|
|
$fieldDefinitionUpdate->isInfoCollector = $this->parserTools->parseBooleanValue($data['isInfoCollector']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// @todo XSD says that isSearchable is mandatory, but field definition can be updated without it |
117
|
|
|
if (array_key_exists('isSearchable', $data)) { |
118
|
|
|
$fieldDefinitionUpdate->isSearchable = $this->parserTools->parseBooleanValue($data['isSearchable']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$fieldDefinition = $this->getFieldDefinition($data); |
122
|
|
|
|
123
|
|
|
// @todo XSD says that defaultValue is mandatory, but content type can be created without it |
124
|
|
|
if (array_key_exists('defaultValue', $data)) { |
125
|
|
|
$fieldDefinitionUpdate->defaultValue = $this->fieldTypeParser->parseValue( |
126
|
|
|
$fieldDefinition->fieldTypeIdentifier, |
127
|
|
|
$data['defaultValue'] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
if (array_key_exists('validatorConfiguration', $data)) { |
|
|
|
|
132
|
|
|
$fieldDefinitionUpdate->validatorConfiguration = $this->fieldTypeParser->parseValidatorConfiguration( |
133
|
|
|
$fieldDefinition->fieldTypeIdentifier, |
134
|
|
|
$data['validatorConfiguration'] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
if (array_key_exists('fieldSettings', $data)) { |
|
|
|
|
139
|
|
|
$fieldDefinitionUpdate->fieldSettings = $this->fieldTypeParser->parseFieldSettings( |
140
|
|
|
$fieldDefinition->fieldTypeIdentifier, |
141
|
|
|
$data['fieldSettings'] |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $fieldDefinitionUpdate; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns field definition by 'typeFieldDefinitionDraft' pattern URL. |
150
|
|
|
* |
151
|
|
|
* Assumes given $data array has '__url' element set. |
152
|
|
|
* |
153
|
|
|
* @todo depends on temporary solution to give parser access to the URL |
154
|
|
|
* |
155
|
|
|
* @see \eZ\Publish\Core\REST\Server\Controller\ContentType::updateFieldDefinition |
156
|
|
|
* |
157
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
158
|
|
|
* |
159
|
|
|
* @param array $data |
160
|
|
|
* |
161
|
|
|
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition |
162
|
|
|
*/ |
163
|
|
|
protected function getFieldDefinition(array $data) |
164
|
|
|
{ |
165
|
|
|
$contentTypeId = $this->requestParser->parseHref($data['__url'], 'contentTypeId'); |
166
|
|
|
$fieldDefinitionId = $this->requestParser->parseHref($data['__url'], 'fieldDefinitionId'); |
167
|
|
|
|
168
|
|
|
$contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId); |
169
|
|
|
foreach ($contentTypeDraft->getFieldDefinitions() as $fieldDefinition) { |
170
|
|
|
if ($fieldDefinition->id == $fieldDefinitionId) { |
171
|
|
|
return $fieldDefinition; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
throw new Exceptions\NotFoundException("Field definition not found: '{$data['__url']}'."); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.