From 9822f8c1fb4acc1b345ce80e83fcd9b596521f4c Mon Sep 17 00:00:00 2001 From: Leandro Silva Date: Fri, 7 May 2021 08:05:44 -0300 Subject: [PATCH] add php 8.0 support and minimum 7.4 --- composer.json | 17 ++-- phpcs.xml | 34 +++++++- src/BuilderInterface.php | 55 +++++++----- src/ElasticSearchBuilder.php | 120 +++++++++++++++++---------- src/Exception/MalformedException.php | 4 + src/ZendDbBuilder.php | 93 ++++++++++----------- 6 files changed, 201 insertions(+), 122 deletions(-) diff --git a/composer.json b/composer.json index a11ec66..ef60b77 100644 --- a/composer.json +++ b/composer.json @@ -11,17 +11,18 @@ } ], "require": { - "php": "^7.1", + "php": "^7.4 | ^8.0", + "ext-json": "*", "psr/http-message": "^1.0" }, "require-dev": { "laminas/laminas-db": "^2.9", - "laminas/laminas-coding-standard": "^1.0", - "squizlabs/php_codesniffer": "^2.7", - "phpstan/phpstan": "^0.9.2", - "phpunit/phpunit": "^7.1", - "laminas/laminas-diactoros": "^1.7", - "php-coveralls/php-coveralls": "^2.0" + "squizlabs/php_codesniffer": "^3.6", + "phpstan/phpstan": "^0.12.85", + "phpunit/phpunit": "^9.5", + "laminas/laminas-diactoros": "^2.0", + "php-coveralls/php-coveralls": "^2.0", + "doctrine/coding-standard": "^9.0" }, "autoload": { "psr-4": { @@ -39,7 +40,7 @@ "@phpstan", "@test" ], - "cs-check": "phpcs", + "cs-check": "phpcs -s", "cs-fix": "phpcbf", "phpstan": "phpstan analyse -l max src", "test": "phpunit --colors=always", diff --git a/phpcs.xml b/phpcs.xml index 2c12b89..3f81445 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,6 +1,36 @@ - - + + App coding standard + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 5 + src diff --git a/src/BuilderInterface.php b/src/BuilderInterface.php index 2f23d39..615e596 100644 --- a/src/BuilderInterface.php +++ b/src/BuilderInterface.php @@ -1,23 +1,25 @@ queryName = $queryName; - $this->hintName = $hintName; + $this->hintName = $hintName; } - public function fromRequest(ServerRequestInterface $request) : array + public function fromRequest(ServerRequestInterface $request): array { $queryParams = $request->getQueryParams(); - $query = json_decode($queryParams['q'] ?? '{}', true); - $hint = json_decode($queryParams['h'] ?? '{}', true); + $query = json_decode($queryParams[$this->queryName] ?? '{}', true); + $hint = json_decode($queryParams[$this->hintName] ?? '{}', true); if (! is_array($query) || ! is_array($hint)) { throw new Exception\MalformedException('Invalid query or hint'); @@ -30,7 +39,7 @@ public function fromRequest(ServerRequestInterface $request) : array return $this->fromParams($query, $hint); } - public function fromParams(array $query, array $hint = []) : array + public function fromParams(array $query, array $hint = []): array { foreach ($query as $key => $value) { $this->params = array_merge($this->params, $this->parseQuery($key, $value)); @@ -41,54 +50,64 @@ public function fromParams(array $query, array $hint = []) : array $hintParams = array_merge($hintParams, $this->parseHint($key, $value)); } - $result = [ 'body' => [ 'query' => ['constant_score' => [ 'filter' => [ 'bool' => $this->params ] ] ] ] ]; + $result = ['body' => ['query' => ['constant_score' => ['filter' => ['bool' => $this->params]]]]]; - if (!empty($hintParams)) { + if (! empty($hintParams)) { $result = array_merge($result, $hintParams); } return $result; } - private function parseQuery(string $key, $value, bool $withoutOperator = false) : array + /** + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + * + * @param mixed $value + */ + private function parseQuery(string $key, $value, bool $withoutOperator = false): array { if ($key === BuilderInterface::OP_NULL) { - $params = [ 'exists' => [ 'field' => $value ] ]; - return $withoutOperator ? $params : [ 'must_not' => $params ]; + $params = ['exists' => ['field' => $value]]; + + return $withoutOperator ? $params : ['must_not' => $params]; } if ($key === BuilderInterface::OP_NOT_NULL) { - $params = [ 'field' => $value ]; - return $withoutOperator ? $params : [ 'exists' => $params ]; + $params = ['field' => $value]; + + return $withoutOperator ? $params : ['exists' => $params]; } if (! is_array($value)) { - $params = [ 'term' => [ $key => $value ] ]; - return $withoutOperator ? $params : [ 'must' => $params ]; + $params = ['term' => [$key => $value]]; + + return $withoutOperator ? $params : ['must' => $params]; } if ($key === BuilderInterface::OP_OR) { $params = []; foreach ($value as $query) { - $value2 = reset($query); - $key2 = key($query); + $value2 = reset($query); + $key2 = key($query); $params[] = $this->parseQuery($key2, $value2, true); } - return $withoutOperator ? $params : [ 'should' => $params ]; + + return $withoutOperator ? $params : ['should' => $params]; } if ($key === BuilderInterface::OP_AND) { $params = []; foreach ($value as $query) { - $value2 = reset($query); - $key2 = key($query); + $value2 = reset($query); + $key2 = key($query); $params[] = $this->parseQuery($key2, $value2, true); } - return $withoutOperator ? $params : [ 'must' => $params ]; + + return $withoutOperator ? $params : ['must' => $params]; } $opValue = reset($value); - $op = key($value); + $op = key($value); if (in_array($op, BuilderInterface::OP_LOGIC)) { return $this->parseLogic($key, $op, $opValue); @@ -101,65 +120,82 @@ private function parseQuery(string $key, $value, bool $withoutOperator = false) return []; } - private function parseLogic(string $key, $op, $value) : array + /** + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + * + * @param mixed $value + */ + private function parseLogic(string $key, string $op, $value): array { if ($op === BuilderInterface::OP_NOT) { - return [ 'must_not' => [ 'term' => [ $key => $value ] ] ]; + return ['must_not' => ['term' => [$key => $value]]]; } if ($op === BuilderInterface::OP_IN) { - return [ 'filter' => [ 'terms' => [ $key => $value ] ] ]; + return ['filter' => ['terms' => [$key => $value]]]; } if ($op === BuilderInterface::OP_NOT_IN) { - return [ 'must_not' => [ 'terms' => [ $key => $value ] ] ]; + return ['must_not' => ['terms' => [$key => $value]]]; } // At this point, should only be BuilderInterface::OP_LIKE . No if to keep PHPUnit happy - return [ 'wildcard' => [ $key => str_replace('%', '*', $value) ] ]; + return ['wildcard' => [$key => str_replace('%', '*', $value)]]; } - private function parseConditional(string $key, $op, $value) : array + /** + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + * + * @param mixed $value + */ + private function parseConditional(string $key, string $op, $value): array { if ($op === BuilderInterface::OP_GREATER) { - return [ 'filter' => [ 'range' => [ $key => [ 'gt' => $value ] ] ] ]; + return ['filter' => ['range' => [$key => ['gt' => $value]]]]; } if ($op === BuilderInterface::OP_GREATER_EQUAL) { - return [ 'filter' => [ 'range' => [ $key => [ 'gte' => $value ] ] ] ]; + return ['filter' => ['range' => [$key => ['gte' => $value]]]]; } if ($op === BuilderInterface::OP_LESS) { - return [ 'filter' => [ 'range' => [ $key => [ 'lt' => $value ] ] ] ]; + return ['filter' => ['range' => [$key => ['lt' => $value]]]]; } if ($op === BuilderInterface::OP_LESS_EQUAL) { - return [ 'filter' => [ 'range' => [ $key => [ 'lte' => $value ] ] ] ]; + return ['filter' => ['range' => [$key => ['lte' => $value]]]]; } // At this point, should only be BuilderInterface::OP_BETWEEN . No if to keep PHPUnit happy - return [ 'filter' => [ 'range' => [ $key => [ 'gt' => $value[0], 'lt' => $value[1] ] ] ] ]; + return ['filter' => ['range' => [$key => ['gt' => $value[0], 'lt' => $value[1]]]]]; } - private function parseHint($key, $value) : array + /** + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + * + * @param mixed $value + */ + private function parseHint(string $key, $value): array { if ($key === BuilderInterface::HINT_SORT) { if (! is_array($value)) { - return [ 'sort' => [ $value ] ]; + return ['sort' => [$value]]; } + $params = []; foreach ($value as $field => $order) { - $order = in_array($order, BuilderInterface::HINT_ORDER_ASC) ? 'asc' : 'desc'; - $params[] = [ $field => [ 'order' => $order ] ]; + $order = in_array($order, BuilderInterface::HINT_ORDER_ASC) ? 'asc' : 'desc'; + $params[] = [$field => ['order' => $order]]; } - return [ 'sort' => $params ]; + + return ['sort' => $params]; } if ($key === BuilderInterface::HINT_LIMIT) { - return [ 'size' => $value ]; + return ['size' => $value]; } // At this point, should only be BuilderInterface::HINT_SKIP . No if to keep PHPUnit happy - return [ 'from' => $value ]; + return ['from' => $value]; } } diff --git a/src/Exception/MalformedException.php b/src/Exception/MalformedException.php index e594d76..40e9b5a 100644 --- a/src/Exception/MalformedException.php +++ b/src/Exception/MalformedException.php @@ -1,10 +1,14 @@ queryName = $queryName; - $this->hintName = $hintName; - $this->select = clone $select; + $this->hintName = $hintName; + $this->select = clone $select; } - /** - * @param ServerRequestInterface $request - * @return Select - */ - public function fromRequest(ServerRequestInterface $request) : Select + public function fromRequest(ServerRequestInterface $request): Select { $queryParams = $request->getQueryParams(); - $query = json_decode($queryParams['q'] ?? '{}', true); - $hint = json_decode($queryParams['h'] ?? '{}', true); + $query = json_decode($queryParams[$this->queryName] ?? '{}', true); + $hint = json_decode($queryParams[$this->hintName] ?? '{}', true); if (! is_array($query) || ! is_array($hint)) { throw new Exception\MalformedException('Invalid query or hint'); @@ -48,9 +45,8 @@ public function fromRequest(ServerRequestInterface $request) : Select /** * @param array $query * @param array $hint - * @return Select */ - public function fromParams(array $query, array $hint = []) : Select + public function fromParams(array $query, array $hint = []): Select { foreach ($query as $key => $value) { $this->parseQuery($key, $value, $this->select->where); @@ -64,24 +60,27 @@ public function fromParams(array $query, array $hint = []) : Select } /** - * @param mixed $key + * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint + * * @param mixed $value - * @param Predicate $where */ - private function parseQuery($key, $value, Predicate $where) : void + private function parseQuery(string $key, $value, Predicate $where): void { if ($key === BuilderInterface::OP_NULL) { $where->addPredicate(new IsNull($value)); + return; } if ($key === BuilderInterface::OP_NOT_NULL) { $where->addPredicate(new IsNotNull($value)); + return; } if (! is_array($value)) { $where->equalTo($key, $value); + return; } @@ -89,10 +88,11 @@ private function parseQuery($key, $value, Predicate $where) : void $nested = $where->nest(); foreach ($value as $query) { $value2 = reset($query); - $key2 = key($query); + $key2 = key($query); $this->parseQuery($key2, $value2, $nested); $nested->or; } + return; } @@ -100,47 +100,47 @@ private function parseQuery($key, $value, Predicate $where) : void $nested = $where->nest(); foreach ($value as $query) { $value2 = reset($query); - $key2 = key($query); + $key2 = key($query); $this->parseQuery($key2, $value2, $nested); $nested->and; } + return; } $opValue = reset($value); - $op = key($value); + $op = key($value); if (in_array($op, BuilderInterface::OP_LOGIC)) { $this->parseLogic($key, $op, $opValue, $where); + return; } if (in_array($op, BuilderInterface::OP_CONDITIONAL)) { $this->parseConditional($key, $op, $opValue, $where); + return; } } - /** - * @param mixed $key - * @param string $op - * @param mixed $value - * @param Predicate $where - */ - private function parseLogic($key, string $op, $value, Predicate $where) : void + private function parseLogic(string $key, string $op, $value, Predicate $where): void { if ($op === BuilderInterface::OP_NOT) { $where->notEqualTo($key, $value); + return; } if ($op === BuilderInterface::OP_IN) { $where->in($key, $value); + return; } if ($op === BuilderInterface::OP_NOT_IN) { $where->notIn($key, $value); + return; } @@ -148,31 +148,29 @@ private function parseLogic($key, string $op, $value, Predicate $where) : void $where->like($key, $value); } - /** - * @param mixed $key - * @param string $op - * @param mixed $value - * @param Predicate $where - */ - private function parseConditional($key, string $op, $value, Predicate $where) : void + private function parseConditional(string $key, string $op, $value, Predicate $where): void { if ($op === BuilderInterface::OP_GREATER) { $where->greaterThan($key, $value); + return; } if ($op === BuilderInterface::OP_GREATER_EQUAL) { $where->greaterThanOrEqualTo($key, $value); + return; } if ($op === BuilderInterface::OP_LESS) { $where->lessThan($key, $value); + return; } if ($op === BuilderInterface::OP_LESS_EQUAL) { $where->lessThanOrEqualTo($key, $value); + return; } @@ -180,30 +178,29 @@ private function parseConditional($key, string $op, $value, Predicate $where) : $where->between($key, $value[0], $value[1]); } - /** - * @param mixed $key - * @param mixed $value - * @param Select $select - */ - private function parseHint($key, $value, Select $select) : void + private function parseHint(string $key, $value, Select $select): void { if ($key === BuilderInterface::HINT_SORT) { if (! is_array($value)) { $select->order($value); + return; } + foreach ($value as $sort => $order) { $order = in_array($order, BuilderInterface::HINT_ORDER_ASC) ? Select::ORDER_ASCENDING : Select::ORDER_DESCENDING; - $value = "$sort $order"; + $value = $sort . ' ' . $order; $select->order($value); } + return; } if ($key === BuilderInterface::HINT_LIMIT) { $select->limit($value); + return; }