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

Result::withoutStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BenTools\SimpleDBAL\Model\Adapter\PDO;
4
5
use BenTools\SimpleDBAL\Contract\ResultInterface;
6
use BenTools\SimpleDBAL\Model\Exception\DBALException;
7
use IteratorAggregate;
8
use PDO;
9
use PDOStatement;
10
11
final class Result implements IteratorAggregate, ResultInterface
12
{
13
    private $pdo;
14
    private $stmt;
15
    private $frozen = false;
16
17
    public function __construct(PDO $pdo = null, PDOStatement $stmt = null)
18
    {
19
        $this->pdo = $pdo;
20
        $this->stmt = $stmt;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function getLastInsertId()
27
    {
28
        if (null === $this->pdo) {
29
            throw new DBALException("No \PDO object provided.");
30
        }
31
32
        return $this->pdo->lastInsertId();
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function count(): int
39
    {
40
        if (null === $this->stmt) {
41
            throw new DBALException("No \PDOStatement object provided.");
42
        }
43
44
        return $this->stmt->rowCount();
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function asArray(): array
51
    {
52
        if (null === $this->stmt) {
53
            throw new DBALException("No \PDOStatement object provided.");
54
        }
55
56
        $this->freeze();
57
58
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function asRow(): ?array
65
    {
66
        if (null === $this->stmt) {
67
            throw new DBALException("No \PDOStatement object provided.");
68
        }
69
70
        $this->freeze();
71
72
        return $this->stmt->fetch(PDO::FETCH_ASSOC) ?: null;
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function asList(): array
79
    {
80
        if (null === $this->stmt) {
81
            throw new DBALException("No \PDOStatement object provided.");
82
        }
83
84
        $this->freeze();
85
86
        $generator = static function (PDOStatement $stmt) {
87
            while (false !== ($value = $stmt->fetchColumn(0))) {
88
                yield $value;
89
            }
90
        };
91
92
        return iterator_to_array($generator($this->stmt));
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function asValue()
99
    {
100
        if (null === $this->stmt) {
101
            throw new DBALException("No \PDOStatement object provided.");
102
        }
103
104
        $this->freeze();
105
106
        $value = $this->stmt->fetchColumn(0);
107
108
        return false !== $value ? $value : null;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function getIterator()
115
    {
116
        if (null === $this->stmt) {
117
            throw new DBALException("No \PDOStatement object provided.");
118
        }
119
120
        $this->freeze();
121
122
        while ($row = $this->stmt->fetch(PDO::FETCH_ASSOC)) {
123
            yield $row;
124
        }
125
    }
126
127
    private function freeze(): void
128
    {
129
        if (true === $this->frozen) {
130
            throw new DBALException("This result is frozen. You have to re-execute this statement.");
131
        }
132
133
        $this->frozen = true;
134
    }
135
136
    public static function from(...$arguments): self
137
    {
138
        $instance = new self();
139
        foreach ($arguments as $argument) {
140
            if ($argument instanceof PDO) {
141
                $instance->pdo = $argument;
142
            }
143
            if ($argument instanceof PDOStatement) {
144
                $instance->stmt = $argument;
145
            }
146
        }
147
148
        return $instance;
149
    }
150
}
151