Skip to content

Commit

Permalink
use LimitIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 17, 2022
1 parent 8af65a9 commit 8e96c32
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions src/Persistence/Array_/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ public function filter(Model\Scope\AbstractScope $condition)
/**
* Calculates SUM|AVG|MIN|MAX aggregate values for $field.
*
* @param string $fx
* @param string $field
* @param bool $coalesce
*
* @return $this
*/
public function aggregate($fx, $field, $coalesce = false)
public function aggregate(string $fx, string $field, bool $coalesce = false)
{
$result = 0;
$column = array_column($this->getRows(), $field);
Expand Down Expand Up @@ -100,15 +96,10 @@ public function aggregate($fx, $field, $coalesce = false)

/**
* Checks if $row matches $condition.
*
* @return bool
*/
protected function match(array $row, Model\Scope\AbstractScope $condition)
protected function match(array $row, Model\Scope\AbstractScope $condition): bool
{
$match = false;

// simple condition
if ($condition instanceof Model\Scope\Condition) {
if ($condition instanceof Model\Scope\Condition) { // simple condition
$args = $condition->toQueryArguments();

$field = $args[0];
Expand All @@ -126,11 +117,8 @@ protected function match(array $row, Model\Scope\AbstractScope $condition)
->addMoreInfo('condition', $condition);
}

$match = $this->evaluateIf($row[$field->short_name] ?? null, $operator, $value);
}

// nested conditions
if ($condition instanceof Model\Scope) {
return $this->evaluateIf($row[$field->short_name] ?? null, $operator, $value);
} elseif ($condition instanceof Model\Scope) { // nested conditions
$matches = [];

foreach ($condition->getNestedConditions() as $nestedCondition) {
Expand All @@ -143,10 +131,11 @@ protected function match(array $row, Model\Scope\AbstractScope $condition)
}

// any matches && all matches the same (if all required)
$match = array_filter($matches) && ($condition->isAnd() ? count(array_unique($matches)) === 1 : true);
return array_filter($matches) && ($condition->isAnd() ? count(array_unique($matches)) === 1 : true);
}

return $match;
throw (new Exception('Unexpected condition type'))
->addMoreInfo('class', get_class($condition));
}

/**
Expand All @@ -160,7 +149,8 @@ protected function evaluateIf($v1, string $operator, $v2): bool
}

if ($v2 instanceof \Traversable) {
throw new \Exception('Unexpected v2 type');
throw (new Exception('Unexpected v2 type'))
->addMoreInfo('class', get_class($v2));
}

switch (strtoupper($operator)) {
Expand Down Expand Up @@ -233,11 +223,9 @@ protected function evaluateIf($v1, string $operator, $v2): bool
/**
* Applies sorting on Iterator.
*
* @param array $fields
*
* @return $this
*/
public function order($fields)
public function order(array $fields)
{
$data = $this->getRows();

Expand Down Expand Up @@ -265,10 +253,7 @@ public function order($fields)
*/
public function limit(int $limit = null, int $offset = 0)
{
$data = array_slice($this->getRows(), $offset, $limit, true);

// put data back in generator
$this->generator = new \ArrayIterator($data);
$this->generator = new \LimitIterator($this->generator, $offset, $limit ?? -1);

return $this;
}
Expand Down

0 comments on commit 8e96c32

Please sign in to comment.