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

Result::count()   A

Complexity

Conditions 3
Paths 4

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 3
eloc 2
nc 4
nop 0
1
<?php
2
3
namespace vakata\database\driver\mysql;
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 $row = [];
13
    protected $last = null;
14
    protected $fetched = -1;
15
    protected $result = null;
16
    protected $nativeDriver = false;
17
18
    public function __construct(\mysqli_stmt $statement)
19
    {
20
        $this->nativeDriver = function_exists('\mysqli_fetch_all');
21
        $this->statement = $statement;
22
        try {
23
            if ($this->nativeDriver) {
24
                $this->result = $this->statement->get_result();
25
            } else {
26
                $this->statement->store_result();
27
                $columns = [];
28
                $temp = $this->statement->result_metadata();
29
                if ($temp) {
30
                    $temp = $temp->fetch_fields();
31
                    if ($temp) {
32
                        $columns = array_map(function ($v) { return $v->name; }, $temp);
33
                    }
34
                }
35
                if (count($columns)) {
36
                    $this->row = array_combine($columns, array_fill(0, count($columns), null));
37
                    $temp = [];
38
                    foreach ($this->row as $k => $v) {
39
                        $temp[] = &$this->row[$k];
40
                    }
41
                    call_user_func_array(array($this->statement, 'bind_result'), $temp);
42
                }
43
            }
44
        } catch (\Exception $e) { }
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
45
    }
46
    public function affected() : int
47
    {
48
        return $this->statement->affected_rows;
49
    }
50
    public function insertID()
51
    {
52
        return $this->statement->insert_id;
53
    }
54
    public function toArray() : array
55
    {
56
        return iterator_to_array($this);
57
    }
58
59
    public function count()
60
    {
61
        return $this->nativeDriver && $this->result ? $this->result->num_rows : $this->statement->num_rows;
62
    }
63
64
    public function key()
65
    {
66
        return $this->fetched;
67
    }
68
    public function current()
69
    {
70
        return $this->nativeDriver ? $this->last : array_map(function ($v) { return $v; }, $this->row);
71
    }
72
    public function rewind()
73
    {
74
        if ($this->fetched >= 0) {
75
            if ($this->nativeDriver) {
76
                $this->result->data_seek(0);
77
            } else {
78
                $this->statement->data_seek(0);
79
            }
80
        }
81
        $this->last = null;
82
        $this->fetched = -1;
83
        $this->next();
84
    }
85
    public function next()
86
    {
87
        $this->fetched ++;
88
        $this->last = $this->nativeDriver ? $this->result->fetch_assoc() : $this->statement->fetch();
89
    }
90
    public function valid()
91
    {
92
        return !!$this->last;
93
    }
94
}