StringMatchFilter::getField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\OpenCubes\Component\Filter\Model;
4
5
use function BenTools\OpenCubes\stringify_uri;
6
7
final class StringMatchFilter extends Filter
8
{
9
    const LIKE = 'LIKE';
10
    const STARTS_WITH = 'STARTS_WITH';
11
    const ENDS_WITH = 'ENDS_WITH';
12
    const REGEXP = 'REGEXP';
13
14
    const OPERATORS = [
15
        self::LIKE,
16
        self::STARTS_WITH,
17
        self::ENDS_WITH,
18
        self::REGEXP,
19
    ];
20
21
    /**
22
     * @var string
23
     */
24
    private $field;
25
26
    /**
27
     * @var FilterValue
28
     */
29
    private $value;
30
31
    /**
32
     * @var string
33
     */
34
    private $operator;
35
36
    /**
37
     * StringMatchFilter constructor.
38
     * @param string           $field
39
     * @param FilterValue|null $value
40
     * @param string           $operator
41
     */
42
    public function __construct(string $field, FilterValue $value = null, string $operator = self::LIKE)
43
    {
44
        $this->field = $field;
45
        $this->value = $value;
46
        $this->operator = $operator;
47
    }
48
49
    /**
50
     * @param string $field
51
     * @param string $value
52
     * @param string $operator
53
     * @return StringMatchFilter
54
     */
55
    public static function createFromValue(string $field, string $value, string $operator = self::LIKE)
56
    {
57
        return new self(
58
            $field,
59
            new FilterValue($value),
60
            $operator
61
        );
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getField(): string
68
    {
69
        return $this->field;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function getValue()
76
    {
77
        return $this->value->getValue();
78
    }
79
80
    /**
81
     * @return FilterValue|null
82
     */
83
    public function getFilterValue()
84
    {
85
        return $this->value;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getOperator(): ?string
92
    {
93
        return $this->operator;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getType(): string
100
    {
101
        return 'string_match';
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function jsonSerialize(): array
108
    {
109
        $output = [
110
            'type'       => $this->getType(),
111
            'field'      => $this->getField(),
112
            'operator'   => $this->getOperator(),
113
            'value'      => $this->getFilterValue(),
114
            'is_applied' => $this->isApplied(),
115
            'is_negated' => $this->isNegated(),
116
        ];
117
118
        if ($this->isApplied()) {
119
            $output['unset_link'] = stringify_uri($this->getToggleUri());
120
        } else {
121
            $output['link'] = stringify_uri($this->getToggleUri());
122
        }
123
124
        return $output;
125
    }
126
}
127