Failed Conditions
Push — 3.0.x ( 66f057...14f5f1 )
by Sergei
36s queued 17s
created

ArrayStatement   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getIterator() 0 5 1
A columnCount() 0 3 1
A setFetchMode() 0 9 2
A closeCursor() 0 5 1
A fetchAll() 0 8 2
A fetchColumn() 0 6 1
A fetch() 0 26 6
1
<?php
2
3
namespace Doctrine\DBAL\Cache;
4
5
use ArrayIterator;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\FetchMode;
8
use InvalidArgumentException;
9
use IteratorAggregate;
10
use function array_merge;
11
use function array_values;
12
use function count;
13
use function reset;
14
15
class ArrayStatement implements IteratorAggregate, ResultStatement
16
{
17
    /** @var mixed[] */
18
    private $data;
19
20
    /** @var int */
21
    private $columnCount = 0;
22
23
    /** @var int */
24
    private $num = 0;
25
26
    /** @var int */
27
    private $defaultFetchMode = FetchMode::MIXED;
28
29
    /**
30
     * @param mixed[] $data
31
     */
32
    public function __construct(array $data)
33
    {
34
        $this->data = $data;
35
        if (count($data) === 0) {
36
            return;
37
        }
38
39
        $this->columnCount = count($data[0]);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function closeCursor()
46
    {
47
        unset($this->data);
48
49
        return true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function columnCount()
56
    {
57
        return $this->columnCount;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function setFetchMode($fetchMode, ...$args)
64
    {
65
        if (count($args) > 0) {
66
            throw new InvalidArgumentException('Caching layer does not support 2nd/3rd argument to setFetchMode()');
67
        }
68
69
        $this->defaultFetchMode = $fetchMode;
70
71
        return true;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getIterator()
78
    {
79
        $data = $this->fetchAll();
80
81
        return new ArrayIterator($data);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function fetch($fetchMode = null, ...$args)
88
    {
89
        if (! isset($this->data[$this->num])) {
90
            return false;
91
        }
92
93
        $row       = $this->data[$this->num++];
94
        $fetchMode = $fetchMode ?? $this->defaultFetchMode;
95
96
        if ($fetchMode === FetchMode::ASSOCIATIVE) {
97
            return $row;
98
        }
99
100
        if ($fetchMode === FetchMode::NUMERIC) {
101
            return array_values($row);
102
        }
103
104
        if ($fetchMode === FetchMode::MIXED) {
105
            return array_merge($row, array_values($row));
106
        }
107
108
        if ($fetchMode === FetchMode::COLUMN) {
109
            return reset($row);
110
        }
111
112
        throw new InvalidArgumentException('Invalid fetch-style given for fetching result.');
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function fetchAll($fetchMode = null, ...$args)
119
    {
120
        $rows = [];
121
        while ($row = $this->fetch($fetchMode, ...$args)) {
122
            $rows[] = $row;
123
        }
124
125
        return $rows;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function fetchColumn($columnIndex = 0)
132
    {
133
        $row = $this->fetch(FetchMode::NUMERIC);
134
135
        // TODO: verify that return false is the correct behavior
136
        return $row[$columnIndex] ?? false;
137
    }
138
}
139