Completed
Push — ezp24315-sorting_in_rest_views ( 4b8804 )
by
unknown
21:51
created

Criterion::dispatchSortClause()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
c 1
b 1
f 1
nc 1
nop 3
dl 0
loc 6
rs 9.4285
1
<?php
2
3
/**
4
 * File containing the ContentIdCriterion 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Server\Input\Parser;
12
13
use eZ\Publish\Core\REST\Common\Input\BaseParser;
14
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
15
use eZ\Publish\Core\REST\Common\Exceptions;
16
17
/**
18
 * Parser for ViewInput.
19
 */
20
abstract class Criterion extends BaseParser
21
{
22
    /**
23
     * @var array
24
     */
25
    protected static $criterionIdMap = array(
26
        'AND' => 'LogicalAnd',
27
        'OR' => 'LogicalOr',
28
        'NOT' => 'LogicalNot',
29
    );
30
31
    /**
32
     * Dispatches parsing of a criterion name + data to its own parser.
33
     *
34
     * @param string $criterionName
35
     * @param mixed $criterionData
36
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
37
     *
38
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
39
     *
40
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion
41
     */
42
    public function dispatchCriterion($criterionName, $criterionData, ParsingDispatcher $parsingDispatcher)
43
    {
44
        $mediaType = $this->getCriterionMediaType($criterionName);
45
        try {
46
            return $parsingDispatcher->parse(array($criterionName => $criterionData), $mediaType);
47
        } catch (Exceptions\Parser $e) {
48
            throw new Exceptions\Parser("Invalid Criterion id <$criterionName> in <AND>", 0, $e);
49
        }
50
    }
51
52
    /**
53
     * Dispatches parsing of a sort clause name + direction to its own parser.
54
     *
55
     * @param string $sortClauseName
56
     * @param string $direction
57
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
58
     *
59
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
60
     *
61
     * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion
62
     */
63
    public function dispatchSortClause($sortClauseName, $direction, ParsingDispatcher $parsingDispatcher)
64
    {
65
        $mediaType = $this->getSortClauseMediaType($sortClauseName);
66
67
        return $parsingDispatcher->parse(array($sortClauseName => $direction), $mediaType);
68
    }
69
70
    protected function getCriterionMediaType($criterionName)
71
    {
72
        $criterionName = str_replace('Criterion', '', $criterionName);
73
        if (isset(self::$criterionIdMap[$criterionName])) {
74
            $criterionName = self::$criterionIdMap[$criterionName];
75
        }
76
77
        return 'application/vnd.ez.api.internal.criterion.' . $criterionName;
78
    }
79
80
    protected function getSortClauseMediaType($sortClauseName)
81
    {
82
        return 'application/vnd.ez.api.internal.sortclause.' . $sortClauseName;
83
    }
84
}
85