Implemented \Traversable in ArrayExpression (#15733)

* Implemented \Traversable in ArrayExpression

* Fix punctuation [skip ci]
This commit is contained in:
Dmitry Naumenko
2018-02-23 22:50:06 +02:00
committed by GitHub
parent 77ad6bc008
commit 6c8b87f3e2
3 changed files with 35 additions and 8 deletions

View File

@@ -7,6 +7,9 @@
namespace yii\db;
use Traversable;
use yii\base\InvalidConfigException;
/**
* Class ArrayExpression represents an array SQL expression.
*
@@ -22,7 +25,7 @@ namespace yii\db;
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.14
*/
class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable
class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* @var null|string the type of the array elements. Defaults to `null` which means the type is
@@ -33,8 +36,8 @@ class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable
*/
private $type;
/**
* @var array|QueryInterface|mixed the array content. Either represented as an array of values or a [[Query]] that
* returns these values. A single value will be considered as an array containing one element.
* @var array|QueryInterface the array's content.
* In can be represented as an array of values or a [[Query]] that returns these values.
*/
private $value;
/**
@@ -167,4 +170,21 @@ class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable
{
return count($this->value);
}
/**
* Retrieve an external iterator
*
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
* @since 5.0.0
*/
public function getIterator()
{
if ($this->getValue() instanceof QueryInterface) {
throw new InvalidConfigException('The ArrayExpression class can not be iterated when the value is a QueryInterface object');
}
return new \ArrayIterator($this->getValue());
}
}