From 12c33003f48472bf65fdc0da8ede4d7fe34e1630 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sat, 9 Dec 2023 16:50:31 +0800 Subject: [PATCH 01/24] implement the interface ArrayableInterface for BaseActiveRecord --- composer.json | 1 + src/ActiveRecordInterface.php | 12 ++++++- src/BaseActiveRecord.php | 33 +++++++++++++------ tests/ActiveRecordTest.php | 22 +++++++++++++ tests/Driver/Oracle/ActiveRecordTest.php | 22 +++++++++++++ tests/Driver/Pgsql/ActiveRecordTest.php | 22 +++++++++++++ .../ActiveRecord/CustomerForArrayable.php | 31 +++++++++++++++++ 7 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 tests/Stubs/ActiveRecord/CustomerForArrayable.php diff --git a/composer.json b/composer.json index 1d26f0fdf..9f43232ff 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "require": { "php": "^8.0", "ext-json": "*", + "yiisoft/arrays": "^3.0", "yiisoft/db": "^1.1", "yiisoft/factory": "^1.0" }, diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 2177dd362..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -502,6 +502,16 @@ public function populateRecord(array|object $row): void; /** * Serializes the active record into its array implementation with attribute name as a key, and it values as value. + * + * @param array $fields the fields that the output array should contain. Fields not specified + * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified + * in {@see fields()} will be returned. + * @param array $expand the additional fields that the output array should contain. + * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields + * will be returned. + * @param bool $recursive Whether to recursively return array representation of embedded objects. + * + * @return array The array representation of the object. */ - public function toArray(): array; + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index bdbdd4d1d..fc40a9c76 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,6 +9,9 @@ use IteratorAggregate; use ReflectionException; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; +use Yiisoft\Arrays\ArrayableTrait; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -40,8 +43,9 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface { + use ArrayableTrait; use BaseActiveRecordTrait; private array $attributes = []; @@ -1266,19 +1270,28 @@ public function getTableName(): string return $this->tableName; } - public function toArray(): array + /** + * @inheritDoc + */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; - foreach ($this->fields() as $key => $value) { - if ($value instanceof Closure) { - /** @var mixed */ - $data[$key] = $value($this); - } else { - /** @var mixed */ - $data[$value] = $this[$value]; + foreach ($this->resolveFields($fields, $expand) as $field => $definition) { + $attribute = $definition instanceof Closure ? $definition($this, $field) : $definition; + + if ($recursive) { + $nestedFields = $this->extractFieldsFor($fields, $field); + $nestedExpand = $this->extractFieldsFor($expand, $field); + if ($attribute instanceof ArrayableInterface) { + $attribute = $attribute->toArray($nestedFields, $nestedExpand); + } elseif (is_array($attribute) && ($nestedExpand || $nestedFields)) { + $attribute = $this->filterAndExpand($attribute, $nestedFields, $nestedExpand); + } } + $data[$field] = $attribute; } - return $data; + + return $recursive ? ArrayHelper::toArray($data) : $data; } } diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 4d054d29e..6fb1bfc7a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -10,6 +10,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item; @@ -20,6 +21,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItemWithNullFK; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\Assert; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -763,4 +765,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index dbad51704..14f6f24d4 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -7,8 +7,10 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Arrays\ArrayHelper; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { @@ -166,4 +168,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 3a91cbd76..f6357ad84 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -12,9 +12,11 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\BoolAR; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -381,4 +383,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php new file mode 100644 index 000000000..70bd1662b --- /dev/null +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -0,0 +1,31 @@ +status == 1 ? 'active' : 'inactive'; + + return $data; + } +} \ No newline at end of file From be499a5238f5a30d3d417f13286e241540ee49fe Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 00:34:21 +0800 Subject: [PATCH 02/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index fc40a9c76..21a0308c4 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1278,7 +1278,7 @@ public function toArray(array $fields = [], array $expand = [], bool $recursive $data = []; foreach ($this->resolveFields($fields, $expand) as $field => $definition) { - $attribute = $definition instanceof Closure ? $definition($this, $field) : $definition; + $attribute = $definition instanceof Closure ? $definition($this, $field) : $this[$definition]; if ($recursive) { $nestedFields = $this->extractFieldsFor($fields, $field); From 3988dcc18a66291431af0ee6646b382dc5023ef7 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 00:41:26 +0800 Subject: [PATCH 03/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 1 + tests/Driver/Pgsql/ActiveRecordTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 14f6f24d4..af0094387 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -183,6 +183,7 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', + 'bool_status' => '1', 'profile_id' => 1, ], ArrayHelper::toArray($customer), diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index f6357ad84..e357fcf8c 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -398,6 +398,7 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', + 'bool_status' => true, 'profile_id' => 1, ], ArrayHelper::toArray($customer), From 424f84162b98254e3c31ec09fa5ec90f45513ee6 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:36:32 +0800 Subject: [PATCH 04/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecordTrait.php | 6 +-- tests/ActiveRecordTest.php | 38 +++++++++++++++++- tests/Driver/Oracle/ActiveRecordTest.php | 39 +++++++++++++++++-- tests/Driver/Pgsql/ActiveRecordTest.php | 39 +++++++++++++++++-- .../ActiveRecord/CustomerForArrayable.php | 27 ++++++++++++- 5 files changed, 137 insertions(+), 12 deletions(-) diff --git a/src/BaseActiveRecordTrait.php b/src/BaseActiveRecordTrait.php index c5061b1ac..50c5ac2d4 100644 --- a/src/BaseActiveRecordTrait.php +++ b/src/BaseActiveRecordTrait.php @@ -250,11 +250,11 @@ public function offsetGet(mixed $offset): mixed * It is implicitly called when you use something like `$model[$offset] = $item;`. * * @param mixed $offset the offset to set element. - * @param mixed $item the element value. + * @param mixed $value the element value. */ - public function offsetSet(mixed $offset, mixed $item): void + public function offsetSet(mixed $offset, mixed $value): void { - $this->$offset = $item; + $this->$offset = $value; } /** diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 6fb1bfc7a..de8d993bc 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -771,7 +771,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -780,9 +789,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index af0094387..e48379226 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -174,7 +174,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -183,10 +192,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'bool_status' => '1', - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index e357fcf8c..620d71d36 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -389,7 +389,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -398,10 +407,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'bool_status' => true, - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index 70bd1662b..7a4bff910 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -1,5 +1,7 @@ item = $item; + } + + public function setItems(CustomerForArrayable ...$items) + { + $this->items = $items; + } + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = parent::toArray($fields, $expand, $recursive); @@ -28,4 +53,4 @@ public function toArray(array $fields = [], array $expand = [], bool $recursive return $data; } -} \ No newline at end of file +} From b64dde562669a73c0d649ef2050c05ba4d8f4eaa Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:42:11 +0800 Subject: [PATCH 05/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Pgsql/ActiveRecordTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 620d71d36..6f6efd6cd 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -403,8 +403,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -416,8 +416,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] @@ -429,11 +429,11 @@ public function testToArrayForArrayable(): void 'address', 'status', 'item.id', - 'item.name', 'item.email', + 'item.name', 'items.0.id', - 'items.0.name', 'items.0.email', + 'items.0.name', ]), ); } From 0f680fe018e9a48d8a667f3ad7f67999a7efb7f8 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:44:00 +0800 Subject: [PATCH 06/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 4 ++-- tests/Driver/Oracle/ActiveRecordTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index de8d993bc..d9c4283b0 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -785,8 +785,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -798,8 +798,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index e48379226..078b3cf1e 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -188,8 +188,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -201,8 +201,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] From 594a2225af0d16a830549513f11e6f4b88c53fd9 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:45:52 +0800 Subject: [PATCH 07/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 3 +-- tests/Driver/Pgsql/ActiveRecordTest.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index d9c4283b0..779935782 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -21,7 +21,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItemWithNullFK; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\Assert; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -802,7 +801,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 6f6efd6cd..44a7999e6 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -16,7 +16,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -420,7 +419,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', From 08711a6f9a094f9d95f367fc0132aa2128bfbaae Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:47:11 +0800 Subject: [PATCH 08/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 078b3cf1e..250212575 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -10,7 +10,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; -use Yiisoft\Arrays\ArrayHelper; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { @@ -205,7 +204,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', From cb9db0e0f30b13c3f2a1ad2e29d0fe25c1d722fc Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:48:12 +0800 Subject: [PATCH 09/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Stubs/ActiveRecord/CustomerForArrayable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index 7a4bff910..e54d1ce85 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -36,11 +36,11 @@ public function fields(): array return $fields; } - public function setItem(CustomerForArrayable $item) { + public function setItem(self $item) { $this->item = $item; } - public function setItems(CustomerForArrayable ...$items) + public function setItems(self ...$items) { $this->items = $items; } From 044755d0bf8a1d325f77a696ce94e6cabcb025aa Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:49:07 +0800 Subject: [PATCH 10/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Stubs/ActiveRecord/CustomerForArrayable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index e54d1ce85..591cd79cf 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -36,7 +36,8 @@ public function fields(): array return $fields; } - public function setItem(self $item) { + public function setItem(self $item) + { $this->item = $item; } From fa7c4cb1144c5d2190ddeb37d4c773bc4921312b Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:52:21 +0800 Subject: [PATCH 11/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 6 +++--- tests/Driver/Oracle/ActiveRecordTest.php | 6 +++--- tests/Driver/Pgsql/ActiveRecordTest.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 779935782..7d232d6cf 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -784,21 +784,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 250212575..d91b25b02 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -187,21 +187,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 44a7999e6..51a25c23e 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -402,21 +402,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], From 0af5333ffee098f07489b5cb0ed87c13f38314a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Sun, 10 Dec 2023 12:44:30 +0800 Subject: [PATCH 12/24] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 21a0308c4..7ec6c7f6f 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1273,7 +1273,6 @@ public function getTableName(): string /** * @inheritDoc */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; From baf1889c004dd6a681530044eaaa55ea5d47496a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Sun, 10 Dec 2023 12:44:45 +0800 Subject: [PATCH 13/24] Update src/ActiveRecordInterface.php Co-authored-by: Sergei Tigrov --- src/ActiveRecordInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..6ec025b45 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -513,5 +513,4 @@ public function populateRecord(array|object $row): void; * * @return array The array representation of the object. */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } From b479a120a6c9e881ebcc99cf2785e2a8b1422a58 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:33:59 +0800 Subject: [PATCH 14/24] Revert "Update src/ActiveRecordInterface.php" This reverts commit baf1889c004dd6a681530044eaaa55ea5d47496a. --- src/ActiveRecordInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 6ec025b45..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -513,4 +513,5 @@ public function populateRecord(array|object $row): void; * * @return array The array representation of the object. */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } From 0af32f14a4d16a65e0ce17278314cbf3d89055ef Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:34:09 +0800 Subject: [PATCH 15/24] Revert "Update src/BaseActiveRecord.php" This reverts commit 0af5333ffee098f07489b5cb0ed87c13f38314a5. --- src/BaseActiveRecord.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 7ec6c7f6f..21a0308c4 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1273,6 +1273,7 @@ public function getTableName(): string /** * @inheritDoc */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; From c84a97d391b12f5841cd55e0c82953dec9c5d237 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:39:55 +0800 Subject: [PATCH 16/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 26 ++------------------------ src/BaseActiveRecord.php | 27 +-------------------------- 2 files changed, 3 insertions(+), 50 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..76f96cdaa 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -6,13 +6,14 @@ use Closure; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; -interface ActiveRecordInterface +interface ActiveRecordInterface extends ArrayableInterface { /** * Returns the list of all attribute names of the model. @@ -86,14 +87,6 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; - /** - * @return array The default implementation returns the names of the columns whose values have been populated into - * this record. - * - * @psalm-return array - */ - public function fields(): array; - /** * Filters array condition before it's assigned to a Query filter. * @@ -499,19 +492,4 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; - - /** - * Serializes the active record into its array implementation with attribute name as a key, and it values as value. - * - * @param array $fields the fields that the output array should contain. Fields not specified - * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified - * in {@see fields()} will be returned. - * @param array $expand the additional fields that the output array should contain. - * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields - * will be returned. - * @param bool $recursive Whether to recursively return array representation of embedded objects. - * - * @return array The array representation of the object. - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 21a0308c4..de49ac475 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -43,7 +43,7 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess { use ArrayableTrait; use BaseActiveRecordTrait; @@ -1269,29 +1269,4 @@ public function getTableName(): string return $this->tableName; } - - /** - * @inheritDoc - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array - { - $data = []; - - foreach ($this->resolveFields($fields, $expand) as $field => $definition) { - $attribute = $definition instanceof Closure ? $definition($this, $field) : $this[$definition]; - - if ($recursive) { - $nestedFields = $this->extractFieldsFor($fields, $field); - $nestedExpand = $this->extractFieldsFor($expand, $field); - if ($attribute instanceof ArrayableInterface) { - $attribute = $attribute->toArray($nestedFields, $nestedExpand); - } elseif (is_array($attribute) && ($nestedExpand || $nestedFields)) { - $attribute = $this->filterAndExpand($attribute, $nestedFields, $nestedExpand); - } - } - $data[$field] = $attribute; - } - - return $recursive ? ArrayHelper::toArray($data) : $data; - } } From 2b82b2bc31b247fbb2400f12180b20a33bb84e59 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:41:34 +0800 Subject: [PATCH 17/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 1 - src/BaseActiveRecord.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 76f96cdaa..6752b1992 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,7 +4,6 @@ namespace Yiisoft\ActiveRecord; -use Closure; use Throwable; use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index de49ac475..448e32706 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,9 +9,7 @@ use IteratorAggregate; use ReflectionException; use Throwable; -use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Arrays\ArrayableTrait; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; From 32cc7b4f9056f1df44b57f41b0ddae903d52d348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Mon, 11 Dec 2023 11:48:04 +0800 Subject: [PATCH 18/24] Update tests/Driver/Oracle/ActiveRecordTest.php Co-authored-by: Sergei Tigrov --- tests/Driver/Oracle/ActiveRecordTest.php | 54 ------------------------ 1 file changed, 54 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index d91b25b02..ac4c9f407 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -167,58 +167,4 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } - - public function testToArrayForArrayable(): void - { - $this->checkFixture($this->db, 'customer', true); - - $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); - - /** @var CustomerForArrayable $customer */ - $customer = $customerQuery->findOne(1); - /** @var CustomerForArrayable $customer2 */ - $customer2 = $customerQuery->findOne(2); - /** @var CustomerForArrayable $customer3 */ - $customer3 = $customerQuery->findOne(3); - - $customer->setItem($customer2); - $customer->setItems($customer3); - - $this->assertSame( - [ - 'id' => 1, - 'email' => 'user1@example.com', - 'name' => 'user1', - 'address' => 'address1', - 'status' => 'active', - 'item' => [ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'status' => 'active', - ], - 'items' => [ - [ - 'id' => 3, - 'email' => 'user3@example.com', - 'name' => 'user3', - 'status' => 'inactive', - ], - ], - ], - $customer->toArray([ - 'id', - 'name', - 'email', - 'address', - 'status', - 'item.id', - 'item.name', - 'item.email', - 'items.0.id', - 'items.0.name', - 'items.0.email', - ]), - ); - } } From 85832694b8fb474253e140d0c4e88404e2195e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Mon, 11 Dec 2023 11:48:12 +0800 Subject: [PATCH 19/24] Update tests/Driver/Pgsql/ActiveRecordTest.php Co-authored-by: Sergei Tigrov --- tests/Driver/Pgsql/ActiveRecordTest.php | 54 ------------------------- 1 file changed, 54 deletions(-) diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 51a25c23e..de5c2dc92 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -382,58 +382,4 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } - - public function testToArrayForArrayable(): void - { - $this->checkFixture($this->db, 'customer', true); - - $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); - - /** @var CustomerForArrayable $customer */ - $customer = $customerQuery->findOne(1); - /** @var CustomerForArrayable $customer2 */ - $customer2 = $customerQuery->findOne(2); - /** @var CustomerForArrayable $customer3 */ - $customer3 = $customerQuery->findOne(3); - - $customer->setItem($customer2); - $customer->setItems($customer3); - - $this->assertSame( - [ - 'id' => 1, - 'email' => 'user1@example.com', - 'name' => 'user1', - 'address' => 'address1', - 'status' => 'active', - 'item' => [ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'status' => 'active', - ], - 'items' => [ - [ - 'id' => 3, - 'email' => 'user3@example.com', - 'name' => 'user3', - 'status' => 'inactive', - ], - ], - ], - $customer->toArray([ - 'id', - 'name', - 'email', - 'address', - 'status', - 'item.id', - 'item.email', - 'item.name', - 'items.0.id', - 'items.0.email', - 'items.0.name', - ]), - ); - } } From e299dfa467f34139f2f9afe7099bea251c672de2 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 11:50:48 +0800 Subject: [PATCH 20/24] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 1 - tests/Driver/Pgsql/ActiveRecordTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index ac4c9f407..dbad51704 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -7,7 +7,6 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; -use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index de5c2dc92..3a91cbd76 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -12,7 +12,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\BoolAR; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; -use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; From 1f8610452a1169deab650668b4dbfc24845f2246 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:20:16 +0800 Subject: [PATCH 21/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 26 ++++++++++++++++++++++++-- src/BaseActiveRecord.php | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 6752b1992..1049a6c5c 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -5,14 +5,13 @@ namespace Yiisoft\ActiveRecord; use Throwable; -use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; -interface ActiveRecordInterface extends ArrayableInterface +interface ActiveRecordInterface { /** * Returns the list of all attribute names of the model. @@ -86,6 +85,14 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; + /** + * @return array The default implementation returns the names of the columns whose values have been populated into + * this record. + * + * @psalm-return array + */ + public function fields(): array; + /** * Filters array condition before it's assigned to a Query filter. * @@ -491,4 +498,19 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; + + /** + * Serializes the active record into its array implementation with attribute name as a key, and it values as value. + * + * @param array $fields the fields that the output array should contain. Fields not specified + * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified + * in {@see fields()} will be returned. + * @param array $expand the additional fields that the output array should contain. + * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields + * will be returned. + * @param bool $recursive Whether to recursively return array representation of embedded objects. + * + * @return array The array representation of the object. + */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 448e32706..8180e271a 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,6 +9,7 @@ use IteratorAggregate; use ReflectionException; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Arrays\ArrayableTrait; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; @@ -41,7 +42,7 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface { use ArrayableTrait; use BaseActiveRecordTrait; From 43f615b5a7c4d754007a99edd60d6bbe7213a7d0 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:24:11 +0800 Subject: [PATCH 22/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 1049a6c5c..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\ActiveRecord; +use Closure; use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; From 418be7de6c995828a20b85069bc874cfeb90c316 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:26:50 +0800 Subject: [PATCH 23/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 8180e271a..d6cb497e3 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -118,7 +118,7 @@ public function extraFields(): array } /** - * @psalm-suppress MixedReturnTypeCoercion + * @psalm-return array */ public function fields(): array { From 564e0191e5f9d34969f0b18325636c2f211a0499 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 23:09:06 +0800 Subject: [PATCH 24/24] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..f8a9952b8 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,7 +4,6 @@ namespace Yiisoft\ActiveRecord; -use Closure; use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -86,14 +85,6 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; - /** - * @return array The default implementation returns the names of the columns whose values have been populated into - * this record. - * - * @psalm-return array - */ - public function fields(): array; - /** * Filters array condition before it's assigned to a Query filter. * @@ -499,19 +490,4 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; - - /** - * Serializes the active record into its array implementation with attribute name as a key, and it values as value. - * - * @param array $fields the fields that the output array should contain. Fields not specified - * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified - * in {@see fields()} will be returned. - * @param array $expand the additional fields that the output array should contain. - * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields - * will be returned. - * @param bool $recursive Whether to recursively return array representation of embedded objects. - * - * @return array The array representation of the object. - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; }