Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

Result::insertID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace vakata\database\driver\pdo;
4
5
use \vakata\database\DriverInterface;
6
use \vakata\database\ResultInterface;
7
use \vakata\collection\Collection;
8
9
class Result implements ResultInterface
10
{
11
    protected $statement;
12
    protected $driver;
13
    protected $fetched = -1;
14
    protected $last = null;
15
16
    public function __construct(\PDOStatement $statement, \PDO $driver)
17
    {
18
        $this->statement = $statement;
19
        $this->driver = $driver;
20
    }
21
    public function affected() : int
22
    {
23
        return $this->statement->rowCount();
24
    }
25
    public function insertID()
26
    {
27
        return $this->driver->lastInsertId();
28
    }
29
    public function toArray() : array
30
    {
31
        return iterator_to_array($this);
32
    }
33
34
    public function count()
35
    {
36
        return $this->statement->rowCount();
37
    }
38
39
    public function key()
40
    {
41
        return $this->fetched;
42
    }
43
    public function current()
44
    {
45
        return $this->last;
46
    }
47
    public function rewind()
48
    {
49
        if ($this->fetched >= 0) {
50
            $this->statement->execute();
51
        }
52
        $this->last = null;
53
        $this->fetched = -1;
54
        $this->next();
55
    }
56
    public function next()
57
    {
58
        $this->fetched ++;
59
        $this->last = $this->statement->fetch(\PDO::FETCH_ASSOC);
60
    }
61
    public function valid()
62
    {
63
        return !!$this->last;
64
    }
65
}