iDokladFilter::getHttpQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Fousky\Component\iDoklad\UrlExtension;
4
5
/**
6
 * @author Lukáš Brzák <[email protected]>
7
 */
8
class iDokladFilter implements UrlExtensionInterface
9
{
10
    const LESS_THEN = 'lt';
11
12
    const LESS_THEN_OR_EQUAL = 'lte';
13
14
    const GREATER_THEN = 'gt';
15
16
    const GREATER_THEN_OR_EQUAL = 'gte';
17
18
    const EQUAL = 'eq';
19
20
    const NOT_EQUAL = '!eq';
21
22
    const CONTAINS = 'ct';
23
24
    const NOT_CONTAINS = '!ct';
25
26
    const BETWEEN = 'between';
27
28
    const FILTER_TYPE_AND = 'and';
29
30
    const FILTER_TYPE_OR = 'or';
31
32
    public static $operators = [
33
        self::LESS_THEN,
34
        self::LESS_THEN_OR_EQUAL,
35
        self::GREATER_THEN,
36
        self::GREATER_THEN_OR_EQUAL,
37
        self::EQUAL,
38
        self::NOT_EQUAL,
39
        self::CONTAINS,
40
        self::NOT_CONTAINS,
41
        self::BETWEEN,
42
    ];
43
44
    protected $parts = [];
45
46
    protected $type = self::FILTER_TYPE_AND;
47
48
    /**
49
     * @param string $property
50
     * @param string $operator
51
     * @param string $value
52
     *
53
     * @throws \InvalidArgumentException
54
     *
55
     * @return $this
56
     */
57
    public function filter(string $property, string $operator, string $value): self
58
    {
59
        if (!$this->isValidOperator($operator)) {
60
            throw new \InvalidArgumentException(sprintf(
61
                'Operator %s is not valid. Available operators: %s',
62
                $operator,
63
                implode(', ', static::$operators)
64
            ));
65
        }
66
67
        $this->parts[] = sprintf(
68
            '%s~%s~%s',
69
            $property,
70
            $operator,
71
            $value
72
        );
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $type
79
     *
80
     * @throws \InvalidArgumentException
81
     *
82
     * @return iDokladFilter
83
     */
84
    public function filterType(string $type): self
85
    {
86
        if (self::FILTER_TYPE_AND !== $type && self::FILTER_TYPE_OR !== $type) {
87
            throw new \InvalidArgumentException(sprintf(
88
                'Type %s is not valid. Available types: %s',
89
                $type,
90
                implode(', ', [self::FILTER_TYPE_AND, self::FILTER_TYPE_OR])
91
            ));
92
        }
93
94
        $this->type = $type;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Return key => value associative array of HTTP GET parameters.
101
     *
102
     * @return array
103
     *
104
     * @internal
105
     */
106
    public function getHttpQuery(): array
107
    {
108
        if (0 === count($this->parts)) {
109
            return [];
110
        }
111
112
        return [
113
            'filter' => implode('|', $this->parts),
114
            'filtertype' => $this->type,
115
        ];
116
    }
117
118
    /**
119
     * @param string $operator
120
     *
121
     * @return bool
122
     */
123
    protected function isValidOperator(string $operator): bool
124
    {
125
        return in_array($operator, self::$operators, true);
126
    }
127
}
128