Completed
Push — spi_getname_change ( 9b3757...5d4921 )
by André
23:58
created

RouteBasedLimitationParser::parseIdFromHref()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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