Completed
Push — rest_role_limitation_href ( 885ab2...f3bd2f )
by
unknown
37:35 queued 08:28
created

LimitationParser::buildLimitation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
/**
13
 * Parses limitation objects from input.
14
 *
15
 * The class expects
16
 */
17
class LimitationParser extends BaseParser
18
{
19
    /**
20
     * Name of the route parameter.
21
     * Example: "sectionId"
22
     * @var string
23
     */
24
    private $limitationRouteParameterName;
25
26
    /**
27
     * Value object class built by the Parser.
28
     * Example: "eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation"
29
     * @var string
30
     */
31
    private $limitationClass;
32
33
    /**
34
     * LimitationParser constructor.
35
     *
36
     * @param $limitationRouteParameterName
37
     * @param $limitationClass
38
     */
39
    public function __construct($limitationRouteParameterName, $limitationClass)
40
    {
41
        $this->limitationRouteParameterName = $limitationRouteParameterName;
42
        $this->limitationClass = $limitationClass;
43
    }
44
45
    /**
46
     * Parse input structure.
47
     *
48
     * @param array $data
49
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
50
     *
51
     * @return \eZ\Publish\API\Repository\Values\ValueObject
52
     */
53 View Code Duplication
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
54
    {
55
        if (!array_key_exists('_identifier', $data)) {
56
            throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation.");
57
        }
58
59
        $limitationObject = $this->buildLimitation();
60
61
        if (!isset($data['values']['ref']) || !is_array($data['values']['ref'])) {
62
            throw new Exceptions\Parser('Invalid format for data values in Limitation.');
63
        }
64
65
        foreach ($data['values']['ref'] as $limitationValue) {
66
            if (!array_key_exists('_href', $limitationValue)) {
67
                throw new Exceptions\Parser('Invalid format for data values in Limitation.');
68
            }
69
70
            $limitationObject->limitationValues[] = $this->parseIdFromHref($limitationValue);
71
        }
72
73
        return $limitationObject;
74
    }
75
76
    /**
77
     * @return \eZ\Publish\API\Repository\Values\User\Limitation
78
     */
79
    protected function buildLimitation()
80
    {
81
        return new $this->limitationClass;
82
    }
83
84
    /**
85
     * @param $limitationValue
86
     *
87
     * @return false|mixed
88
     */
89
    protected function parseIdFromHref($limitationValue)
90
    {
91
        return $this->requestParser->parseHref(
92
            $limitationValue['_href'],
93
            $this->limitationRouteParameterName
94
        );
95
    }
96
}
97