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

Create ArrayableTrait #315

Merged
merged 2 commits into from
May 19, 2024
Merged
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
5 changes: 4 additions & 1 deletion src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use IteratorAggregate;
use Throwable;
use Yiisoft\ActiveRecord\Trait\ArrayableTrait;
use Yiisoft\ActiveRecord\Trait\ArrayIteratorTrait;
use Yiisoft\Arrays\ArrayableInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -85,8 +87,9 @@
*
* @template-implements IteratorAggregate<string, mixed>
*/
class ActiveRecord extends BaseActiveRecord implements IteratorAggregate
class ActiveRecord extends BaseActiveRecord implements ArrayableInterface, IteratorAggregate
{
use ArrayableTrait;
use ArrayIteratorTrait;

/**
Expand Down
27 changes: 1 addition & 26 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Closure;
use ReflectionException;
use Throwable;
use Yiisoft\Arrays\ArrayableInterface;
use Yiisoft\Arrays\ArrayableTrait;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand All @@ -20,7 +18,6 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbStringHelper;

use function array_combine;
use function array_diff_key;
use function array_diff;
use function array_fill_keys;
Expand All @@ -46,9 +43,8 @@
*
* @template-implements ArrayAccess<int, mixed>
*/
abstract class BaseActiveRecord implements ActiveRecordInterface, ArrayAccess, ArrayableInterface
abstract class BaseActiveRecord implements ActiveRecordInterface, ArrayAccess
{
use ArrayableTrait;
use BaseActiveRecordTrait;

private array $attributes = [];
Expand Down Expand Up @@ -86,27 +82,6 @@ public function equals(ActiveRecordInterface $record): bool
return $this->getTableName() === $record->getTableName() && $this->getPrimaryKey() === $record->getPrimaryKey();
}

/**
* @return array The default implementation returns the names of the relations that have been populated into this
* record.
*/
public function extraFields(): array
{
$fields = array_keys($this->getRelatedRecords());

return array_combine($fields, $fields);
}

/**
* @psalm-return array<string, string|Closure>
*/
public function fields(): array
{
$fields = array_keys($this->attributes);

return array_combine($fields, $fields);
}

public function getAttribute(string $name): mixed
{
return $this->attributes[$name] ?? null;
Expand Down
46 changes: 46 additions & 0 deletions src/Trait/ArrayableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Trait;

use Closure;
use Yiisoft\ActiveRecord\BaseActiveRecord;

use function array_combine;
use function array_keys;

/**
* Trait to implement {@see \Yiisoft\Arrays\ArrayableTrait} interface for ActiveRecord.
*
* @method string[] attributes()
* @see BaseActiveRecord::attributes() for more info.
*
* @method array getRelatedRecords()
* @see BaseActiveRecord::getRelatedRecords() for more info.
*/
trait ArrayableTrait
{
use \Yiisoft\Arrays\ArrayableTrait;

/**
* @return array The default implementation returns the names of the relations that have been populated into this
* record.
*/
public function extraFields(): array
{
$fields = array_keys($this->getRelatedRecords());

return array_combine($fields, $fields);
}

/**
* @psalm-return array<string, string|Closure>
*/
public function fields(): array
{
$fields = $this->attributes();

return array_combine($fields, $fields);
}
}
Loading