Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.2.x' into php-8
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansoweb committed Sep 19, 2023
2 parents d7ec4cf + 6a61718 commit 86ddeca
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 19 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.2.0 - TBD

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 1.0.0 - 2020-01-02

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@phpstan",
"@test"
],
"cs-check": "phpcs",
"cs-check": "phpcs -s",
"cs-fix": "phpcbf",
"phpstan": "phpstan analyse",
"test": "phpunit --colors=always",
Expand Down
37 changes: 37 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<ruleset name="App coding standard">
<description>App coding standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>

<!-- inherit rules from: -->
<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<!-- <exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint"/>-->
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
<!-- <exclude name="Squiz.Commenting.FunctionComment.WrongStyle"/>-->
</rule>
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="160"/>
<property name="absoluteLineLimit" value="180"/>
</properties>
</rule>

<rule ref="Generic.CodeAnalysis.EmptyStatement.DetectedCatch">
<severity>5</severity>
</rule>

<!-- Paths to check -->
<file>src</file>
</ruleset>
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ parameters:
paths:
- ./src
ignoreErrors:
- '#no type specified#'
- '#mixed#'
- '#on a separate line does not do anything#'
11 changes: 9 additions & 2 deletions src/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Los\Uql;

use Laminas\Db\Sql\Select;
use Psr\Http\Message\ServerRequestInterface;

interface BuilderInterface
Expand Down Expand Up @@ -52,7 +53,13 @@ interface BuilderInterface
public const HINT_ORDER_ASC = ['asc', 'ASC', 1, '1'];
public const HINT_ORDER_DESC = ['desc', 'DESC', -1, '-1'];

public function fromRequest(ServerRequestInterface $request): mixed;
/**
* @phpcs:disable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*
* @return Select|array
*/
public function fromRequest(ServerRequestInterface $request);

public function fromParams(array $query, array $hint = []): mixed;
/** @return Select|array */
public function fromParams(array $query, array $hint = []);
}
38 changes: 31 additions & 7 deletions src/ElasticSearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@
use Psr\Http\Message\ServerRequestInterface;

use function array_merge;
use function assert;
use function in_array;
use function is_array;
use function is_string;
use function json_decode;
use function key;
use function reset;
use function str_replace;

final class ElasticSearchBuilder implements BuilderInterface
{
private $params = [];
private array $params = [];

public function __construct()
public function __construct(private string $queryName = 'q', private string $hintName = 'h')
{
}

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');
Expand Down Expand Up @@ -55,6 +57,11 @@ public function fromParams(array $query, array $hint = []): array
return $result;
}

/**
* @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
* @param mixed $value
*/
private function parseQuery(string $key, $value, bool $withoutOperator = false): array
{
if ($key === BuilderInterface::OP_NULL) {
Expand Down Expand Up @@ -100,6 +107,8 @@ private function parseQuery(string $key, $value, bool $withoutOperator = false):
$opValue = reset($value);
$op = key($value);

assert(is_string($op));

if (in_array($op, BuilderInterface::OP_LOGIC)) {
return $this->parseLogic($key, $op, $opValue);
}
Expand All @@ -111,7 +120,12 @@ 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]]];
Expand All @@ -129,7 +143,12 @@ private function parseLogic(string $key, $op, $value): array
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]]]];
Expand All @@ -151,7 +170,12 @@ private function parseConditional(string $key, $op, $value): array
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)) {
Expand Down
1 change: 1 addition & 0 deletions src/Exception/MalformedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use RuntimeException;

/** @phpcs:disable SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix */
class MalformedException extends RuntimeException
{
}
21 changes: 13 additions & 8 deletions src/LaminasDbBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

final class LaminasDbBuilder implements BuilderInterface
{
private $select;
private Select $select;

public function __construct(Select $select)
public function __construct(Select $select, private string $queryName = 'q', private string $hintName = 'h')
{
$this->select = clone $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');
Expand All @@ -57,7 +57,12 @@ public function fromParams(array $query, array $hint = []): Select
return $this->select;
}

private function parseQuery(mixed $key, mixed $value, Predicate $where): void
/**
* @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
* @param mixed $value
*/
private function parseQuery(string $key, $value, Predicate $where): void
{
if ($key === BuilderInterface::OP_NULL) {
$where->addPredicate(new IsNull($value));
Expand Down Expand Up @@ -119,7 +124,7 @@ private function parseQuery(mixed $key, mixed $value, Predicate $where): void
}
}

private function parseLogic(mixed $key, string $op, mixed $value, Predicate $where): void
private function parseLogic(string $key, string $op, mixed $value, Predicate $where): void
{
if ($op === BuilderInterface::OP_NOT) {
$where->notEqualTo($key, $value);
Expand All @@ -143,7 +148,7 @@ private function parseLogic(mixed $key, string $op, mixed $value, Predicate $whe
$where->like($key, $value);
}

private function parseConditional(mixed $key, string $op, mixed $value, Predicate $where): void
private function parseConditional(string $key, string $op, mixed $value, Predicate $where): void
{
if ($op === BuilderInterface::OP_GREATER) {
$where->greaterThan($key, $value);
Expand Down Expand Up @@ -173,7 +178,7 @@ private function parseConditional(mixed $key, string $op, mixed $value, Predicat
$where->between($key, $value[0], $value[1]);
}

private function parseHint(mixed $key, mixed $value, Select $select): void
private function parseHint(string $key, mixed $value, Select $select): void
{
if ($key === BuilderInterface::HINT_SORT) {
if (! is_array($value)) {
Expand Down

0 comments on commit 86ddeca

Please sign in to comment.