Completed
Push — rest_role_limitation_href ( 55b811 )
by
unknown
28:34
created

BaseLimitationParser::parse()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
rs 8.439
cc 6
eloc 15
nc 5
nop 2
1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Publish\Core\REST\Server\Input\Parser\Limitation;
6
7
use eZ\Publish\Core\REST\Common\Input\BaseParser;
8
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
9
use eZ\Publish\Core\REST\Common\Exceptions;
10
use eZ\Publish\API\Repository\Values;
11
12
abstract class BaseLimitationParser extends BaseParser
13
{
14
    /**
15
     * Parse input structure.
16
     *
17
     * @param array $data
18
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
19
     *
20
     * @return \eZ\Publish\API\Repository\Values\ValueObject
21
     */
22
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
23
    {
24
        if (!array_key_exists('_identifier', $data)) {
25
            throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation.");
26
        }
27
28
        $limitationObject = $this->buildLimitationObject();
29
30
        if (!isset($data['values']['ref']) || !is_array($data['values']['ref'])) {
31
            throw new Exceptions\Parser('Invalid format for data values in Limitation.');
32
        }
33
34
        $limitationValues = [];
35
        foreach ($data['values']['ref'] as $limitationValue) {
36
            if (!array_key_exists('_href', $limitationValue)) {
37
                throw new Exceptions\Parser('Invalid format for data values in Limitation.');
38
            }
39
40
            $limitationValues[] = $this->requestParser->parseHref(
41
                $limitationValue['_href'],
42
                $this->getLimitationValueHrefIdName()
43
            );
44
        }
45
46
        $limitationObject->limitationValues = $limitationValues;
47
48
        return $limitationObject;
49
    }
50
51
    /**
52
     * Returns the name of the id variable in the href of a limitation value.
53
     * Example: the ID of the Section in /content/sections/{contentId} is 'contentId'.
54
     *
55
     * @return string
56
     */
57
    abstract protected function getLimitationValueHrefIdName();
58
59
    /**
60
     * Returns the limitation object the parser handles.
61
     *
62
     * @return \eZ\Publish\API\Repository\Values\User\Limitation A Limitation value object
63
     */
64
    abstract protected function buildLimitationObject();
65
}
66