Completed
Pull Request — master (#1)
by BENOIT
07:29
created

Result   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 162
c 0
b 0
f 0
wmc 28
lcom 1
cbo 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLastInsertId() 0 8 2
A count() 0 4 2
A asArray() 0 10 2
A asRow() 0 10 3
A asList() 0 16 3
A asValue() 0 12 2
A getIterator() 0 12 3
A freeze() 0 8 2
A getAffectedRows() 0 8 2
A getNumRows() 0 8 2
A from() 0 14 4
1
<?php
2
3
namespace BenTools\SimpleDBAL\Model\Adapter\Mysqli;
4
5
use BenTools\SimpleDBAL\Contract\ResultInterface;
6
use BenTools\SimpleDBAL\Model\Exception\DBALException;
7
use IteratorAggregate;
8
use mysqli;
9
use mysqli_result;
10
use mysqli_stmt;
11
12
final class Result implements IteratorAggregate, ResultInterface
13
{
14
    /**
15
     * @var mysqli|null
16
     */
17
    private $mysqli;
18
19
    /**
20
     * @var mysqli_result|null
21
     */
22
    private $result;
23
24
    private $frozen = false;
25
26
    public function __construct(mysqli $mysqli = null, mysqli_result $result = null)
27
    {
28
        $this->mysqli = $mysqli;
29
        $this->result = $result;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function getLastInsertId()
36
    {
37
        if (null === $this->mysqli) {
38
            throw new DBALException("No \mysqli object provided.");
39
        }
40
41
        return $this->mysqli->insert_id;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function count(): int
48
    {
49
        return null === $this->result ? $this->getAffectedRows() : $this->getNumRows();
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function asArray(): array
56
    {
57
        if (null === $this->result) {
58
            throw new DBALException("No \mysqli_result object provided.");
59
        }
60
61
        $this->freeze();
62
63
        return $this->result->fetch_all(MYSQLI_ASSOC);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function asRow(): ?array
70
    {
71
        if (null === $this->result) {
72
            throw new DBALException("No \mysqli_result object provided.");
73
        }
74
75
        $this->freeze();
76
77
        return $this->result->fetch_array(MYSQLI_ASSOC) ?: null;
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function asList(): array
84
    {
85
        if (null === $this->result) {
86
            throw new DBALException("No \mysqli_result object provided.");
87
        }
88
89
        $this->freeze();
90
91
        $generator = function (mysqli_result $result) {
92
            while (null !== ($row = $result->fetch_array(MYSQLI_NUM))) {
93
                yield $row[0];
94
            }
95
        };
96
97
        return iterator_to_array($generator($this->result));
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function asValue()
104
    {
105
        if (null === $this->result) {
106
            throw new DBALException("No \mysqli_result object provided.");
107
        }
108
109
        $this->freeze();
110
111
        $row = $this->result->fetch_array(MYSQLI_NUM);
112
113
        return $row[0] ?? null;
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function getIterator()
120
    {
121
        if (null === $this->result) {
122
            throw new DBALException("No \mysqli_result object provided.");
123
        }
124
125
        $this->freeze();
126
127
        while ($row = $this->result->fetch_array(MYSQLI_ASSOC)) {
128
            yield $row;
129
        }
130
    }
131
132
    private function freeze(): void
133
    {
134
        if (true === $this->frozen) {
135
            throw new DBALException("This result is frozen. You have to re-execute this statement.");
136
        }
137
138
        $this->frozen = true;
139
    }
140
141
    private function getAffectedRows(): int
142
    {
143
        if (null === $this->mysqli) {
144
            throw new DBALException("No \mysqli object provided.");
145
        }
146
147
        return $this->mysqli->affected_rows;
148
    }
149
150
    private function getNumRows(): int
151
    {
152
        if (null === $this->result) {
153
            throw new DBALException("No \mysqli_result object provided.");
154
        }
155
156
        return $this->result->num_rows;
157
    }
158
159
    public static function from(...$arguments): self
160
    {
161
        $instance = new self();
162
        foreach ($arguments as $argument) {
163
            if ($argument instanceof mysqli) {
164
                $instance->mysqli = $argument;
165
            }
166
            if ($argument instanceof mysqli_result) {
167
                $instance->result = $argument;
168
            }
169
        }
170
171
        return $instance;
172
    }
173
}
174