Completed
Push — master ( 558b0d...64816c )
by Sébastien
04:12
created

Resultset   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 68.08%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 201
wmc 20
lcom 1
cbo 1
ccs 32
cts 47
cp 0.6808
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A count() 0 4 1
A append() 0 9 2
A offsetExists() 0 4 1
A offsetGet() 0 4 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getArray() 0 4 1
A getArrayObject() 0 8 2
A getReturnType() 0 4 1
1
<?php
2
3
namespace Soluble\DbWrapper\Result;
4
5
use ArrayObject;
6
use Soluble\DbWrapper\Exception;
7
8
class Resultset implements ResultInterface
9
{
10
11
    const TYPE_ARRAYOBJECT = 'arrayobject';
12
    const TYPE_ARRAY  = 'array';
13
14
    /**
15
     * Allowed return types
16
     *
17
     * @var array
18
     */
19
    protected $allowedReturnTypes = [
20
        self::TYPE_ARRAYOBJECT,
21
        self::TYPE_ARRAY,
22
    ];
23
24
25
    /**
26
     * Return type to use when returning an object from the set
27
     *
28
     * @var ResultSet::TYPE_ARRAYOBJECT|ResultSet::TYPE_ARRAY
29
     */
30
    protected $returnType = self::TYPE_ARRAY;
31
32
33
    /**
34
     *
35
     * @var integer 
36
     */
37
    protected $position = 0;
38
39
    /**
40
     *
41
     * @var integer
42
     */
43
    protected $count = 0;
44
45
    /**
46
     *
47
     * @var array|ArrayObject
48
     */
49
    protected $storage;
50
51
52
    /**
53
     * Constructor
54
     *
55
     * @throws Exception\InvalidArgumentException
56
     * @param string $returnType
57
     */
58 15
    public function __construct($returnType = self::TYPE_ARRAY)
59
    {
60 15
        if (!in_array($returnType, $this->allowedReturnTypes)) {
61
            throw new Exception\InvalidArgumentException("Unsupported returnType argument ($returnType)");
62
        }
63 15
        $this->returnType = $returnType;
64 15
        if ($this->returnType == self::TYPE_ARRAYOBJECT) {
65 2
            $this->storage = new ArrayObject([]);
66 2
        }
67 15
        $this->position = 0;
68 15
        $this->count = count($this->storage);
69 15
    }
70
71
    /**
72
     * {@inheritdoc}
73
     * @return array|ArrayObject
74
     */
75 2
    public function current()
76
    {
77 2
        return $this->storage[$this->position];
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     * @return int position
83
     */
84
    public function key()
85
    {
86
        return $this->position;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function next()
93
    {
94 2
        ++$this->position;
95 2
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 2
    public function rewind()
101
    {
102 2
        $this->position=0;
103 2
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function valid()
109
    {
110 2
        return isset($this->storage[$this->position]);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 6
    public function count()
117
    {
118 6
        return $this->count;
119
    }
120
121
    /**
122
     * Append a row to the end of resultset
123
     * 
124
     * @param array $row an associative array
125
     */
126 10
    public function append(array $row)
127
    {
128 10
        if ($this->returnType == self::TYPE_ARRAYOBJECT) {
129 2
            $this->storage[] = new ArrayObject($row);
130 2
        } else {
131 9
            $this->storage[] = $row;
132
        }
133 10
        ++$this->count;
134 10
    }
135
136
137
    /**
138
     * 
139
     * {@inheritdoc}
140
     */
141
    public function offsetExists($position)
142
    {
143
        return isset($this->storage[$position]);
144
    }
145
146
    /**
147
     * 
148
     * {@inheritdoc}
149
     */
150 10
    public function offsetGet($position)
151
    {
152 10
        return isset($this->storage[$position]) ? $this->storage[$position] : null;
153
    }
154
155
    /**
156
     * 
157
     * {@inheritdoc}
158
     */
159
    public function offsetSet($position, $row)
160
    {
161
        throw new \Exception('Resultsets are immutable');
162
    }
163
164
    /**
165
     * 
166
     * {@inheritdoc}
167
     */
168
    public function offsetUnset($position)
169
    {
170
        throw new \Exception("Resultsets are immutable");
171
    }
172
173
    /**
174
     * Return underlying stored resultset as array
175
     * @return array
176
     */
177 2
    public function getArray()
178
    {
179 2
        return (array) $this->storage;
180
    }
181
182
    /**
183
     * Return underlying stored resultset as ArrayObject
184
     * 
185
     * Depending on the $returnType Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT you can modify
186
     * the internal storage
187
     * 
188
     * @return ArrayObject
189
     */
190
    public function getArrayObject()
191
    {
192
        if ($this->returnType == self::TYPE_ARRAY) {
193
            return new ArrayObject($this->storage);
194
        } else {
195
            return $this->storage;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->storage; of type array|ArrayObject adds the type array to the return on line 195 which is incompatible with the return type documented by Soluble\DbWrapper\Result\Resultset::getArrayObject of type ArrayObject.
Loading history...
196
        }
197
    }
198
199
    /**
200
     * Return the currently set return type 
201
     * @see Resultset::TYPE_ARRAY|Resultset::TYPE_ARRAYOBJECT
202
     * @return string
203
     */
204
    public function getReturnType()
205
    {
206
        return $this->returnType;
207
    }
208
}
209