Completed
Pull Request — 4.0 (#64)
by
unknown
05:04
created

ResultIterator::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Mouf\Database\TDBM;
4
5
use Doctrine\DBAL\Statement;
6
use Mouf\Database\MagicQuery;
7
use Porpaginas\Result;
8
use Traversable;
9
10
/*
11
 Copyright (C) 2006-2016 David Négrier - THE CODING MACHINE
12
13
 This program is free software; you can redistribute it and/or modify
14
 it under the terms of the GNU General Public License as published by
15
 the Free Software Foundation; either version 2 of the License, or
16
 (at your option) any later version.
17
18
 This program is distributed in the hope that it will be useful,
19
 but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 GNU General Public License for more details.
22
23
 You should have received a copy of the GNU General Public License
24
 along with this program; if not, write to the Free Software
25
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26
 */
27
28
/**
29
 * Iterator used to retrieve results.
30
 */
31
class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
32
{
33
    /**
34
     * @var Statement
35
     */
36
    protected $statement;
37
38
    protected $fetchStarted = false;
39
    private $objectStorage;
40
    private $className;
41
42
    private $tdbmService;
43
    private $magicSql;
44
    private $magicSqlCount;
45
    private $parameters;
46
    private $columnDescriptors;
47
    private $magicQuery;
48
49
    /**
50
     * @var InnerResultIterator
51
     */
52
    private $innerResultIterator;
53
54
    /**
55
     * The key of the current retrieved object.
56
     *
57
     * @var int
58
     */
59
    protected $key = -1;
60
61
    protected $current = null;
62
63
    private $databasePlatform;
64
65
    private $totalCount;
66
67
    private $mode;
68
69 View Code Duplication
    public function __construct($magicSql, $magicSqlCount, array $parameters, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode)
0 ignored issues
show
Duplication introduced by
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...
70
    {
71
        $this->magicSql = $magicSql;
72
        $this->magicSqlCount = $magicSqlCount;
73
        $this->objectStorage = $objectStorage;
74
        $this->className = $className;
75
        $this->tdbmService = $tdbmService;
76
        $this->parameters = $parameters;
77
        $this->columnDescriptors = $columnDescriptors;
78
        $this->magicQuery = $magicQuery;
79
        $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
80
        $this->mode = $mode;
81
    }
82
83
    protected function executeCountQuery()
84
    {
85
        $sql = $this->magicQuery->build($this->magicSqlCount, $this->parameters);
86
        $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters);
87
    }
88
89
    /**
90
     * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings).
91
     *
92
     * @return int
93
     */
94
    public function count()
95
    {
96
        if ($this->totalCount === null) {
97
            $this->executeCountQuery();
98
        }
99
100
        return $this->totalCount;
101
    }
102
103
    /**
104
     * Casts the result set to a PHP array.
105
     *
106
     * @return array
107
     */
108
    public function toArray()
109
    {
110
        return iterator_to_array($this->getIterator());
111
    }
112
113
    /**
114
     * Returns a new iterator mapping any call using the $callable function.
115
     *
116
     * @param callable $callable
117
     *
118
     * @return MapIterator
119
     */
120
    public function map(callable $callable)
121
    {
122
        return new MapIterator($this->getIterator(), $callable);
123
    }
124
125
    /**
126
     * Retrieve an external iterator.
127
     *
128
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
129
     *
130
     * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
131
     *                             <b>Traversable</b>
132
     *
133
     * @since 5.0.0
134
     */
135
    public function getIterator()
136
    {
137
        if ($this->innerResultIterator === null) {
138
            if ($this->mode === TDBMService::MODE_CURSOR) {
139
                $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
140
            } else {
141
                $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, null, null, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery);
142
            }
143
        }
144
145
        return $this->innerResultIterator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->innerResultIterator; (Mouf\Database\TDBM\InnerResultIterator) is incompatible with the return type declared by the interface Porpaginas\Result::getIterator of type Porpaginas\Iterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
146
    }
147
148
    /**
149
     * @param int $offset
150
     *
151
     * @return PageIterator
152
     */
153
    public function take($offset, $limit)
154
    {
155
        return new PageIterator($this, $this->magicSql, $this->parameters, $limit, $offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode);
156
    }
157
158
    /**
159
     * Whether a offset exists.
160
     *
161
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
162
     *
163
     * @param mixed $offset <p>
164
     *                      An offset to check for.
165
     *                      </p>
166
     *
167
     * @return bool true on success or false on failure.
168
     *              </p>
169
     *              <p>
170
     *              The return value will be casted to boolean if non-boolean was returned.
171
     *
172
     * @since 5.0.0
173
     */
174
    public function offsetExists($offset)
175
    {
176
        return $this->getIterator()->offsetExists($offset);
177
    }
178
179
    /**
180
     * Offset to retrieve.
181
     *
182
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
183
     *
184
     * @param mixed $offset <p>
185
     *                      The offset to retrieve.
186
     *                      </p>
187
     *
188
     * @return mixed Can return all value types.
189
     *
190
     * @since 5.0.0
191
     */
192
    public function offsetGet($offset)
193
    {
194
        return $this->getIterator()->offsetGet($offset);
195
    }
196
197
    /**
198
     * Offset to set.
199
     *
200
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
201
     *
202
     * @param mixed $offset <p>
203
     *                      The offset to assign the value to.
204
     *                      </p>
205
     * @param mixed $value  <p>
206
     *                      The value to set.
207
     *                      </p>
208
     *
209
     * @since 5.0.0
210
     */
211
    public function offsetSet($offset, $value)
212
    {
213
        return $this->getIterator()->offsetSet($offset, $value);
214
    }
215
216
    /**
217
     * Offset to unset.
218
     *
219
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
220
     *
221
     * @param mixed $offset <p>
222
     *                      The offset to unset.
223
     *                      </p>
224
     *
225
     * @since 5.0.0
226
     */
227
    public function offsetUnset($offset)
228
    {
229
        return $this->getIterator()->offsetUnset($offset);
230
    }
231
232
    /**
233
     * Specify data which should be serialized to JSON.
234
     *
235
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
236
     *
237
     * @return mixed data which can be serialized by <b>json_encode</b>,
238
     *               which is a value of any type other than a resource.
239
     *
240
     * @since 5.4.0
241
     */
242
    public function jsonSerialize()
243
    {
244
        return array_map(function (AbstractTDBMObject $item) {
245
            return $item->jsonSerialize();
246
        }, $this->toArray());
247
    }
248
249
    /**
250
     * Returns only one value (the first) of the result set.
251
     * Returns null if no value exists.
252
     *
253
     * @return mixed|null
254
     */
255
    public function first()
256
    {
257
        $page = $this->take(0, 1);
258
        foreach ($page as $bean) {
259
            return $bean;
260
        }
261
        return null;
262
    }
263
}
264