Skip to content

Commit 336e466

Browse files
authored
Merge pull request #3 from Antevenio/custom_row_class
Rows to be returned as custom objects if desired.
2 parents d3df9c0 + ff0688a commit 336e466

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"PdoMysqlSelectIterator\\": "src/"
2222
}
2323
},
24+
"autoload-dev": {
25+
"psr-4": { "PdoMysqlSelectIterator\\Test\\": "test/" }
26+
},
2427
"require": {
2528
"php": ">=5.6"
2629
},

src/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Factory {
77
* @param $query
88
* @param $blockSize
99
* @return Iterator
10+
* @throws Exception\InvalidQueryException
1011
*/
1112
public function create(\PDO $adapter, $query, $blockSize)
1213
{

src/LimitIterator.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class LimitIterator implements \Iterator, Iterator
4545
*/
4646
protected $results;
4747

48+
protected $rowClass;
49+
50+
/**
51+
* LimitIterator constructor.
52+
* @param \PDO $pdo
53+
* @param $query
54+
* @param int $blockSize
55+
* @throws InvalidQueryException
56+
*/
4857
public function __construct(\PDO $pdo, $query, $blockSize = self::BLOCK_SIZE)
4958
{
5059
$this->assertValidQuery($query);
@@ -55,8 +64,18 @@ public function __construct(\PDO $pdo, $query, $blockSize = self::BLOCK_SIZE)
5564
$this->blockSize = $blockSize;
5665
$this->results = null;
5766
$this->rowCount = null;
67+
$this->rowClass = null;
5868
}
5969

70+
public function setRowClass($rowClass)
71+
{
72+
$this->rowClass = $rowClass;
73+
}
74+
75+
/**
76+
* @param $query
77+
* @throws InvalidQueryException
78+
*/
6079
protected function assertValidQuery($query)
6180
{
6281
if (!$this->isAValidSelect($query)) {
@@ -128,7 +147,14 @@ public function key()
128147

129148
public function current()
130149
{
131-
return ($this->results[$this->currentBlockIndex]);
150+
$rowdata = $this->results[$this->currentBlockIndex];
151+
if ($this->rowClass) {
152+
/** @var Row $row */
153+
$row = new $this->rowClass();
154+
$row->hydrate($rowdata);
155+
return ($row);
156+
}
157+
return ($rowdata);
132158
}
133159

134160
public function rewind()

src/Row.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace PdoMysqlSelectIterator;
4+
5+
interface Row
6+
{
7+
/**
8+
* @param array $row
9+
* @return mixed
10+
*/
11+
public function hydrate(array $row);
12+
}

test/FactoryTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
2+
namespace PdoMysqlSelectIterator\Test;
3+
4+
use PDO;
25
use PdoMysqlSelectIterator\LimitIterator;
3-
use PdoMysqlSelectIterator\NativePDOIterator;
6+
use PDOStatement;
47
use PHPUnit\Framework\TestCase;
8+
use PHPUnit_Framework_MockObject_MockObject;
59

610
class FactoryTest extends TestCase
711
{

test/IteratorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2+
namespace PdoMysqlSelectIterator\Test;
3+
24
use PdoMysqlSelectIterator\LimitIterator;
5+
use PDOStatement;
36
use PHPUnit\Framework\TestCase;
47

58
class IteratorTest extends TestCase
@@ -192,6 +195,24 @@ public function testCurrentShouldReturnCurrentRow()
192195
}
193196
}
194197

198+
public function testCurrentShouldReturnAnHydratedRowClassIfDefined()
199+
{
200+
$this->sut->setRowClass(TestRow::class);
201+
$this->setPdoQueryExpectations(
202+
[
203+
[3, 0, self::DATA_BLOCKS[0]],
204+
[3, 3, self::DATA_BLOCKS[1]]
205+
]
206+
);
207+
$this->sut->rewind();
208+
for ($i = 0; $i < 3; $i++) {
209+
$this->assertEquals(
210+
["a" => "a".($i+1), "b" => "b".($i+1)], $this->sut->current()->getRow()
211+
);
212+
$this->sut->next();
213+
}
214+
}
215+
195216
public function testValidShouldReturnTrueWhenCurrentRowExists()
196217
{
197218
$this->setPdoQueryExpectations(

test/TestRow.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace PdoMysqlSelectIterator\Test;
3+
4+
use PdoMysqlSelectIterator\Row;
5+
6+
class TestRow implements Row
7+
{
8+
protected $row;
9+
10+
public function hydrate(array $row)
11+
{
12+
$this->row = $row;
13+
}
14+
15+
public function getRow()
16+
{
17+
return $this->row;
18+
}
19+
}

0 commit comments

Comments
 (0)