Skip to content

Commit

Permalink
Create ArrayIteratorTrait, move IteratorAggregate implementation to A…
Browse files Browse the repository at this point in the history
…ctiveRecord (#312)
  • Loading branch information
Tigrov authored May 17, 2024
1 parent f4966cc commit dd3b2fa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
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);
}
}

0 comments on commit dd3b2fa

Please sign in to comment.