ResultSet::fetchAssoc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file ResultSet.php
33
 *
34
 *  The database Result Set class
35
 *
36
 *  @package    Platine\Database
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database;
48
49
use Closure;
50
use PDO;
51
use PDOStatement;
52
53
/**
54
 * @class ResultSet
55
 * @package Platine\Database
56
 */
57
class ResultSet
58
{
59
    /**
60
     * Class constructor
61
     * @param PDOStatement $statement
62
     */
63
    public function __construct(protected PDOStatement $statement)
64
    {
65
    }
66
67
    /**
68
     * Destructor of the class
69
     *
70
     */
71
    public function __destruct()
72
    {
73
        $this->statement->closeCursor();
74
    }
75
76
    /**
77
     * Count the number of rows affected
78
     * @return int
79
     */
80
    public function count(): int
81
    {
82
        return $this->statement->rowCount();
83
    }
84
85
    /**
86
     * Fetch all record
87
     * @param callable|null $callable
88
     * @param int $fetchStyle the PDO fetch style
89
     * @return array<int, mixed>
90
     */
91
    public function all(?callable $callable = null, int $fetchStyle = 0): array
92
    {
93
        if ($callable === null) {
94
            return $this->statement->fetchAll($fetchStyle);
95
        }
96
        return $this->statement->fetchAll($fetchStyle | PDO::FETCH_FUNC, $callable);
97
    }
98
99
    /**
100
     * Fetch all record per group
101
     * @param bool $uniq
102
     * @param callable|null $callable
103
     * @return array<int, mixed>
104
     */
105
    public function allGroup(bool $uniq = false, ?callable $callable = null): array
106
    {
107
        $fetchStyle = PDO::FETCH_GROUP | ($uniq ? PDO::FETCH_UNIQUE : 0);
108
109
        if ($callable === null) {
110
            return $this->statement->fetchAll($fetchStyle);
111
        }
112
113
        return $this->statement->fetchAll($fetchStyle | PDO::FETCH_FUNC, $callable);
114
    }
115
116
    /**
117
     * Fetch one record
118
     * @param callable|null $callable
119
     * @return mixed
120
     */
121
    public function get(?callable $callable = null): mixed
122
    {
123
        $result = $this->statement->fetch();
124
        $this->statement->closeCursor();
125
        if ($callable !== null) {
126
            $result = $callable($result);
127
        }
128
129
        return $result;
130
    }
131
132
    /**
133
     * Fetch the next record
134
     *
135
     * @return mixed
136
     */
137
    public function next(): mixed
138
    {
139
        return $this->statement->fetch();
140
    }
141
142
    /**
143
     * Close the cursor
144
     * @return mixed
145
     */
146
    public function flush(): mixed
147
    {
148
        return $this->statement->closeCursor();
149
    }
150
151
    /**
152
     * Fetch the column record
153
     * @param int $col 0-indexed number of the column you wish to retrieve
154
     *
155
     * @return mixed
156
     */
157
    public function column(int $col = 0): mixed
158
    {
159
        return $this->statement->fetchColumn($col);
160
    }
161
162
    /**
163
     * Fetch each result as an associative array
164
     * @return $this
165
     */
166
    public function fetchAssoc(): self
167
    {
168
        $this->statement->setFetchMode(PDO::FETCH_ASSOC);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Fetch each result as an stdClass
175
     * @return $this
176
     */
177
    public function fetchObject(): self
178
    {
179
        $this->statement->setFetchMode(PDO::FETCH_OBJ);
180
181
        return $this;
182
    }
183
184
    /**
185
     * Fetch each result as an named
186
     * @return $this
187
     */
188
    public function fetchNamed(): self
189
    {
190
        $this->statement->setFetchMode(PDO::FETCH_NAMED);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Fetch each result as indexed column
197
     * @return $this
198
     */
199
    public function fetchNum(): self
200
    {
201
        $this->statement->setFetchMode(PDO::FETCH_NUM);
202
203
        return $this;
204
    }
205
206
    /**
207
     * Fetch each result as key/pair
208
     * @return $this
209
     */
210
    public function fetchKeyPair(): self
211
    {
212
        $this->statement->setFetchMode(PDO::FETCH_KEY_PAIR);
213
214
        return $this;
215
    }
216
217
    /**
218
     * Fetch each result as an instance of the given class
219
     * @param string $class the name of the class
220
     * @param array<int, mixed> $cargs the constructor arguments
221
     * @return $this
222
     */
223
    public function fetchClass(string $class, array $cargs = []): self
224
    {
225
        $this->statement->setFetchMode(PDO::FETCH_CLASS, $class, $cargs);
226
227
        return $this;
228
    }
229
230
    /**
231
     * Fetch each result and pass to given function
232
     * @param Closure $closure
233
     * @return $this
234
     */
235
    public function fetchCustom(Closure $closure): self
236
    {
237
        $closure($this->statement);
238
239
        return $this;
240
    }
241
}
242