Completed
Push — master ( 65ba07...f0ec2b )
by André
104:16 queued 82:47
created

FieldParser::parse()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 2
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the facet builder Field parser 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\Server\Input\Parser\FacetBuilder;
10
11
use eZ\Publish\Core\REST\Common\Input\BaseParser;
12
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
13
use eZ\Publish\Core\REST\Common\Exceptions;
14
use eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder\FieldFacetBuilder;
15
16
/**
17
 * Parser for Field facet builder.
18
 */
19
class FieldParser extends BaseParser
20
{
21
    /**
22
     * Parses input structure to a FacetBuilder object.
23
     *
24
     * @param array $data
25
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
26
     *
27
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
28
     *
29
     * @return \eZ\Publish\API\Repository\Values\Content\Query\FacetBuilder\FieldFacetBuilder
30
     */
31
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
32
    {
33
        if (!array_key_exists('Field', $data)) {
34
            throw new Exceptions\Parser('Invalid <Field> format');
35
        }
36
37
        $sortType = [
38
            'COUNT_ASC' => FieldFacetBuilder::COUNT_ASC,
39
            'COUNT_DESC' => FieldFacetBuilder::COUNT_DESC,
40
            'TERM_ASC' => FieldFacetBuilder::TERM_ASC,
41
            'TERM_DESC' => FieldFacetBuilder::TERM_DESC,
42
        ];
43
44
        if (isset($data['Field']['sort'])) {
45
            $type = $data['Field']['sort'];
46
47
            if (!in_array($type, $sortType)) {
48
                throw new Exceptions\Parser('<Field> unknown sort type (supported: ' . implode(', ', array_keys($sortType)) . ')');
49
            }
50
51
            $data['Field']['sort'] = $sortType[$type];
52
        } else {
53
            throw new Exceptions\Parser('<Field> sort type missing (supported: ' . implode(', ', array_keys($sortType)) . ')');
54
        }
55
56
        return new FieldFacetBuilder($data['Field']);
57
    }
58
}
59