Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

lib/Doctrine/DBAL/Cache/ArrayStatement.php (2 issues)

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Cache;
21
22
use Doctrine\DBAL\Driver\ResultStatement;
23
use PDO;
24
25
class ArrayStatement implements \IteratorAggregate, ResultStatement
26
{
27
    /**
28
     * @var array
29
     */
30
    private $data;
31
32
    /**
33
     * @var integer
34
     */
35
    private $columnCount = 0;
36
37
    /**
38
     * @var integer
39
     */
40
    private $num = 0;
41
42
    /**
43
     * @var integer
44
     */
45
    private $defaultFetchMode = PDO::FETCH_BOTH;
46
47
    /**
48
     * @param array $data
49
     */
50 9
    public function __construct(array $data)
51
    {
52 9
        $this->data = $data;
53 9
        if (count($data)) {
54 6
            $this->columnCount = count($data[0]);
55
        }
56 9
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 7
    public function closeCursor()
62
    {
63 7
        unset ($this->data);
64 7
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 4
    public function columnCount()
70
    {
71 4
        return $this->columnCount;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 9
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
78
    {
79 9
        if ($arg2 !== null || $arg3 !== null) {
80
            throw new \InvalidArgumentException("Caching layer does not support 2nd/3rd argument to setFetchMode()");
81
        }
82
83 9
        $this->defaultFetchMode = $fetchMode;
84
85 9
        return true;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function getIterator()
92
    {
93 1
        $data = $this->fetchAll();
94
95 1
        return new \ArrayIterator($data);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 7
    public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
102
    {
103 7
        if (isset($this->data[$this->num])) {
104 6
            $row = $this->data[$this->num++];
105 6
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
106 6
            if ($fetchMode === PDO::FETCH_ASSOC) {
107 2
                return $row;
108 5
            } elseif ($fetchMode === PDO::FETCH_NUM) {
109 3
                return array_values($row);
110 3
            } elseif ($fetchMode === PDO::FETCH_BOTH) {
111 2
                return array_merge($row, array_values($row));
112 1
            } elseif ($fetchMode === PDO::FETCH_COLUMN) {
113 1
                return reset($row);
114
            } else {
115
                throw new \InvalidArgumentException("Invalid fetch-style given for fetching result.");
116
            }
117
        }
118
119 7
        return false;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1 View Code Duplication
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127 1
        $rows = [];
128 1
        while ($row = $this->fetch($fetchMode)) {
129 1
            $rows[] = $row;
130
        }
131
132 1
        return $rows;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 View Code Duplication
    public function fetchColumn($columnIndex = 0)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $row = $this->fetch(PDO::FETCH_NUM);
141
        if (!isset($row[$columnIndex])) {
142
            // TODO: verify this is correct behavior
143
            return false;
144
        }
145
146
        return $row[$columnIndex];
147
    }
148
}
149