1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the FieldDefinitionCreate 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
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Parser for FieldDefinitionCreate. |
21
|
|
|
*/ |
22
|
|
|
class FieldDefinitionCreate extends BaseParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* ContentType service. |
26
|
|
|
* |
27
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
28
|
|
|
*/ |
29
|
|
|
protected $contentTypeService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* FieldType parser. |
33
|
|
|
* |
34
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\FieldTypeParser |
35
|
|
|
*/ |
36
|
|
|
protected $fieldTypeParser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parser tools. |
40
|
|
|
* |
41
|
|
|
* @var \eZ\Publish\Core\REST\Common\Input\ParserTools |
42
|
|
|
*/ |
43
|
|
|
protected $parserTools; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Construct. |
47
|
|
|
* |
48
|
|
|
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService |
49
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\FieldTypeParser $fieldTypeParser |
50
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParserTools $parserTools |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ContentTypeService $contentTypeService, FieldTypeParser $fieldTypeParser, ParserTools $parserTools) |
53
|
|
|
{ |
54
|
|
|
$this->contentTypeService = $contentTypeService; |
55
|
|
|
$this->fieldTypeParser = $fieldTypeParser; |
56
|
|
|
$this->parserTools = $parserTools; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Parse input structure. |
61
|
|
|
* |
62
|
|
|
* @param array $data |
63
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
64
|
|
|
* |
65
|
|
|
* @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser If an error is found while parsing |
66
|
|
|
* |
67
|
|
|
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct |
68
|
|
|
*/ |
69
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
70
|
|
|
{ |
71
|
|
|
if (!array_key_exists('identifier', $data)) { |
72
|
|
|
throw new Exceptions\Parser("Missing 'identifier' element for FieldDefinitionCreate."); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!array_key_exists('fieldType', $data)) { |
76
|
|
|
throw new Exceptions\Parser("Missing 'fieldType' element for FieldDefinitionCreate."); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$fieldDefinitionCreate = $this->contentTypeService->newFieldDefinitionCreateStruct( |
80
|
|
|
$data['identifier'], |
81
|
|
|
$data['fieldType'] |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
// @todo XSD says that descriptions is mandatory, but content type can be created without it |
85
|
|
|
if (array_key_exists('names', $data)) { |
86
|
|
|
if (!is_array($data['names']) || !array_key_exists('value', $data['names']) || !is_array($data['names']['value'])) { |
87
|
|
|
throw new Exceptions\Parser("Invalid 'names' element for FieldDefinitionCreate."); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$fieldDefinitionCreate->names = $this->parserTools->parseTranslatableList($data['names']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// @todo XSD says that descriptions is mandatory, but content type can be created without it |
94
|
|
|
if (array_key_exists('descriptions', $data)) { |
95
|
|
|
if (!is_array($data['descriptions']) || !array_key_exists('value', $data['descriptions']) || !is_array($data['descriptions']['value'])) { |
96
|
|
|
throw new Exceptions\Parser("Invalid 'descriptions' element for FieldDefinitionCreate."); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$fieldDefinitionCreate->descriptions = $this->parserTools->parseTranslatableList($data['descriptions']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// @todo XSD says that fieldGroup is mandatory, but content type can be created without it |
103
|
|
|
if (array_key_exists('fieldGroup', $data)) { |
104
|
|
|
$fieldDefinitionCreate->fieldGroup = $data['fieldGroup']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// @todo XSD says that position is mandatory, but content type can be created without it |
108
|
|
|
if (array_key_exists('position', $data)) { |
109
|
|
|
$fieldDefinitionCreate->position = (int)$data['position']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// @todo XSD says that isTranslatable is mandatory, but content type can be created without it |
113
|
|
|
if (array_key_exists('isTranslatable', $data)) { |
114
|
|
|
$fieldDefinitionCreate->isTranslatable = $this->parserTools->parseBooleanValue($data['isTranslatable']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// @todo XSD says that isRequired is mandatory, but content type can be created without it |
118
|
|
|
if (array_key_exists('isRequired', $data)) { |
119
|
|
|
$fieldDefinitionCreate->isRequired = $this->parserTools->parseBooleanValue($data['isRequired']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// @todo XSD says that isInfoCollector is mandatory, but content type can be created without it |
123
|
|
|
if (array_key_exists('isInfoCollector', $data)) { |
124
|
|
|
$fieldDefinitionCreate->isInfoCollector = $this->parserTools->parseBooleanValue($data['isInfoCollector']); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// @todo XSD says that isSearchable is mandatory, but content type can be created without it |
128
|
|
|
if (array_key_exists('isSearchable', $data)) { |
129
|
|
|
$fieldDefinitionCreate->isSearchable = $this->parserTools->parseBooleanValue($data['isSearchable']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// @todo XSD says that defaultValue is mandatory, but content type can be created without it |
133
|
|
View Code Duplication |
if (array_key_exists('defaultValue', $data)) { |
|
|
|
|
134
|
|
|
try { |
135
|
|
|
$fieldDefinitionCreate->defaultValue = $this->fieldTypeParser->parseValue( |
136
|
|
|
$data['fieldType'], |
137
|
|
|
$data['defaultValue'] |
138
|
|
|
); |
139
|
|
|
} catch (Exception $e) { |
140
|
|
|
throw new Exceptions\Parser("Invalid 'defaultValue' element for FieldDefinitionCreate.", 0, $e); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
if (array_key_exists('validatorConfiguration', $data)) { |
|
|
|
|
145
|
|
|
$fieldDefinitionCreate->validatorConfiguration = $this->fieldTypeParser->parseValidatorConfiguration( |
146
|
|
|
$data['fieldType'], |
147
|
|
|
$data['validatorConfiguration'] |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
if (array_key_exists('fieldSettings', $data)) { |
|
|
|
|
152
|
|
|
$fieldDefinitionCreate->fieldSettings = $this->fieldTypeParser->parseFieldSettings( |
153
|
|
|
$data['fieldType'], |
154
|
|
|
$data['fieldSettings'] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $fieldDefinitionCreate; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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.