Passed
Push — develop ( a3aa5d...84fa8b )
by nguereza
06:38
created

ResultSet::fetchNamed()   A

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   http://www.iacademy.cf
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
    /**
61
     * The PDOStatement instance
62
     * @var PDOStatement
63
     */
64
    protected PDOStatement $statement;
65
66
    /**
67
     * Class constructor
68
     * @param PDOStatement $statement
69
     */
70
    public function __construct(PDOStatement $statement)
71
    {
72
        $this->statement = $statement;
73
    }
74
75
    /**
76
     * Destructor of the class
77
     *
78
     */
79
    public function __destruct()
80
    {
81
        $this->statement->closeCursor();
82
    }
83
84
    /**
85
     * Count the number of rows affected
86
     * @return int
87
     */
88
    public function count(): int
89
    {
90
        return $this->statement->rowCount();
91
    }
92
93
    /**
94
     * Fetch all record
95
     * @param callable $callable
96
     * @param int $fetchStyle the PDO fetch style
97
     * @return array<int, mixed>|false
98
     */
99
    public function all(callable $callable = null, int $fetchStyle = 0)
100
    {
101
        if ($callable === null) {
102
            return $this->statement->fetchAll($fetchStyle);
103
        }
104
        return $this->statement->fetchAll($fetchStyle | PDO::FETCH_FUNC, $callable);
105
    }
106
107
    /**
108
     * Fetch all record per group
109
     * @param bool $uniq
110
     * @param callable $callable
111
     * @return array<int, mixed>|false
112
     */
113
    public function allGroup(bool $uniq = false, callable $callable = null)
114
    {
115
        $fetchStyle = PDO::FETCH_GROUP | ($uniq ? PDO::FETCH_UNIQUE : 0);
116
117
        if ($callable === null) {
118
            return $this->statement->fetchAll($fetchStyle);
119
        }
120
        return $this->statement->fetchAll($fetchStyle | PDO::FETCH_FUNC, $callable);
121
    }
122
123
    /**
124
     * Fetch one record
125
     * @param callable $callable
126
     * @return mixed
127
     */
128
    public function get(callable $callable = null)
129
    {
130
        $result = $this->statement->fetch();
131
        $this->statement->closeCursor();
132
        if ($callable !== null) {
133
            $result = $callable($result);
134
        }
135
136
        return $result;
137
    }
138
139
    /**
140
     * Fetch the next record
141
     *
142
     * @return mixed
143
     */
144
    public function next()
145
    {
146
        return $this->statement->fetch();
147
    }
148
149
    /**
150
     * Close the cursor
151
     * @return mixed
152
     */
153
    public function flush()
154
    {
155
        return $this->statement->closeCursor();
156
    }
157
158
    /**
159
     * Fetch the column record
160
     * @param int $col 0-indexed number of the column you wish to retrieve
161
     *
162
     * @return mixed
163
     */
164
    public function column(int $col = 0)
165
    {
166
        return $this->statement->fetchColumn($col);
167
    }
168
169
    /**
170
     * Fetch each result as an associative array
171
     * @return self
172
     */
173
    public function fetchAssoc(): self
174
    {
175
        $this->statement->setFetchMode(PDO::FETCH_ASSOC);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Fetch each result as an stdClass
182
     * @return self
183
     */
184
    public function fetchObject(): self
185
    {
186
        $this->statement->setFetchMode(PDO::FETCH_OBJ);
187
188
        return $this;
189
    }
190
191
    /**
192
     * Fetch each result as an named
193
     * @return self
194
     */
195
    public function fetchNamed(): self
196
    {
197
        $this->statement->setFetchMode(PDO::FETCH_NAMED);
198
199
        return $this;
200
    }
201
202
    /**
203
     * Fetch each result as indexed column
204
     * @return self
205
     */
206
    public function fetchNum(): self
207
    {
208
        $this->statement->setFetchMode(PDO::FETCH_NUM);
209
210
        return $this;
211
    }
212
213
    /**
214
     * Fetch each result as key/pair
215
     * @return self
216
     */
217
    public function fetchKeyPair(): self
218
    {
219
        $this->statement->setFetchMode(PDO::FETCH_KEY_PAIR);
220
221
        return $this;
222
    }
223
224
    /**
225
     * Fetch each result as an instance of the given class
226
     * @param string $class the name of the class
227
     * @param array<int, mixed> $cargs the constructor arguments
228
     * @return self
229
     */
230
    public function fetchClass(string $class, array $cargs = []): self
231
    {
232
        $this->statement->setFetchMode(PDO::FETCH_CLASS, $class, $cargs);
233
234
        return $this;
235
    }
236
237
    /**
238
     * Fetch each result and pass to given function
239
     * @param Closure $closure
240
     * @return self
241
     */
242
    public function fetchCustom(Closure $closure): self
243
    {
244
        $closure($this->statement);
245
246
        return $this;
247
    }
248
}
249