diff --git a/3.0.0.md b/3.0.0.md index b1c3abfb90..c48427557f 100644 --- a/3.0.0.md +++ b/3.0.0.md @@ -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` diff --git a/src/RowGateway/AbstractRowGateway.php b/src/RowGateway/AbstractRowGateway.php index 89b2068fff..746252bf8b 100644 --- a/src/RowGateway/AbstractRowGateway.php +++ b/src/RowGateway/AbstractRowGateway.php @@ -1,12 +1,12 @@ 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.'); } @@ -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(); @@ -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 @@ -192,12 +164,7 @@ public function save() return $rowsAffected; } - /** - * Delete - * - * @return int - */ - public function delete() + public function delete() : int { $this->initialize(); @@ -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; @@ -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); } @@ -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; @@ -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) { diff --git a/src/RowGateway/Exception/ExceptionInterface.php b/src/RowGateway/Exception/ExceptionInterface.php index b3d11b4a67..7b912d3ba5 100644 --- a/src/RowGateway/Exception/ExceptionInterface.php +++ b/src/RowGateway/Exception/ExceptionInterface.php @@ -1,12 +1,12 @@ rowGateway = $rowGateway; } - /** - * @throws \Zend\Db\RowGateway\Exception\RuntimeException - */ public function initialize() { throw new Exception\RuntimeException('This method is not intended to be called on this object.'); } - /** - * @return array - */ - public function getMagicMethodSpecifications() + public function getMagicMethodSpecifications() : array { return []; } diff --git a/src/RowGateway/Feature/FeatureSet.php b/src/RowGateway/Feature/FeatureSet.php index f3c36a07c3..0c8d413288 100644 --- a/src/RowGateway/Feature/FeatureSet.php +++ b/src/RowGateway/Feature/FeatureSet.php @@ -1,50 +1,35 @@ addFeatures($features); - } + $this->addFeatures($features); } - /** - * @param AbstractRowGateway $rowGateway - * @return self Provides a fluent interface - */ - public function setRowGateway(AbstractRowGateway $rowGateway) + public function setRowGateway(AbstractRowGateway $rowGateway) : self { $this->rowGateway = $rowGateway; foreach ($this->features as $feature) { @@ -53,6 +38,10 @@ public function setRowGateway(AbstractRowGateway $rowGateway) return $this; } + /** + * @param mixed $featureClassName + * @return bool|AbstractFeature + */ public function getFeatureByClassName($featureClassName) { $feature = false; @@ -65,11 +54,7 @@ public function getFeatureByClassName($featureClassName) return $feature; } - /** - * @param array $features - * @return self Provides a fluent interface - */ - public function addFeatures(array $features) + public function addFeatures(array $features) : self { foreach ($features as $feature) { $this->addFeature($feature); @@ -77,18 +62,14 @@ public function addFeatures(array $features) return $this; } - /** - * @param AbstractFeature $feature - * @return self Provides a fluent interface - */ - public function addFeature(AbstractFeature $feature) + public function addFeature(AbstractFeature $feature) : self { $this->features[] = $feature; $feature->setRowGateway($feature); return $this; } - public function apply($method, $args) + public function apply(string $method, array $args) : void { foreach ($this->features as $feature) { if (method_exists($feature, $method)) { @@ -100,62 +81,30 @@ public function apply($method, $args) } } - /** - * @param string $property - * @return bool - */ - public function canCallMagicGet($property) + public function canCallMagicGet(string $property) : bool { return false; } - /** - * @param string $property - * @return mixed - */ - public function callMagicGet($property) + public function callMagicGet(string $property) : void { - $return = null; - return $return; } - /** - * @param string $property - * @return bool - */ - public function canCallMagicSet($property) + public function canCallMagicSet(string $property) : bool { return false; } - /** - * @param $property - * @param $value - * @return mixed - */ - public function callMagicSet($property, $value) + public function callMagicSet(string $property, $value) : void { - $return = null; - return $return; } - /** - * @param string $method - * @return bool - */ - public function canCallMagicCall($method) + public function canCallMagicCall(string $method) : bool { return false; } - /** - * @param string $method - * @param array $arguments - * @return mixed - */ - public function callMagicCall($method, $arguments) + public function callMagicCall(string $method, array $arguments) : void { - $return = null; - return $return; } } diff --git a/src/RowGateway/RowGateway.php b/src/RowGateway/RowGateway.php index 2248538eab..a014e6d790 100644 --- a/src/RowGateway/RowGateway.php +++ b/src/RowGateway/RowGateway.php @@ -1,12 +1,12 @@ primaryKeyColumn = empty($primaryKeyColumn) ? null : (array) $primaryKeyColumn; + $this->primaryKeyColumn = $primaryKeyColumn === '' ? [] : (array) $primaryKeyColumn; - // set table $this->table = $table; - // set Sql object if ($adapterOrSql instanceof Sql) { $this->sql = $adapterOrSql; } elseif ($adapterOrSql instanceof Adapter) { diff --git a/src/RowGateway/RowGatewayInterface.php b/src/RowGateway/RowGatewayInterface.php index b623c4ed72..c6be38d2de 100644 --- a/src/RowGateway/RowGatewayInterface.php +++ b/src/RowGateway/RowGatewayInterface.php @@ -1,12 +1,12 @@