LikeFilter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Filters;
5
6
/**
7
 * Class LikeFilter
8
 *
9
 * Like filters can be used for basic wildcard searches. They are equivalent to the SQL LIKE operator. Special
10
 * characters supported are "%" (matches any number of characters) and "_" (matches any one character).
11
 *
12
 * @package Level23\Druid\Filters
13
 */
14
class LikeFilter implements FilterInterface
15
{
16
    protected string $dimension;
17
18
    protected string $pattern;
19
20
    protected string $escapeCharacter;
21
22
    /**
23
     * LikeFilter constructor.
24
     *
25
     * @param string                   $dimension               The dimension to filter on
26
     * @param string                   $pattern                 LIKE pattern, such as "foo%" or "___bar".
27
     * @param string                   $escapeCharacter         An escape character that can be used to escape special
28
     *                                                          characters.
29
     */
30 6
    public function __construct(
31
        string $dimension,
32
        string $pattern,
33
        string $escapeCharacter = '\\'
34
    ) {
35 6
        $this->dimension       = $dimension;
36 6
        $this->pattern         = $pattern;
37 6
        $this->escapeCharacter = $escapeCharacter;
38
    }
39
40
    /**
41
     * Return the filter as it can be used in the druid query.
42
     *
43
     * @return array<string,string|array<string,string|int|bool|array<mixed>>>
44
     */
45 5
    public function toArray(): array
46
    {
47 5
        return [
48 5
            'type'      => 'like',
49 5
            'dimension' => $this->dimension,
50 5
            'pattern'   => $this->pattern,
51 5
            'escape'    => $this->escapeCharacter,
52 5
        ];
53
    }
54
}