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

Refactor of Zend\Db\RowGateway for 3.0.0 #371

Open
wants to merge 10 commits into
base: 3.0.0
Choose a base branch
from
4 changes: 4 additions & 0 deletions 3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ Changed `setDriver(DriverInterface $driver)` for all the following platforms:
- Zend\Db\Adapter\Platform\Postgresql
- Zend\Db\Adapter\Platform\Sqlite
- Zend\Db\Adapter\Platform\SqlServer

- Zend\Db\RowGateway\AbstractRowGateway

We cannot use type hint for parameter in `offsetExists()` because of definition in `\ArrayAccess`
163 changes: 45 additions & 118 deletions src/RowGateway/AbstractRowGateway.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\RowGateway;

use ArrayAccess;
Expand All @@ -16,40 +16,26 @@

abstract class AbstractRowGateway implements ArrayAccess, Countable, RowGatewayInterface
{
/**
* @var bool
*/
/** @var bool */
protected $isInitialized = false;

/**
* @var string|TableIdentifier
*/
protected $table = null;
/** @var string|TableIdentifier */
protected $table;

/**
* @var array
*/
protected $primaryKeyColumn = null;
/** @var array */
protected $primaryKeyColumn = [];

/**
* @var array
*/
protected $primaryKeyData = null;
/** @var array */
protected $primaryKeyData = [];

/**
* @var array
*/
/** @var array */
protected $data = [];

/**
* @var Sql
*/
protected $sql = null;
/** @var Sql */
protected $sql;

/**
* @var Feature\FeatureSet
*/
protected $featureSet = null;
/** @var Feature\FeatureSet */
protected $featureSet;

/**
* initialize()
Expand All @@ -71,12 +57,14 @@ public function initialize()
throw new Exception\RuntimeException('This row object does not have a valid table set.');
}

if ($this->primaryKeyColumn === null) {
throw new Exception\RuntimeException('This row object does not have a primary key column set.');
} elseif (is_string($this->primaryKeyColumn)) {
if (is_string($this->primaryKeyColumn)) {
$this->primaryKeyColumn = (array) $this->primaryKeyColumn;
}

if (count($this->primaryKeyColumn) === 0) {
throw new Exception\RuntimeException('This row object does not have a primary key column set.');
}

if (! $this->sql instanceof Sql) {
throw new Exception\RuntimeException('This row object does not have a Sql object set.');
}
Expand All @@ -86,42 +74,26 @@ public function initialize()
$this->isInitialized = true;
}

/**
* Populate Data
*
* @param array $rowData
* @param bool $rowExistsInDatabase
* @return self Provides a fluent interface
*/
public function populate(array $rowData, $rowExistsInDatabase = false)
public function populate(array $rowData, bool $rowExistsInDatabase = false) : self
{
$this->initialize();

$this->data = $rowData;
if ($rowExistsInDatabase == true) {
if ($rowExistsInDatabase === true) {
$this->processPrimaryKeyData();
} else {
$this->primaryKeyData = null;
$this->primaryKeyData = [];
}

return $this;
}

/**
* @param mixed $array
* @return AbstractRowGateway
*/
public function exchangeArray($array)
public function exchangeArray(array $array) : self
{
return $this->populate($array, true);
}

/**
* Save
*
* @return int
*/
public function save()
public function save() : int
{
$this->initialize();

Expand Down Expand Up @@ -163,7 +135,7 @@ public function save()
$statement = $this->sql->prepareStatementForSqlObject($insert);

$result = $statement->execute();
if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) == 1) {
if (($primaryKeyValue = $result->getGeneratedValue()) && count($this->primaryKeyColumn) === 1) {
$this->primaryKeyData = [$this->primaryKeyColumn[0] => $primaryKeyValue];
} else {
// make primary key data available so that $where can be complete
Expand Down Expand Up @@ -192,12 +164,7 @@ public function save()
return $rowsAffected;
}

/**
* Delete
*
* @return int
*/
public function delete()
public function delete() : int
{
$this->initialize();

Expand All @@ -213,9 +180,9 @@ public function delete()
$result = $statement->execute();

$affectedRows = $result->getAffectedRows();
if ($affectedRows == 1) {
if ($affectedRows === 1) {
// detach from database
$this->primaryKeyData = null;
$this->primaryKeyData = [];
}

return $affectedRows;
Expand All @@ -227,7 +194,7 @@ public function delete()
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset) : bool
{
return array_key_exists($offset, $this->data);
}
Expand All @@ -250,7 +217,7 @@ public function offsetGet($offset)
* @param mixed $value
* @return self Provides a fluent interface
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value) : self
{
$this->data[$offset] = $value;
return $this;
Expand All @@ -262,92 +229,52 @@ public function offsetSet($offset, $value)
* @param string $offset
* @return self Provides a fluent interface
*/
public function offsetUnset($offset)
public function offsetUnset($offset) : self
{
$this->data[$offset] = null;
return $this;
}

/**
* @return int
*/
public function count()
public function count() : int
{
return count($this->data);
}

/**
* To array
*
* @return array
*/
public function toArray()
public function toArray() : array
{
return $this->data;
}

/**
* __get
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function __get($name)
public function __get(string $name)
{
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
} else {
throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name);
}

throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name);
}

/**
* __set
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
public function __set(string $name, $value) : void
{
$this->offsetSet($name, $value);
}

/**
* __isset
*
* @param string $name
* @return bool
*/
public function __isset($name)
public function __isset(string $name) : bool
{
return $this->offsetExists($name);
}

/**
* __unset
*
* @param string $name
* @return void
*/
public function __unset($name)
public function __unset(string $name) : void
{
$this->offsetUnset($name);
}

/**
* @return bool
*/
public function rowExistsInDatabase()
public function rowExistsInDatabase() : bool
{
return ($this->primaryKeyData !== null);
return count($this->primaryKeyData) > 0;
}

/**
* @throws Exception\RuntimeException
*/
protected function processPrimaryKeyData()
protected function processPrimaryKeyData() : void
{
$this->primaryKeyData = [];
foreach ($this->primaryKeyColumn as $column) {
Expand Down
10 changes: 5 additions & 5 deletions src/RowGateway/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\RowGateway\Exception;

use Zend\Db\Exception;
Expand Down
10 changes: 5 additions & 5 deletions src/RowGateway/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\RowGateway\Exception;

use Zend\Db\Exception;
Expand Down
10 changes: 5 additions & 5 deletions src/RowGateway/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\RowGateway\Exception;

use Zend\Db\Exception;
Expand Down
Loading