Passed
Push — master ( ee6b03...f5a05a )
by Aleksandar
02:24
created

QueryResultBuilder::useReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL;
19
20
use ArekX\PQL\Contracts\ResultBuilder;
21
use ArekX\PQL\Contracts\ResultReader;
22
23
/**
24
 * Result builder for query.
25
 */
26
class QueryResultBuilder implements ResultBuilder
27
{
28
    /**
29
     * Result reader which is used to read all results from.
30
     *
31
     * @var ResultReader
32
     */
33
    protected ResultReader $reader;
34
35
    /**
36
     * List of pipeline methods to apply when calling result()
37
     *
38
     * @see QueryResultBuilder::result()
39
     * @var array
40
     */
41
    protected array $pipelines = [];
42
43
    /**
44
     * Create new instance query result builder.
45
     * @param ResultReader|null $reader Reader to be used
46
     * @return static
47
     */
48 21
    public static function create(ResultReader $reader = null): static
49
    {
50 21
        return new static($reader);
51
    }
52
53
    /**
54
     * Create new instance query result builder.
55
     * @param ResultReader|null $reader Reader to be used
56
     */
57 22
    public function __construct(ResultReader $reader = null)
58
    {
59 22
        if ($reader) {
60 13
            $this->useReader($reader);
61
        }
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 22
    public function useReader(ResultReader $reader): static
68
    {
69 22
        $this->reader = $reader;
70 22
        return $this;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function first(): mixed
77
    {
78 1
        $result = $this->reader->getNextRow();
79 1
        $this->reader->finalize();
80 1
        return $result;
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86 8
    public function scalar($index = 0): mixed
87
    {
88 8
        $result = $this->reader->getNextColumn($index);
89 8
        $this->reader->finalize();
90 8
        return $result;
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 1
    public function column($index = 0): array
97
    {
98 1
        $result = $this->reader->getColumnRows($index);
99 1
        $this->reader->finalize();
100 1
        return $result;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106 1
    public function exists(): bool
107
    {
108 1
        $result = $this->reader->getNextColumn();
109 1
        $this->reader->finalize();
110 1
        return $result !== null;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116 1
    public function clearPipeline(): static
117
    {
118 1
        $this->pipelines = [];
119 1
        return $this;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125 1
    public function pipeIndexBy(string $column): static
126
    {
127 1
        return $this->pipeReduce(function ($result, $row) use ($column) {
128 1
            $result[$row[$column]] = $row;
129 1
            return $result;
130 1
        });
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136 5
    public function pipeReduce(callable $reducer, $initialValue = null): static
137
    {
138 5
        $this->pipelines[] = fn($results) => array_reduce($results, $reducer, $initialValue);
139 5
        return $this;
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145 1
    public function pipeSort(callable $sorter): static
146
    {
147 1
        $this->pipelines[] = function ($results) use ($sorter) {
148 1
            usort($results, $sorter);
149 1
            return $results;
150 1
        };
151
152 1
        return $this;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158 1
    public function pipeMap(callable $mapper): static
159
    {
160 1
        $this->pipelines[] = fn($results) => array_map($mapper, $results);
161 1
        return $this;
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167 1
    public function pipeFilter(callable $filter): static
168
    {
169 1
        $this->pipelines[] = fn($results) => array_values(array_filter($results, $filter));
170 1
        return $this;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176 10
    public function result(): mixed
177
    {
178 10
        $result = $this->all();
179
180 10
        foreach ($this->pipelines as $pipelineMethod) {
181 7
            $result = $pipelineMethod($result, $this);
182
        }
183
184 9
        return $result;
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190 11
    public function all(): array
191
    {
192 11
        $result = $this->reader->getAllRows();
193 11
        $this->reader->finalize();
194 11
        return $result;
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200 2
    public function pipeListBy(string $keyColumn, string $valueColumn): static
201
    {
202 2
        return $this->pipeReduce(function ($previous, $row) use ($keyColumn, $valueColumn) {
203
204 2
            if (!array_key_exists($keyColumn, $row) || !array_key_exists($valueColumn, $row)) {
205 1
                throw new \InvalidArgumentException('$keyColumn and $valueColumn must exist in the result.');
206
            }
207
208 1
            $previous[$row[$keyColumn]] = $row[$valueColumn];
209
210 1
            return $previous;
211 2
        }, []);
212
    }
213
}
214