Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Refactor of Zend\Db\ResultSet for 3.0.0 #370

Open
wants to merge 8 commits into
base: 3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 27 additions & 68 deletions src/ResultSet/AbstractResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,18 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface
* if array, already buffering
* @var mixed
*/
protected $buffer = null;
protected $buffer;

/**
* @var null|int
*/
protected $count = null;
/** @var null|int */
protected $count;

/**
* @var Iterator|IteratorAggregate|ResultInterface
*/
protected $dataSource = null;
/** @var Iterator|IteratorAggregate|ResultInterface */
protected $dataSource;

/**
* @var int
*/
/** @var int */
protected $fieldCount = 0;

/**
* @var int
*/
/** @var int */
protected $position = 0;

/**
Expand All @@ -54,7 +46,7 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface
* @return self Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function initialize($dataSource)
public function initialize($dataSource) : self
{
// reset buffering
if (is_array($this->buffer)) {
Expand Down Expand Up @@ -93,42 +85,33 @@ public function initialize($dataSource)
return $this;
}

/**
* @return self Provides a fluent interface
* @throws Exception\RuntimeException
*/
public function buffer()
public function buffer() : self
{
if ($this->buffer === -2) {
throw new Exception\RuntimeException('Buffering must be enabled before iteration is started');
} elseif ($this->buffer === null) {
}

if ($this->buffer === null) {
$this->buffer = [];
if ($this->dataSource instanceof ResultInterface) {
$this->dataSource->rewind();
}
}

return $this;
}

public function isBuffered()
public function isBuffered() : bool
{
if ($this->buffer === -1 || is_array($this->buffer)) {
return true;
}
return false;
return $this->buffer === -1 || is_array($this->buffer);
}

/**
* Get the data source used to create the result set
*
* @return null|Iterator
*/
public function getDataSource()
public function getDataSource() : ?Iterator
{
return $this->dataSource;
}

public function getFieldCount(): int
public function getFieldCount() : int
{
if (null !== $this->fieldCount) {
return $this->fieldCount;
Expand All @@ -155,12 +138,7 @@ public function getFieldCount(): int
return $this->fieldCount;
}

/**
* Iterator: move pointer to next item
*
* @return void
*/
public function next()
public function next() : void
{
if ($this->buffer === null) {
$this->buffer = -2; // implicitly disable buffering from here on
Expand All @@ -171,12 +149,7 @@ public function next()
$this->position++;
}

/**
* Iterator: retrieve current key
*
* @return mixed
*/
public function key()
public function key() : int
{
return $this->position;
}
Expand Down Expand Up @@ -205,30 +178,21 @@ public function current()
return is_array($data) ? $data : null;
}

/**
* Iterator: is pointer valid?
*
* @return bool
*/
public function valid()
public function valid() : bool
{
if (is_array($this->buffer) && isset($this->buffer[$this->position])) {
return true;
}

if ($this->dataSource instanceof Iterator) {
return $this->dataSource->valid();
} else {
$key = key($this->dataSource);
return ($key !== null);
}

$key = key($this->dataSource);
return ($key !== null);
}

/**
* Iterator: rewind
*
* @return void
*/
public function rewind()
public function rewind() : void
{
if (! is_array($this->buffer)) {
if ($this->dataSource instanceof Iterator) {
Expand All @@ -240,12 +204,7 @@ public function rewind()
$this->position = 0;
}

/**
* Countable: return count of rows
*
* @return int
*/
public function count()
public function count() : int
{
if ($this->count !== null) {
return $this->count;
Expand All @@ -264,7 +223,7 @@ public function count()
* @return array
* @throws Exception\RuntimeException if any row is not castable to an array
*/
public function toArray()
public function toArray() : array
{
$return = [];
foreach ($this as $row) {
Expand Down
10 changes: 5 additions & 5 deletions src/ResultSet/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Db\ResultSet\Exception;

use Zend\Db\Exception;
Expand Down
10 changes: 5 additions & 5 deletions src/ResultSet/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Db\ResultSet\Exception;

use Zend\Db\Exception;
Expand Down
10 changes: 5 additions & 5 deletions src/ResultSet/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Db\ResultSet\Exception;

use Zend\Db\Exception;
Expand Down
78 changes: 17 additions & 61 deletions src/ResultSet/HydratingResultSet.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace Zend\Db\ResultSet;

use ArrayObject;
Expand All @@ -16,85 +16,47 @@

class HydratingResultSet extends AbstractResultSet
{
/**
* @var HydratorInterface
*/
protected $hydrator = null;
/** @var HydratorInterface */
protected $hydrator;

/**
* @var null|object
*/
protected $objectPrototype = null;
/** @var null|object */
protected $objectPrototype;

/**
* Constructor
*
* @param null|HydratorInterface $hydrator
* @param null|object $objectPrototype
*/
public function __construct(HydratorInterface $hydrator = null, $objectPrototype = null)
public function __construct(?HydratorInterface $hydrator = null, ?object $objectPrototype = null)
{
$defaultHydratorClass = class_exists(ArraySerializableHydrator::class)
? ArraySerializableHydrator::class
: ArraySerializable::class;
$this->setHydrator($hydrator ?: new $defaultHydratorClass());
$this->setObjectPrototype(($objectPrototype) ?: new ArrayObject);
$this->setObjectPrototype($objectPrototype ?: new ArrayObject);
}

/**
* Set the row object prototype
*
* @param object $objectPrototype
* @return self Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
public function setObjectPrototype($objectPrototype)
public function setObjectPrototype(object $objectPrototype) : self
{
if (! is_object($objectPrototype)) {
throw new Exception\InvalidArgumentException(
'An object must be set as the object prototype, a ' . gettype($objectPrototype) . ' was provided.'
);
}
$this->objectPrototype = $objectPrototype;
return $this;
}

/**
* Get the row object prototype
*
* @return object
*/
public function getObjectPrototype()
public function getObjectPrototype() : object
{
return $this->objectPrototype;
}

/**
* Set the hydrator to use for each row object
*
* @param HydratorInterface $hydrator
* @return self Provides a fluent interface
*/
public function setHydrator(HydratorInterface $hydrator)
public function setHydrator(HydratorInterface $hydrator) : self
{
$this->hydrator = $hydrator;
return $this;
}

/**
* Get the hydrator to use for each row object
*
* @return HydratorInterface
*/
public function getHydrator()
public function getHydrator() : HydratorInterface
{
return $this->hydrator;
}

/**
* Iterator: get current item
*
* @return object
* @return null|array|bool
*/
public function current()
{
Expand All @@ -113,13 +75,7 @@ public function current()
return $object;
}

/**
* Cast result set to array of arrays
*
* @return array
* @throws Exception\RuntimeException if any row is not castable to an array
*/
public function toArray()
public function toArray() : array
{
$return = [];
foreach ($this as $row) {
Expand Down
Loading