Issues (52)

src/Queries/SearchQuery.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Queries;
5
6
use Level23\Druid\Types\Granularity;
7
use Level23\Druid\Types\SortingOrder;
8
use Level23\Druid\Context\QueryContext;
9
use Level23\Druid\Filters\FilterInterface;
10
use Level23\Druid\Responses\SearchQueryResponse;
11
use Level23\Druid\Collections\IntervalCollection;
12
use Level23\Druid\DataSources\DataSourceInterface;
13
use Level23\Druid\SearchFilters\SearchFilterInterface;
14
15
class SearchQuery implements QueryInterface
16
{
17
    protected DataSourceInterface $dataSource;
18
19
    protected Granularity $granularity;
20
21
    protected IntervalCollection $intervals;
22
23
    protected ?FilterInterface $filter = null;
24
25
    protected ?int $limit = null;
26
27
    /**
28
     * The dimensions to run the search over. Excluding this means the search is run over all dimensions.
29
     *
30
     * @var array|string[]
31
     */
32
    protected array $dimensions = [];
33
34
    protected SortingOrder $sort = SortingOrder::LEXICOGRAPHIC;
35
36
    protected ?QueryContext $context = null;
37
38
    protected SearchFilterInterface $searchFilter;
39
40 3
    public function __construct(
41
        DataSourceInterface $dataSource,
42
        string|Granularity $granularity,
43
        IntervalCollection $intervals,
44
        SearchFilterInterface $searchFilter
45
    ) {
46 3
        $this->dataSource   = $dataSource;
47 3
        $this->granularity  = is_string($granularity) ? Granularity::from(strtolower($granularity)) : $granularity;
48 3
        $this->intervals    = $intervals;
49 3
        $this->searchFilter = $searchFilter;
50
    }
51
52
    /**
53
     * Return the query in array format, so we can fire it to druid.
54
     *
55
     * @return array<string,string|array<mixed>|int>
56
     */
57 1
    public function toArray(): array
58
    {
59 1
        $result = [
60 1
            'queryType'   => 'search',
61 1
            'dataSource'  => $this->dataSource->toArray(),
62 1
            'granularity' => $this->granularity->value,
63 1
            'intervals'   => $this->intervals->toArray(),
64 1
            'sort'        => ['type' => $this->sort->value],
65 1
            'query'       => $this->searchFilter->toArray(),
66 1
        ];
67
68 1
        if ($this->filter) {
69 1
            $result['filter'] = $this->filter->toArray();
70
        }
71
72 1
        if ($this->limit > 0) {
73 1
            $result['limit'] = $this->limit;
74
        }
75
76 1
        if (count($this->dimensions) > 0) {
77 1
            $result['searchDimensions'] = $this->dimensions;
78
        }
79
80 1
        if (isset($this->context)) {
81 1
            $context = $this->context->toArray();
0 ignored issues
show
The method toArray() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            /** @scrutinizer ignore-call */ 
82
            $context = $this->context->toArray();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82 1
            if (sizeof($context) > 0) {
83 1
                $result['context'] = $context;
84
            }
85
        }
86
87 1
        return $result;
88
    }
89
90
    /**
91
     * Parse the response into something we can return to the user.
92
     *
93
     * @param array<string|int,string|int|array<mixed>> $response
94
     *
95
     * @return SearchQueryResponse
96
     */
97 1
    public function parseResponse(array $response): SearchQueryResponse
98
    {
99 1
        return new SearchQueryResponse($response);
100
    }
101
102
    /**
103
     * @param \Level23\Druid\Filters\FilterInterface $filter
104
     */
105 1
    public function setFilter(FilterInterface $filter): void
106
    {
107 1
        $this->filter = $filter;
108
    }
109
110
    /**
111
     * @param int $limit
112
     */
113 1
    public function setLimit(int $limit): void
114
    {
115 1
        $this->limit = $limit;
116
    }
117
118
    /**
119
     * @param array|string[] $dimensions
120
     */
121 1
    public function setDimensions(array $dimensions): void
122
    {
123 1
        $this->dimensions = $dimensions;
124
    }
125
126
    /**
127
     * @param string|SortingOrder $sort
128
     */
129 1
    public function setSort(string|SortingOrder $sort): void
130
    {
131 1
        $this->sort = is_string($sort) ? SortingOrder::from(strtolower($sort)) : $sort;
132
    }
133
134
    /**
135
     * @param \Level23\Druid\Context\QueryContext $context
136
     */
137 1
    public function setContext(QueryContext $context): void
138
    {
139 1
        $this->context = $context;
140
    }
141
}