Skip to content

Commit

Permalink
implement the interface ArrayableInterface for BaseActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
niqingyang committed Dec 10, 2023
1 parent 0af32f1 commit c84a97d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 50 deletions.
26 changes: 2 additions & 24 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, string|Closure>
*/
public function fields(): array;

/**
* Filters array condition before it's assigned to a Query filter.
*
Expand Down Expand Up @@ -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;
}
27 changes: 1 addition & 26 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @template-implements ArrayAccess<int, mixed>
* @template-implements IteratorAggregate<int>
*/
abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface
abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess
{
use ArrayableTrait;
use BaseActiveRecordTrait;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c84a97d

Please sign in to comment.