From 8e96c327dc5d68d07ab4023e6c3a45aaae07979f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 17 Jan 2022 12:13:52 +0100 Subject: [PATCH] use LimitIterator --- src/Persistence/Array_/Action.php | 39 ++++++++++--------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/Persistence/Array_/Action.php b/src/Persistence/Array_/Action.php index 9e432fd53..b181a84cf 100644 --- a/src/Persistence/Array_/Action.php +++ b/src/Persistence/Array_/Action.php @@ -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); @@ -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]; @@ -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) { @@ -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)); } /** @@ -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)) { @@ -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(); @@ -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; }