Completed
Push — ezp-30696 ( 9bb3ad...3bd812 )
by
unknown
49:02 queued 18:35
created

ParserTools::parseLimitation()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 25
loc 25
rs 8.8977
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ParserTools 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\Common\Input;
10
11
use eZ\Publish\API\Repository\Values;
12
use eZ\Publish\Core\REST\Common\Exceptions;
13
use RuntimeException;
14
15
/**
16
 * Tools object to be used in Input Parsers.
17
 */
18
class ParserTools
19
{
20
    /**
21
     * Parses the given $objectElement, if it contains embedded data.
22
     *
23
     * @param array $objectElement
24
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
25
     *
26
     * @return mixed
27
     */
28
    public function parseObjectElement(array $objectElement, ParsingDispatcher $parsingDispatcher)
29
    {
30
        if ($this->isEmbeddedObject($objectElement)) {
31
            $parsingDispatcher->parse(
32
                $objectElement,
33
                $objectElement['_media-type']
34
            );
35
        }
36
37
        return $objectElement['_href'];
38
    }
39
40
    /**
41
     * Returns if the given $objectElement has embedded object data or is only
42
     * a reference.
43
     *
44
     * @param array $objectElement
45
     *
46
     * @return bool
47
     */
48
    public function isEmbeddedObject(array $objectElement)
49
    {
50
        foreach ($objectElement as $childKey => $childValue) {
51
            $childKeyIndicator = substr($childKey, 0, 1);
52
            if ($childKeyIndicator !== '#' && $childKeyIndicator !== '_') {
53
                return true;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * Parses a translatable list, like names or descriptions.
62
     *
63
     * @param array $listElement
64
     *
65
     * @return array
66
     */
67
    public function parseTranslatableList(array $listElement)
68
    {
69
        $listItems = [];
70
        foreach ($listElement['value'] as $valueRow) {
71
            $listItems[$valueRow['_languageCode']] = isset($valueRow['#text']) ?
72
                $valueRow['#text'] :
73
                '';
74
        }
75
76
        return $listItems;
77
    }
78
79
    /**
80
     * Parses a boolean from $value.
81
     *
82
     * @param string|bool $value
83
     *
84
     * @return bool
85
     * @throws \RuntimeException if the value can not be transformed to a boolean
86
     */
87
    public function parseBooleanValue($value)
88
    {
89
        if (is_bool($value)) {
90
            return $value;
91
        }
92
        switch (strtolower($value)) {
93
            case 'true':
94
                return true;
95
            case 'false':
96
                return false;
97
        }
98
99
        throw new RuntimeException("Unknown boolean value '{$value}'.");
100
    }
101
102
    /**
103
     * Parses the content types status from $contentTypeStatus.
104
     *
105
     * @param string $contentTypeStatus
106
     *
107
     * @return int
108
     */
109
    public function parseStatus($contentTypeStatus)
110
    {
111
        switch (strtoupper($contentTypeStatus)) {
112
            case 'DEFINED':
113
                return Values\ContentType\ContentType::STATUS_DEFINED;
114
            case 'DRAFT':
115
                return Values\ContentType\ContentType::STATUS_DRAFT;
116
            case 'MODIFIED':
117
                return Values\ContentType\ContentType::STATUS_MODIFIED;
118
        }
119
120
        throw new \RuntimeException("Unknown ContentType status '{$contentTypeStatus}.'");
121
    }
122
123
    /**
124
     * Parses the default sort field from the given $defaultSortFieldString.
125
     *
126
     * @param string $defaultSortFieldString
127
     *
128
     * @return int
129
     */
130 View Code Duplication
    public function parseDefaultSortField($defaultSortFieldString)
131
    {
132
        switch ($defaultSortFieldString) {
133
            case 'PATH':
134
                return Values\Content\Location::SORT_FIELD_PATH;
135
            case 'PUBLISHED':
136
                return Values\Content\Location::SORT_FIELD_PUBLISHED;
137
            case 'MODIFIED':
138
                return Values\Content\Location::SORT_FIELD_MODIFIED;
139
            case 'SECTION':
140
                return Values\Content\Location::SORT_FIELD_SECTION;
141
            case 'DEPTH':
142
                return Values\Content\Location::SORT_FIELD_DEPTH;
143
            case 'CLASS_IDENTIFIER':
144
                return Values\Content\Location::SORT_FIELD_CLASS_IDENTIFIER;
145
            case 'CLASS_NAME':
146
                return Values\Content\Location::SORT_FIELD_CLASS_NAME;
147
            case 'PRIORITY':
148
                return Values\Content\Location::SORT_FIELD_PRIORITY;
149
            case 'NAME':
150
                return Values\Content\Location::SORT_FIELD_NAME;
151
            case 'MODIFIED_SUBNODE':
152
                return Values\Content\Location::SORT_FIELD_MODIFIED_SUBNODE;
0 ignored issues
show
Deprecated Code introduced by
The constant eZ\Publish\API\Repositor..._FIELD_MODIFIED_SUBNODE has been deprecated.

This class constant has been deprecated.

Loading history...
153
            case 'NODE_ID':
154
                return Values\Content\Location::SORT_FIELD_NODE_ID;
155
            case 'CONTENTOBJECT_ID':
156
                return Values\Content\Location::SORT_FIELD_CONTENTOBJECT_ID;
157
        }
158
159
        throw new \RuntimeException("Unknown default sort field: '{$defaultSortFieldString}'.");
160
    }
161
162
    /**
163
     * Parses the default sort order from the given $defaultSortOrderString.
164
     *
165
     * @param string $defaultSortOrderString
166
     *
167
     * @return int
168
     */
169
    public function parseDefaultSortOrder($defaultSortOrderString)
170
    {
171
        switch (strtoupper($defaultSortOrderString)) {
172
            case 'ASC':
173
                return Values\Content\Location::SORT_ORDER_ASC;
174
            case 'DESC':
175
                return Values\Content\Location::SORT_ORDER_DESC;
176
        }
177
178
        throw new \RuntimeException("Unknown default sort order: '{$defaultSortOrderString}'.");
179
    }
180
181
    /**
182
     * Parses the input structure to Limitation object.
183
     *
184
     * @param array $limitation
185
     *
186
     * @return \eZ\Publish\API\Repository\Values\User\Limitation
187
     */
188 View Code Duplication
    public function parseLimitation(array $limitation)
189
    {
190
        if (!array_key_exists('_identifier', $limitation)) {
191
            throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation.");
192
        }
193
194
        $limitationObject = $this->getLimitationByIdentifier($limitation['_identifier']);
195
196
        if (!isset($limitation['values']['ref']) || !is_array($limitation['values']['ref'])) {
197
            throw new Exceptions\Parser('Invalid format for limitation values in Limitation.');
198
        }
199
200
        $limitationValues = [];
201
        foreach ($limitation['values']['ref'] as $limitationValue) {
202
            if (!array_key_exists('_href', $limitationValue)) {
203
                throw new Exceptions\Parser('Invalid format for limitation values in Limitation.');
204
            }
205
206
            $limitationValues[] = $limitationValue['_href'];
207
        }
208
209
        $limitationObject->limitationValues = $limitationValues;
210
211
        return $limitationObject;
212
    }
213
214
    /**
215
     * Instantiates Limitation object based on identifier.
216
     *
217
     * @param string $identifier
218
     *
219
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
220
     *
221
     * @return \eZ\Publish\API\Repository\Values\User\Limitation
222
     *
223
     * @todo Use dependency injection system
224
     */
225 View Code Duplication
    protected function getLimitationByIdentifier($identifier)
226
    {
227
        switch ($identifier) {
228
            case Values\User\Limitation::CONTENTTYPE:
229
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation();
230
231
            case Values\User\Limitation::LANGUAGE:
232
                return new \eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation();
233
234
            case Values\User\Limitation::LOCATION:
235
                return new \eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation();
236
237
            case Values\User\Limitation::OWNER:
238
                return new \eZ\Publish\API\Repository\Values\User\Limitation\OwnerLimitation();
239
240
            case Values\User\Limitation::PARENTOWNER:
241
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentOwnerLimitation();
242
243
            case Values\User\Limitation::PARENTCONTENTTYPE:
244
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentContentTypeLimitation();
245
246
            case Values\User\Limitation::PARENTDEPTH:
247
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentDepthLimitation();
248
249
            case Values\User\Limitation::SECTION:
250
                return new \eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation();
251
252
            case Values\User\Limitation::SITEACCESS:
253
                return new \eZ\Publish\API\Repository\Values\User\Limitation\SiteaccessLimitation();
254
255
            case Values\User\Limitation::STATE:
256
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ObjectStateLimitation();
257
258
            case Values\User\Limitation::SUBTREE:
259
                return new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation();
260
261
            case Values\User\Limitation::USERGROUP:
262
                return new \eZ\Publish\API\Repository\Values\User\Limitation\UserGroupLimitation();
263
264
            case Values\User\Limitation::PARENTUSERGROUP:
265
                return new \eZ\Publish\API\Repository\Values\User\Limitation\ParentUserGroupLimitation();
266
267
            default:
268
                throw new \eZ\Publish\Core\Base\Exceptions\NotFoundException('Limitation', $identifier);
269
        }
270
    }
271
}
272