Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add EntityInterface #8325

Closed
Closed
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
4 changes: 2 additions & 2 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Database\Query;
use CodeIgniter\DataConverter\DataConverter;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\Pager;
Expand Down Expand Up @@ -1796,7 +1796,7 @@ protected function timeToString(array $properties): array
protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
{
// Entity::toRawArray() returns array.
if (method_exists($object, 'toRawArray')) {
if ($object instanceof EntityInterface) {
$properties = $object->toRawArray($onlyChanged, $recursive);
} else {
$mirror = new ReflectionClass($object);
Expand Down
8 changes: 4 additions & 4 deletions system/Database/BaseResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace CodeIgniter\Database;

use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use stdClass;

/**
Expand Down Expand Up @@ -159,7 +159,7 @@ public function getCustomResultObject(string $className)
$this->customResultObject[$className] = [];

while ($row = $this->fetchObject($className)) {
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
if (! is_subclass_of($row, EntityInterface::class) && method_exists($row, 'syncOriginal')) {
$row->syncOriginal();
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public function getResultObject(): array
}

while ($row = $this->fetchObject()) {
if (! is_subclass_of($row, Entity::class) && method_exists($row, 'syncOriginal')) {
if (! is_subclass_of($row, EntityInterface::class) && method_exists($row, 'syncOriginal')) {
$row->syncOriginal();
}

Expand Down Expand Up @@ -539,7 +539,7 @@ abstract protected function fetchAssoc();
*
* Overridden by child classes.
*
* @return Entity|false|object|stdClass
* @return EntityInterface|false|object|stdClass
*/
abstract protected function fetchObject(string $className = 'stdClass');
}
6 changes: 3 additions & 3 deletions system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace CodeIgniter\Database\MySQLi;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use mysqli;
use mysqli_result;
use stdClass;
Expand Down Expand Up @@ -145,11 +145,11 @@ protected function fetchAssoc()
*
* Overridden by child classes.
*
* @return Entity|false|object|stdClass
* @return EntityInterface|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
colethorsen marked this conversation as resolved.
Show resolved Hide resolved
{
if (is_subclass_of($className, Entity::class)) {
if (is_subclass_of($className, EntityInterface::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
}

Expand Down
6 changes: 3 additions & 3 deletions system/Database/OCI8/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace CodeIgniter\Database\OCI8;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use stdClass;

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function fetchAssoc()
*
* Overridden by child classes.
*
* @return Entity|false|object|stdClass
* @return EntityInterface|false|object|stdClass
*/
protected function fetchObject(string $className = 'stdClass')
{
Expand All @@ -104,7 +104,7 @@ protected function fetchObject(string $className = 'stdClass')
if ($className === 'stdClass' || ! $row) {
colethorsen marked this conversation as resolved.
Show resolved Hide resolved
return $row;
}
if (is_subclass_of($className, Entity::class)) {
if (is_subclass_of($className, EntityInterface::class)) {
return (new $className())->injectRawData((array) $row);
}

Expand Down
6 changes: 3 additions & 3 deletions system/Database/Postgre/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace CodeIgniter\Database\Postgre;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use PgSql\Connection as PgSqlConnection;
use PgSql\Result as PgSqlResult;
use stdClass;
Expand Down Expand Up @@ -111,11 +111,11 @@ protected function fetchAssoc()
*
* Overridden by child classes.
*
* @return Entity|false|object|stdClass
* @return EntityInterface|false|object|stdClass
*/
colethorsen marked this conversation as resolved.
Show resolved Hide resolved
protected function fetchObject(string $className = 'stdClass')
{
if (is_subclass_of($className, Entity::class)) {
if (is_subclass_of($className, EntityInterface::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
}

Expand Down
6 changes: 3 additions & 3 deletions system/Database/SQLSRV/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace CodeIgniter\Database\SQLSRV;

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use stdClass;

/**
Expand Down Expand Up @@ -151,11 +151,11 @@ protected function fetchAssoc()
/**
* Returns the result set as an object.
*
* @return Entity|false|object|stdClass
* @return EntityInterface|false|object|stdClass
*/
colethorsen marked this conversation as resolved.
Show resolved Hide resolved
protected function fetchObject(string $className = 'stdClass')
{
if (is_subclass_of($className, Entity::class)) {
if (is_subclass_of($className, EntityInterface::class)) {
return empty($data = $this->fetchAssoc()) ? false : (new $className())->injectRawData($data);
}

Expand Down
4 changes: 2 additions & 2 deletions system/Database/SQLite3/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Closure;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Entity\EntityInterface;
use SQLite3;
use SQLite3Result;
use stdClass;
Expand Down Expand Up @@ -143,7 +143,7 @@ protected function fetchObject(string $className = 'stdClass')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update @return.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/system/Database/SQLite3/Result.php
+++ b/system/Database/SQLite3/Result.php
@@ -128,7 +128,7 @@ class Result extends BaseResult
      *
      * Overridden by child classes.
      *
-     * @return Entity|false|object|stdClass
+     * @return EntityInterface|false|object|stdClass
      */
     protected function fetchObject(string $className = 'stdClass')
     {

$classObj = new $className();

if (is_subclass_of($className, Entity::class)) {
if (is_subclass_of($className, EntityInterface::class)) {
return $classObj->injectRawData($row);
}

Expand Down
2 changes: 1 addition & 1 deletion system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @see \CodeIgniter\Entity\EntityTest
*/
class Entity implements JsonSerializable
class Entity implements EntityInterface, JsonSerializable
{
/**
* Maps names used in sets and gets against unique
Expand Down
41 changes: 41 additions & 0 deletions system/Entity/EntityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Entity;

/**
* Entity encapsulation, for use with CodeIgniter\Model
*
* @see \CodeIgniter\Entity\EntityTest
*/
interface EntityInterface
{
/**
* Returns the raw values of the current attributes.
*
* @param bool $onlyChanged If true, only return values that have changed since object creation
* @param bool $recursive If true, inner entities will be cast as array as well.
*
* @return array<string, mixed>
*/
public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array;
kenjis marked this conversation as resolved.
Show resolved Hide resolved

/**
* Set raw data array without any mutations
*
* @param array<string, mixed> $data
*
* @return $this
*/
public function injectRawData(array $data);
kenjis marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Forge
Others
------

- **Entity:**
- Added ``CodeIgniter\Entity\EntityInterface`` for better extensibility.
- Now ``Entity`` implements ``EntityInterface``.
- Now ``BaseModel`` and ``Result`` classes use ``EntityInterface`` instead of ``Entity``.

Model
=====

Expand Down