Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ParserTools.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Input;
8
9 use eZ\Publish\API\Repository\Values;
10 use eZ\Publish\Core\REST\Common\Exceptions;
11 use RuntimeException;
12
16 class ParserTools
17 {
26 public function parseObjectElement(array $objectElement, ParsingDispatcher $parsingDispatcher)
27 {
28 if ($this->isEmbeddedObject($objectElement)) {
29 $parsingDispatcher->parse(
30 $objectElement,
31 $objectElement['_media-type']
32 );
33 }
34
35 return $objectElement['_href'];
36 }
37
46 public function isEmbeddedObject(array $objectElement)
47 {
48 foreach ($objectElement as $childKey => $childValue) {
49 $childKeyIndicator = substr($childKey, 0, 1);
50 if ($childKeyIndicator !== '#' && $childKeyIndicator !== '_') {
51 return true;
52 }
53 }
54
55 return false;
56 }
57
65 public function parseTranslatableList(array $listElement)
66 {
67 $listItems = [];
68 foreach ($listElement['value'] as $valueRow) {
69 $listItems[$valueRow['_languageCode']] = isset($valueRow['#text']) ?
70 $valueRow['#text'] :
71 '';
72 }
73
74 return $listItems;
75 }
76
86 public function parseBooleanValue($value)
87 {
88 if (is_bool($value)) {
89 return $value;
90 }
91 switch (strtolower($value)) {
92 case 'true':
93 return true;
94 case 'false':
95 return false;
96 }
97
98 throw new RuntimeException("Unknown boolean value '{$value}'.");
99 }
100
108 public function parseStatus($contentTypeStatus)
109 {
110 switch (strtoupper($contentTypeStatus)) {
111 case 'DEFINED':
112 return Values\ContentType\ContentType::STATUS_DEFINED;
113 case 'DRAFT':
114 return Values\ContentType\ContentType::STATUS_DRAFT;
115 case 'MODIFIED':
116 return Values\ContentType\ContentType::STATUS_MODIFIED;
117 }
118
119 throw new \RuntimeException("Unknown ContentType status '{$contentTypeStatus}.'");
120 }
121
129 public function parseDefaultSortField($defaultSortFieldString)
130 {
131 switch ($defaultSortFieldString) {
132 case 'PATH':
133 return Values\Content\Location::SORT_FIELD_PATH;
134 case 'PUBLISHED':
135 return Values\Content\Location::SORT_FIELD_PUBLISHED;
136 case 'MODIFIED':
137 return Values\Content\Location::SORT_FIELD_MODIFIED;
138 case 'SECTION':
139 return Values\Content\Location::SORT_FIELD_SECTION;
140 case 'DEPTH':
141 return Values\Content\Location::SORT_FIELD_DEPTH;
142 case 'CLASS_IDENTIFIER':
143 return Values\Content\Location::SORT_FIELD_CLASS_IDENTIFIER;
144 case 'CLASS_NAME':
145 return Values\Content\Location::SORT_FIELD_CLASS_NAME;
146 case 'PRIORITY':
147 return Values\Content\Location::SORT_FIELD_PRIORITY;
148 case 'NAME':
149 return Values\Content\Location::SORT_FIELD_NAME;
150 case 'MODIFIED_SUBNODE':
151 return Values\Content\Location::SORT_FIELD_MODIFIED_SUBNODE;
152 case 'NODE_ID':
153 return Values\Content\Location::SORT_FIELD_NODE_ID;
154 case 'CONTENTOBJECT_ID':
155 return Values\Content\Location::SORT_FIELD_CONTENTOBJECT_ID;
156 }
157
158 throw new \RuntimeException("Unknown default sort field: '{$defaultSortFieldString}'.");
159 }
160
168 public function parseDefaultSortOrder($defaultSortOrderString)
169 {
170 switch (strtoupper($defaultSortOrderString)) {
171 case 'ASC':
172 return Values\Content\Location::SORT_ORDER_ASC;
173 case 'DESC':
174 return Values\Content\Location::SORT_ORDER_DESC;
175 }
176
177 throw new \RuntimeException("Unknown default sort order: '{$defaultSortOrderString}'.");
178 }
179
187 public function parseLimitation(array $limitation)
188 {
189 if (!array_key_exists('_identifier', $limitation)) {
190 throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation.");
191 }
192
193 $limitationObject = $this->getLimitationByIdentifier($limitation['_identifier']);
194
195 if (!isset($limitation['values']['ref']) || !is_array($limitation['values']['ref'])) {
196 throw new Exceptions\Parser('Invalid format for limitation values in Limitation.');
197 }
198
199 $limitationValues = [];
200 foreach ($limitation['values']['ref'] as $limitationValue) {
201 if (!array_key_exists('_href', $limitationValue)) {
202 throw new Exceptions\Parser('Invalid format for limitation values in Limitation.');
203 }
204
205 $limitationValues[] = $limitationValue['_href'];
206 }
207
208 $limitationObject->limitationValues = $limitationValues;
209
210 return $limitationObject;
211 }
212
224 protected function getLimitationByIdentifier($identifier)
225 {
226 switch ($identifier) {
227 case Values\User\Limitation::CONTENTTYPE:
228 return new \eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation();
229
230 case Values\User\Limitation::LANGUAGE:
231 return new \eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation();
232
233 case Values\User\Limitation::LOCATION:
234 return new \eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation();
235
236 case Values\User\Limitation::OWNER:
237 return new \eZ\Publish\API\Repository\Values\User\Limitation\OwnerLimitation();
238
239 case Values\User\Limitation::PARENTOWNER:
240 return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentOwnerLimitation();
241
242 case Values\User\Limitation::PARENTCONTENTTYPE:
243 return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentContentTypeLimitation();
244
245 case Values\User\Limitation::PARENTDEPTH:
246 return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentDepthLimitation();
247
248 case Values\User\Limitation::SECTION:
249 return new \eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation();
250
251 case Values\User\Limitation::SITEACCESS:
252 return new \eZ\Publish\API\Repository\Values\User\Limitation\SiteaccessLimitation();
253
254 case Values\User\Limitation::STATE:
255 return new \eZ\Publish\API\Repository\Values\User\Limitation\ObjectStateLimitation();
256
257 case Values\User\Limitation::SUBTREE:
258 return new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation();
259
260 case Values\User\Limitation::USERGROUP:
261 return new \eZ\Publish\API\Repository\Values\User\Limitation\UserGroupLimitation();
262
263 case Values\User\Limitation::PARENTUSERGROUP:
264 return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation();
265
266 default:
267 throw new \eZ\Publish\Core\Base\Exceptions\NotFoundException('Limitation', $identifier);
268 }
269 }
270 }
eZ\Publish\Core\REST\Common\Exceptions\Parser
Exception thrown if a parser discovers an error.
Definition: REST/Common/Exceptions/Parser.php:15
eZ\Publish\Core\REST\Common\Input\ParserTools
Tools object to be used in Input Parsers.
Definition: ParserTools.php:17
eZ\Publish\Core\REST\Common\Input\ParserTools\parseDefaultSortOrder
parseDefaultSortOrder($defaultSortOrderString)
Parses the default sort order from the given $defaultSortOrderString.
Definition: ParserTools.php:168
eZ\Publish\Core\REST\Common\Input\ParserTools\parseBooleanValue
parseBooleanValue($value)
Parses a boolean from $value.
Definition: ParserTools.php:86
eZ\Publish\Core\REST\Common\Input\ParserTools\parseLimitation
parseLimitation(array $limitation)
Parses the input structure to Limitation object.
Definition: ParserTools.php:187
eZ\Publish\Core\REST\Common\Input\ParserTools\parseTranslatableList
parseTranslatableList(array $listElement)
Parses a translatable list, like names or descriptions.
Definition: ParserTools.php:65
eZ\Publish\Core\REST\Common\Input\ParserTools\parseObjectElement
parseObjectElement(array $objectElement, ParsingDispatcher $parsingDispatcher)
Parses the given $objectElement, if it contains embedded data.
Definition: ParserTools.php:26
eZ\Publish\Core\REST\Common\Input\ParserTools\isEmbeddedObject
isEmbeddedObject(array $objectElement)
Returns if the given $objectElement has embedded object data or is only a reference.
Definition: ParserTools.php:46
eZ\Publish\Core\REST\Common\Input\ParserTools\parseDefaultSortField
parseDefaultSortField($defaultSortFieldString)
Parses the default sort field from the given $defaultSortFieldString.
Definition: ParserTools.php:129
eZ\Publish\Core\REST\Common\Input\ParserTools\getLimitationByIdentifier
getLimitationByIdentifier($identifier)
Instantiates Limitation object based on identifier.
Definition: ParserTools.php:224
eZ\Publish\Core\REST\Common\Input\ParserTools\parseStatus
parseStatus($contentTypeStatus)
Parses the content types status from $contentTypeStatus.
Definition: ParserTools.php:108
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher
Parsing dispatcher.
Definition: ParsingDispatcher.php:15
eZ\Publish\Core\REST\Common\Input\ParsingDispatcher\parse
parse(array $data, $mediaType)
Parses the given $data according to $mediaType.
Definition: ParsingDispatcher.php:66
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Common\Input
Definition: BaseParser.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.