From b65d149f321eac9d3d015b5090e3b4bdfeb2b29a Mon Sep 17 00:00:00 2001 From: Kirill Fuks Date: Wed, 26 Jul 2023 19:14:01 +0200 Subject: [PATCH 1/4] feat: add first draft for supporting complex logic conditions --- .../{Operator.php => FilterOperators.php} | 7 +-- src/RequestBuilder/Enums/LogicOperators.php | 12 +++++ src/RequestBuilder/Enums/OperatorModifier.php | 11 ++++ .../Exceptions/FilterLogicException.php | 2 + src/RequestBuilder/LogicOperatorCondition.php | 36 +++++++++++++ .../Traits/ArrayRangeOperators.php | 26 +++++----- .../Traits/EqualityOperators.php | 12 ++--- src/RequestBuilder/Traits/FilterOperators.php | 1 + .../Traits/FullTextSearchOperators.php | 10 ++-- .../Traits/GreaterLessOperators.php | 14 ++--- .../Traits/LogicalOperators.php | 52 +++++++++++++++++++ .../Traits/ModifierOperators.php | 14 ++--- .../Traits/PatternMatchingOperators.php | 14 ++--- 13 files changed, 161 insertions(+), 50 deletions(-) rename src/RequestBuilder/Enums/{Operator.php => FilterOperators.php} (88%) create mode 100644 src/RequestBuilder/Enums/LogicOperators.php create mode 100644 src/RequestBuilder/Enums/OperatorModifier.php create mode 100644 src/RequestBuilder/LogicOperatorCondition.php create mode 100644 src/RequestBuilder/Traits/LogicalOperators.php diff --git a/src/RequestBuilder/Enums/Operator.php b/src/RequestBuilder/Enums/FilterOperators.php similarity index 88% rename from src/RequestBuilder/Enums/Operator.php rename to src/RequestBuilder/Enums/FilterOperators.php index 9650a4e..ab6a23e 100644 --- a/src/RequestBuilder/Enums/Operator.php +++ b/src/RequestBuilder/Enums/FilterOperators.php @@ -9,7 +9,7 @@ * * @link https://postgrest.org/en/stable/references/api/tables_views.html#operators */ -enum Operator: string +enum FilterOperators: string { case EQUAL = 'eq'; case GREATER_THAN = 'gt'; @@ -36,9 +36,4 @@ enum Operator: string case NOT_EXTEND_TO_RIGHT = 'nxr'; case NOT_EXTEND_TO_LEFT = 'nxl'; case ADJACENT = 'adj'; - case NOT = 'not'; - case OR = 'or'; - case AND = 'and'; - case ALL = 'all'; - case ANY = 'any'; } diff --git a/src/RequestBuilder/Enums/LogicOperators.php b/src/RequestBuilder/Enums/LogicOperators.php new file mode 100644 index 0000000..b910415 --- /dev/null +++ b/src/RequestBuilder/Enums/LogicOperators.php @@ -0,0 +1,12 @@ +modifier && $this->language) { + throw new FilterLogicException(FilterLogicException::INVALID_CONDITION); + } + } + + public function __toString() + { + $operator = $this->operator->value; + $operator = $this->negate ? sprintf('%s.%s', LogicOperators::NOT->value, $operator) : $operator; + $operator = $this->modifier ? sprintf('%s(%s)', $operator, $this->modifier->value) : $operator; + $operator = $this->language ? sprintf('%s(%s)', $operator, $this->language) : $operator; + return sprintf('%s.%s.%s', $this->column, $operator, strval($this->value)); + } +} diff --git a/src/RequestBuilder/Traits/ArrayRangeOperators.php b/src/RequestBuilder/Traits/ArrayRangeOperators.php index ce7aacc..dd7fc3d 100644 --- a/src/RequestBuilder/Traits/ArrayRangeOperators.php +++ b/src/RequestBuilder/Traits/ArrayRangeOperators.php @@ -4,7 +4,7 @@ namespace PostgrestPhp\RequestBuilder\Traits; -use PostgrestPhp\RequestBuilder\Enums\Operator; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\Enums\OverlapType; use PostgrestPhp\RequestBuilder\Exceptions\NotUnifiedValuesException; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; @@ -25,7 +25,7 @@ trait ArrayRangeOperators */ public function cs(string $columnName, array $value): PostgrestRequestBuilder { - return $this->arrayHelper(Operator::CONTAINS, $columnName, $value, ['{', '}']); + return $this->arrayHelper(FilterOperators::CONTAINS, $columnName, $value, ['{', '}']); } /** @@ -39,7 +39,7 @@ public function cs(string $columnName, array $value): PostgrestRequestBuilder */ public function cd(string $columnName, array $value): PostgrestRequestBuilder { - return $this->arrayHelper(Operator::CONTAINED_IN, $columnName, $value, ['{', '}']); + return $this->arrayHelper(FilterOperators::CONTAINED_IN, $columnName, $value, ['{', '}']); } /** @@ -60,7 +60,7 @@ public function ov( if (! $this->helper::checkUnifiedValueTypes($value)) { throw new NotUnifiedValuesException(NotUnifiedValuesException::NOT_UNIFIED_ARRAY); } - $operator = $this->negateOperator(Operator::OVERLAP, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::OVERLAP, $this->negateNextFilter); $transformedValue = $this->helper::implodeWithBraces($value, $overlapType->value[0], $overlapType->value[1]); return $this->filterRawColumn($columnName, $operator, $transformedValue); } @@ -77,7 +77,7 @@ public function ov( */ public function sl(string $columnName, int|float $start, int|float $end): PostgrestRequestBuilder { - return $this->rangeHelper(Operator::STRICTLY_LEFT_OF, $columnName, $start, $end); + return $this->rangeHelper(FilterOperators::STRICTLY_LEFT_OF, $columnName, $start, $end); } /** @@ -92,7 +92,7 @@ public function sl(string $columnName, int|float $start, int|float $end): Postgr */ public function sr(string $columnName, int|float $start, int|float $end): PostgrestRequestBuilder { - return $this->rangeHelper(Operator::STRICTLY_RIGHT_OF, $columnName, $start, $end); + return $this->rangeHelper(FilterOperators::STRICTLY_RIGHT_OF, $columnName, $start, $end); } /** @@ -107,7 +107,7 @@ public function sr(string $columnName, int|float $start, int|float $end): Postgr */ public function nxr(string $columnName, int|float $start, int|float $end): PostgrestRequestBuilder { - return $this->rangeHelper(Operator::NOT_EXTEND_TO_RIGHT, $columnName, $start, $end); + return $this->rangeHelper(FilterOperators::NOT_EXTEND_TO_RIGHT, $columnName, $start, $end); } /** @@ -122,7 +122,7 @@ public function nxr(string $columnName, int|float $start, int|float $end): Postg */ public function nxl(string $columnName, int|float $start, int|float $end): PostgrestRequestBuilder { - return $this->rangeHelper(Operator::NOT_EXTEND_TO_LEFT, $columnName, $start, $end); + return $this->rangeHelper(FilterOperators::NOT_EXTEND_TO_LEFT, $columnName, $start, $end); } /** @@ -137,13 +137,13 @@ public function nxl(string $columnName, int|float $start, int|float $end): Postg */ public function adj(string $columnName, int|float $start, int|float $end): PostgrestRequestBuilder { - return $this->rangeHelper(Operator::ADJACENT, $columnName, $start, $end); + return $this->rangeHelper(FilterOperators::ADJACENT, $columnName, $start, $end); } /** * Array operator helper. * Prevents code duplication. - * @param Operator $op The operator to use. + * @param FilterOperators $op The operator to use. * @param string $columnName The name of the column. * @param string[]|int[]|float[] $value The value to use. * @param string[] $braces The braces to use. @@ -151,7 +151,7 @@ public function adj(string $columnName, int|float $start, int|float $end): Postg * @throws NotUnifiedValuesException If the value types are not unified. */ private function arrayHelper( - Operator $op, + FilterOperators $op, string $columnName, array $value, array $braces, @@ -167,7 +167,7 @@ private function arrayHelper( /** * Range operator helper. * Prevents code duplication. - * @param Operator $op The operator to use. + * @param FilterOperators $op The operator to use. * @param string $columnName The name of the column. * @param int|float $start The start value to use. * @param int|float $end The end value to use. @@ -175,7 +175,7 @@ private function arrayHelper( * @throws NotUnifiedValuesException If the start and end types are not unified. */ private function rangeHelper( - Operator $op, + FilterOperators $op, string $columnName, int|float $start, int|float $end, diff --git a/src/RequestBuilder/Traits/EqualityOperators.php b/src/RequestBuilder/Traits/EqualityOperators.php index 7a58268..4767b08 100644 --- a/src/RequestBuilder/Traits/EqualityOperators.php +++ b/src/RequestBuilder/Traits/EqualityOperators.php @@ -4,8 +4,8 @@ namespace PostgrestPhp\RequestBuilder\Traits; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\Enums\IsCheck; -use PostgrestPhp\RequestBuilder\Enums\Operator; use PostgrestPhp\RequestBuilder\Exceptions\FilterLogicException; use PostgrestPhp\RequestBuilder\Exceptions\NotUnifiedValuesException; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; @@ -29,7 +29,7 @@ public function eq(string $columnName, string|int|float ...$value): PostgrestReq { $numValues = count($value); $this->checkOperatorModifier($numValues, $value); - $operator = $this->negateOperator(Operator::EQUAL, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::EQUAL, $this->negateNextFilter); if ($numValues > 1) { $operator = $this->applyOperatorModifier($operator); $transformedValue = $this->helper::implodeWithBraces($value, '{', '}'); @@ -49,7 +49,7 @@ public function eq(string $columnName, string|int|float ...$value): PostgrestReq */ public function neq(string $columnName, string|int|float $value): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::NOT_EQUAL, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::NOT_EQUAL, $this->negateNextFilter); return $this->filterRawColumn($columnName, $operator, $this->helper::escapeString($value)); } @@ -63,7 +63,7 @@ public function neq(string $columnName, string|int|float $value): PostgrestReque */ public function is(string $columnName, IsCheck $value): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::IS, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::IS, $this->negateNextFilter); return $this->filterRawColumn($columnName, $operator, $value->value); } @@ -81,7 +81,7 @@ public function in(string $columnName, string|int|float ...$value): PostgrestReq if (! $this->helper::checkUnifiedValueTypes($value)) { throw new NotUnifiedValuesException(NotUnifiedValuesException::NOT_UNIFIED_ARRAY); } - $operator = $this->negateOperator(Operator::IN, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::IN, $this->negateNextFilter); $transformedValue = $this->helper::implodeWithBraces($value, '(', ')'); return $this->filterRawColumn($columnName, $operator, $transformedValue); } @@ -96,7 +96,7 @@ public function in(string $columnName, string|int|float ...$value): PostgrestReq */ public function isdistinct(string $columnName, string|int|float $value): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::IS_DISTINCT_FROM, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::IS_DISTINCT_FROM, $this->negateNextFilter); return $this->filterRawColumn($columnName, $operator, $this->helper::escapeString($value)); } } diff --git a/src/RequestBuilder/Traits/FilterOperators.php b/src/RequestBuilder/Traits/FilterOperators.php index ecc48af..d44aff1 100644 --- a/src/RequestBuilder/Traits/FilterOperators.php +++ b/src/RequestBuilder/Traits/FilterOperators.php @@ -13,6 +13,7 @@ trait FilterOperators { use ModifierOperators; use EqualityOperators; + use LogicalOperators; use GreaterLessOperators; use PatternMatchingOperators; use FullTextSearchOperators; diff --git a/src/RequestBuilder/Traits/FullTextSearchOperators.php b/src/RequestBuilder/Traits/FullTextSearchOperators.php index 84f2be4..d4183f0 100644 --- a/src/RequestBuilder/Traits/FullTextSearchOperators.php +++ b/src/RequestBuilder/Traits/FullTextSearchOperators.php @@ -4,7 +4,7 @@ namespace PostgrestPhp\RequestBuilder\Traits; -use PostgrestPhp\RequestBuilder\Enums\Operator; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; /** @@ -23,7 +23,7 @@ trait FullTextSearchOperators */ public function fts(string $columnName, string $value, ?string $language = null): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::FULL_TEXT_SEARCH, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::FULL_TEXT_SEARCH, $this->negateNextFilter); return $this->ftsHelper($operator, $columnName, $value, $language); } @@ -38,7 +38,7 @@ public function fts(string $columnName, string $value, ?string $language = null) */ public function plfts(string $columnName, string $value, ?string $language = null): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::PLAIN_FULL_TEXT_SEARCH, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::PLAIN_FULL_TEXT_SEARCH, $this->negateNextFilter); return $this->ftsHelper($operator, $columnName, $value, $language); } @@ -53,7 +53,7 @@ public function plfts(string $columnName, string $value, ?string $language = nul */ public function phfts(string $columnName, string $value, ?string $language = null): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::PHRASE_FULL_TEXT_SEARCH, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::PHRASE_FULL_TEXT_SEARCH, $this->negateNextFilter); return $this->ftsHelper($operator, $columnName, $value, $language); } @@ -68,7 +68,7 @@ public function phfts(string $columnName, string $value, ?string $language = nul */ public function wfts(string $columnName, string $value, ?string $language = null): PostgrestRequestBuilder { - $operator = $this->negateOperator(Operator::WEBSEARCH_FULL_TEXT_SEARCH, $this->negateNextFilter); + $operator = $this->negateOperator(FilterOperators::WEBSEARCH_FULL_TEXT_SEARCH, $this->negateNextFilter); return $this->ftsHelper($operator, $columnName, $value, $language); } diff --git a/src/RequestBuilder/Traits/GreaterLessOperators.php b/src/RequestBuilder/Traits/GreaterLessOperators.php index 86fb939..27190ae 100644 --- a/src/RequestBuilder/Traits/GreaterLessOperators.php +++ b/src/RequestBuilder/Traits/GreaterLessOperators.php @@ -4,7 +4,7 @@ namespace PostgrestPhp\RequestBuilder\Traits; -use PostgrestPhp\RequestBuilder\Enums\Operator; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\Exceptions\FilterLogicException; use PostgrestPhp\RequestBuilder\Exceptions\NotUnifiedValuesException; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; @@ -26,7 +26,7 @@ trait GreaterLessOperators */ public function gt(string $columnName, string|int|float ...$value): PostgrestRequestBuilder { - return $this->greaterLessHelper(Operator::GREATER_THAN, $columnName, ...$value); + return $this->greaterLessHelper(FilterOperators::GREATER_THAN, $columnName, ...$value); } /** @@ -41,7 +41,7 @@ public function gt(string $columnName, string|int|float ...$value): PostgrestReq */ public function gte(string $columnName, string|int|float ...$value): PostgrestRequestBuilder { - return $this->greaterLessHelper(Operator::GREATER_THAN_OR_EQUAL, $columnName, ...$value); + return $this->greaterLessHelper(FilterOperators::GREATER_THAN_OR_EQUAL, $columnName, ...$value); } /** @@ -56,7 +56,7 @@ public function gte(string $columnName, string|int|float ...$value): PostgrestRe */ public function lt(string $columnName, string|int|float ...$value): PostgrestRequestBuilder { - return $this->greaterLessHelper(Operator::LESS_THAN, $columnName, ...$value); + return $this->greaterLessHelper(FilterOperators::LESS_THAN, $columnName, ...$value); } /** @@ -71,13 +71,13 @@ public function lt(string $columnName, string|int|float ...$value): PostgrestReq */ public function lte(string $columnName, string|int|float ...$value): PostgrestRequestBuilder { - return $this->greaterLessHelper(Operator::LESS_THAN_OR_EQUAL, $columnName, ...$value); + return $this->greaterLessHelper(FilterOperators::LESS_THAN_OR_EQUAL, $columnName, ...$value); } /** * Greater than operator helper. * Prevents code duplication. - * @param Operator $op The operator to use. + * @param FilterOperators $op The operator to use. * @param string $columnName The name of the column. * @param string|int|float ...$value The value(s) to use. * @return PostgrestRequestBuilder The PostgrestRequestBuilder instance. @@ -85,7 +85,7 @@ public function lte(string $columnName, string|int|float ...$value): PostgrestRe * @throws NotUnifiedValuesException If the value types are not unified. */ private function greaterLessHelper( - Operator $op, + FilterOperators $op, string $columnName, string|int|float ...$value, ): PostgrestRequestBuilder { diff --git a/src/RequestBuilder/Traits/LogicalOperators.php b/src/RequestBuilder/Traits/LogicalOperators.php new file mode 100644 index 0000000..3f7aa73 --- /dev/null +++ b/src/RequestBuilder/Traits/LogicalOperators.php @@ -0,0 +1,52 @@ +negateOperator(LogicOperators::AND, $this->negateNextFilter); + $filter = sprintf('%s=(%s)', $operator, implode(',', $condition)); + return $this->filterRaw($filter); + } + + /** + * Basic implementation for 'or' logical operator. + * You will need to provide each condition as a string, + * and escape it yourself. Alternatively, you can use + * the LogicOperatorCondition class to build the condition, + * but this route does not support nested and/or conditions. + * + * @param Stringable ...$condition The conditions to use. + * @return PostgrestRequestBuilder The PostgrestRequestBuilder instance. + */ + public function or(Stringable ...$condition): PostgrestRequestBuilder + { + $operator = $this->negateOperator(LogicOperators::OR, $this->negateNextFilter); + $filter = sprintf('%s=(%s)', $operator, implode(',', $condition)); + return $this->filterRaw($filter); + } +} diff --git a/src/RequestBuilder/Traits/ModifierOperators.php b/src/RequestBuilder/Traits/ModifierOperators.php index 3257f3a..4020669 100644 --- a/src/RequestBuilder/Traits/ModifierOperators.php +++ b/src/RequestBuilder/Traits/ModifierOperators.php @@ -4,7 +4,9 @@ namespace PostgrestPhp\RequestBuilder\Traits; -use PostgrestPhp\RequestBuilder\Enums\Operator; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; +use PostgrestPhp\RequestBuilder\Enums\LogicOperators; +use PostgrestPhp\RequestBuilder\Enums\OperatorModifier; use PostgrestPhp\RequestBuilder\Exceptions\FilterLogicException; use PostgrestPhp\RequestBuilder\Exceptions\NotUnifiedValuesException; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; @@ -67,15 +69,15 @@ public function any(): PostgrestRequestBuilder /** * Prepend negation to operator if necessary. * - * @param Operator $operator The operator to use. + * @param FilterOperators|LogicOperators $operator The operator to use. * @param bool $negate Whether to negate the operator. * @return string The operator. */ - private function negateOperator(Operator $operator, bool $negate): string + private function negateOperator(FilterOperators|LogicOperators $operator, bool $negate): string { if ($negate) { $this->negateNextFilter = false; - return sprintf('%s.%s', Operator::NOT->value, $operator->value); + return sprintf('%s.%s', LogicOperators::NOT->value, $operator->value); } return $operator->value; } @@ -89,7 +91,7 @@ private function negateOperator(Operator $operator, bool $negate): string private function allOperator(string $operator): string { $this->allNextFilter = false; - return sprintf('%s(%s)', $operator, Operator::ALL->value); + return sprintf('%s(%s)', $operator, OperatorModifier::ALL->value); } /** @@ -101,7 +103,7 @@ private function allOperator(string $operator): string private function anyOperator(string $operator): string { $this->allNextFilter = false; - return sprintf('%s(%s)', $operator, Operator::ANY->value); + return sprintf('%s(%s)', $operator, OperatorModifier::ANY->value); } /** diff --git a/src/RequestBuilder/Traits/PatternMatchingOperators.php b/src/RequestBuilder/Traits/PatternMatchingOperators.php index 69bd3cb..b0b57c2 100644 --- a/src/RequestBuilder/Traits/PatternMatchingOperators.php +++ b/src/RequestBuilder/Traits/PatternMatchingOperators.php @@ -4,7 +4,7 @@ namespace PostgrestPhp\RequestBuilder\Traits; -use PostgrestPhp\RequestBuilder\Enums\Operator; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\Exceptions\FilterLogicException; use PostgrestPhp\RequestBuilder\PostgrestRequestBuilder; @@ -24,7 +24,7 @@ trait PatternMatchingOperators */ public function like(string $columnName, string ...$value): PostgrestRequestBuilder { - return $this->patternMatchingHelper(Operator::LIKE, $columnName, ...$value); + return $this->patternMatchingHelper(FilterOperators::LIKE, $columnName, ...$value); } /** @@ -38,7 +38,7 @@ public function like(string $columnName, string ...$value): PostgrestRequestBuil */ public function ilike(string $columnName, string ...$value): PostgrestRequestBuilder { - return $this->patternMatchingHelper(Operator::ILIKE, $columnName, ...$value); + return $this->patternMatchingHelper(FilterOperators::ILIKE, $columnName, ...$value); } /** @@ -52,7 +52,7 @@ public function ilike(string $columnName, string ...$value): PostgrestRequestBui */ public function match(string $columnName, string ...$value): PostgrestRequestBuilder { - return $this->patternMatchingHelper(Operator::MATCH, $columnName, ...$value); + return $this->patternMatchingHelper(FilterOperators::MATCH, $columnName, ...$value); } /** @@ -66,20 +66,20 @@ public function match(string $columnName, string ...$value): PostgrestRequestBui */ public function imatch(string $columnName, string ...$value): PostgrestRequestBuilder { - return $this->patternMatchingHelper(Operator::IMATCH, $columnName, ...$value); + return $this->patternMatchingHelper(FilterOperators::IMATCH, $columnName, ...$value); } /** * Pattern matching operator helper. * Prevents code duplication. - * @param Operator $op The operator to use. + * @param FilterOperators $op The operator to use. * @param string $columnName The name of the column. * @param string ...$value The value(s) to use. * @return PostgrestRequestBuilder The PostgrestRequestBuilder instance. * @throws FilterLogicException If multiple values are passed without modifier like all() or any(). */ private function patternMatchingHelper( - Operator $op, + FilterOperators $op, string $columnName, string ...$value, ): PostgrestRequestBuilder { From 430170819d592f1096fc32bffeb44f67c621b182 Mon Sep 17 00:00:00 2001 From: Kirill Fuks Date: Wed, 26 Jul 2023 19:14:28 +0200 Subject: [PATCH 2/4] feat: add test cases for complex logic conditions --- tests/FilterOperatorTest.php | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/FilterOperatorTest.php b/tests/FilterOperatorTest.php index 9f645bb..dfe49f3 100644 --- a/tests/FilterOperatorTest.php +++ b/tests/FilterOperatorTest.php @@ -7,10 +7,13 @@ use PHPUnit\Framework\TestCase; use PostgrestPhp\Client\Base\ClientAuthConfig; use PostgrestPhp\Client\PostgrestSyncClient; +use PostgrestPhp\RequestBuilder\Enums\FilterOperators; use PostgrestPhp\RequestBuilder\Enums\IsCheck; +use PostgrestPhp\RequestBuilder\Enums\OperatorModifier; use PostgrestPhp\RequestBuilder\Enums\OverlapType; use PostgrestPhp\RequestBuilder\Exceptions\FilterLogicException; use PostgrestPhp\RequestBuilder\Exceptions\NotUnifiedValuesException; +use PostgrestPhp\RequestBuilder\LogicOperatorCondition; class FilterOperatorTest extends TestCase { @@ -622,4 +625,48 @@ public function testAdjacentFilter(): void ->select('*') ->adj('g', 10, 15.0); } + + public function testOrFilter(): void + { + $response = self::$client->run( + self::$client + ->from('test_schema', 'filter_test_table') + ->select('*') + ->or( + (new LogicOperatorCondition('e', FilterOperators::GREATER_THAN, '2020-01-09')), + (new LogicOperatorCondition('e', FilterOperators::LESS_THAN, '2020-01-02')) + ) + ); + $result = $response->result(); + $this->assertNotNull($result); + $this->assertEquals(2, count($result)); + } + + public function testAndFilter(): void + { + $response = self::$client->run( + self::$client + ->from('test_schema', 'filter_test_table') + ->select('*') + ->and( + (new LogicOperatorCondition('c', FilterOperators::IN, '(0.1,0.2)')), + (new LogicOperatorCondition('a', FilterOperators::EQUAL, 'test1')) + ) + ); + $result = $response->result(); + $this->assertNotNull($result); + $this->assertEquals(1, count($result)); + } + + public function testInvalidCondition(): void + { + $this->expectException(FilterLogicException::class); + new LogicOperatorCondition( + 'e', + FilterOperators::GREATER_THAN, + '2020-01-09', + modifier: OperatorModifier::ALL, + language: 'english' + ); + } } From 5ba0f6c07a3188facd71ee18341c2a25e5105c7d Mon Sep 17 00:00:00 2001 From: Kirill Fuks Date: Wed, 26 Jul 2023 19:17:25 +0200 Subject: [PATCH 3/4] chore: document complex logic conditions and add new feature to changelog --- CHANGELOG.rst | 32 ++++++++++++++++++++++++++- docs/guides/request-builder/index.rst | 22 ++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index db6fa98..573f3de 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,7 +13,37 @@ Versioning `__. :depth: 1 `[Unreleased] `_ ------------------------------------------------------------ +----------------------------------------------------------------- + +Added +~~~~~ + +- Support for complex logic conditions using and/or (`#4 `_) + +Changed +~~~~~~~ + +- n/a + +Deprecated +~~~~~~~~~~ + +- n/a + +Removed +~~~~~~~ + +- n/a + +Fixed +~~~~~ + +- n/a + +Security +~~~~~~~~ + +- n/a `[0.0.1] `_ - 2023-07-24 ------------------------------------------------------------------------ diff --git a/docs/guides/request-builder/index.rst b/docs/guides/request-builder/index.rst index 496a920..4cb59bb 100644 --- a/docs/guides/request-builder/index.rst +++ b/docs/guides/request-builder/index.rst @@ -31,6 +31,28 @@ pass it to the ``run()`` method of the client. $response = $client->run($query); +Complex logic conditions +~~~~~~~~~~~~~~~~~~~~~~~~ + +The ``PostgrestRequestBuilder`` supports complex logic conditions using ``and``/ ``or``. +Unfortunately, when using ``and``/ ``or`` the ``PostgrestRequestBuilder`` will not be able to, +escape the values for you. You will have to escape the values yourself. + +.. code:: php + + $query = $client->from('schema_name', 'table_name') + ->select('*') + ->or( + (new LogicOperatorCondition('a', FilterOperator::EQUAL, 42)), + (new LogicOperatorCondition('b', FilterOperator::LESS_THAN, 2.0, negate: true)), + // escape strings yourself + (new LogicOperatorCondition('c', FilterOperator::IN, '("foo bar",bar)')), + ); + +Nested complex logic conditions are not supported using the LogicOperatorCondition class. +You will need to build the string yourself. You can implement your own logic condition class +which implements the ``Stringable`` interface, as the functions ``or()`` and ``and()`` accept this interface. + Exceptions ---------- From 9769e0e33b38430e1943bcb8951d00b63e7fc513 Mon Sep 17 00:00:00 2001 From: Kirill Fuks Date: Wed, 26 Jul 2023 19:21:09 +0200 Subject: [PATCH 4/4] fix(ci): run codecov report generation on pr against master --- .github/workflows/create-coverage-report.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/create-coverage-report.yml b/.github/workflows/create-coverage-report.yml index 056ce07..dde1094 100644 --- a/.github/workflows/create-coverage-report.yml +++ b/.github/workflows/create-coverage-report.yml @@ -13,7 +13,7 @@ on: - "**" pull_request: branches: - - "main" + - "master" paths: - "src/**.php" - "tests/**.php"