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 ArrayIteratorTrait, move IteratorAggregate implementation to ActiveRecord #312

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

namespace Yiisoft\ActiveRecord;

use IteratorAggregate;
use Throwable;
use Yiisoft\ActiveRecord\Trait\ArrayIteratorTrait;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -80,9 +82,13 @@
*
* @method ActiveQuery hasMany($class, array $link) {@see BaseActiveRecord::hasMany()} for more info.
* @method ActiveQuery hasOne($class, array $link) {@see BaseActiveRecord::hasOne()} for more info.
*
* @template-implements IteratorAggregate<string, mixed>
*/
class ActiveRecord extends BaseActiveRecord
class ActiveRecord extends BaseActiveRecord implements IteratorAggregate
{
use ArrayIteratorTrait;

/**
* The insert operation. This is mainly used when overriding {@see transactions()} to specify which operations are
* transactional.
Expand Down
4 changes: 1 addition & 3 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ArrayAccess;
use Closure;
use IteratorAggregate;
use ReflectionException;
use Throwable;
use Yiisoft\Arrays\ArrayableInterface;
Expand Down Expand Up @@ -46,9 +45,8 @@
* See {@see ActiveRecord} for a concrete implementation.
*
* @template-implements ArrayAccess<int, mixed>
* @template-implements IteratorAggregate<int>
*/
abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface
abstract class BaseActiveRecord implements ActiveRecordInterface, ArrayAccess, ArrayableInterface
{
use ArrayableTrait;
use BaseActiveRecordTrait;
Expand Down
16 changes: 0 additions & 16 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Yiisoft\ActiveRecord;

use ArrayAccess;
use ArrayIterator;
use Error;
use Exception;
use IteratorAggregate;
use ReflectionException;
use ReflectionMethod;
use Throwable;
Expand Down Expand Up @@ -210,20 +208,6 @@ public function __set(string $name, mixed $value): void
throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name);
}

/**
* Returns an iterator for traversing the attributes in the ActiveRecord.
*
* This method is required by the interface {@see IteratorAggregate}.
*
* @return ArrayIterator an iterator for traversing the items in the list.
*/
public function getIterator(): ArrayIterator
{
$attributes = $this->getAttributes();

return new ArrayIterator($attributes);
}

/**
* Returns whether there is an element at the specified offset.
*
Expand Down
31 changes: 31 additions & 0 deletions src/Trait/ArrayIteratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Trait;

use ArrayIterator;
use IteratorAggregate;

/**
* Trait to implement {@see IteratorAggregate} interface for ActiveRecord.
*
* @method array getAttributes(array|null $names = null, array $except = [])
* @see ActiveRecordInterface::getAttributes() for more info.
*/
trait ArrayIteratorTrait
{
/**
* Returns an iterator for traversing the attributes in the ActiveRecord.
*
* This method is required by the interface {@see IteratorAggregate}.
*
* @return ArrayIterator an iterator for traversing the items in the list.
*/
public function getIterator(): ArrayIterator
{
$attributes = $this->getAttributes();

return new ArrayIterator($attributes);
}
}