From c79b00b70b492247a76a0686a59d3df5370f0b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schadegg=20Br=C3=B8nniche?= Date: Thu, 29 Feb 2024 13:09:53 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Changed=20how=20filter?= =?UTF-8?q?s=20is=20parsed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Classes/EconomicQueryFilterBuilder.php | 61 ++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/src/Classes/EconomicQueryFilterBuilder.php b/src/Classes/EconomicQueryFilterBuilder.php index 658156b..85d2877 100644 --- a/src/Classes/EconomicQueryFilterBuilder.php +++ b/src/Classes/EconomicQueryFilterBuilder.php @@ -30,6 +30,16 @@ class EconomicQueryFilterBuilder const FILTER_VALUE_NULL = '$null:'; + const ESCAPES = [ + '$' => '$$', + '(' => '$(', + ')' => '$)', + '*' => '$*', + ',' => '$,', + '[' => '$[', + ']' => '$]', + ]; + protected array $filters = []; public function __construct(protected string $relation) @@ -65,6 +75,33 @@ protected function convertOperator(string $operator): string return $operator; } + protected static function isOperator(string $operator): bool + { + return in_array($operator, [ + '=', + '!=', + '>', + '>=', + '<', + '<=', + 'LIKE', + 'like', + 'IN', + 'in', + 'NOT IN', + 'not in', + static::FILTER_OPERATOR_EQUAL, + static::FILTER_OPERATOR_NOT_EQUAL, + static::FILTER_OPERATOR_GREATER_THAN, + static::FILTER_OPERATOR_GREATER_THAN_OR_EQUAL, + static::FILTER_OPERATOR_LESS_THAN, + static::FILTER_OPERATOR_LESS_THAN_OR_EQUAL, + static::FILTER_OPERATOR_LIKE, + static::FILTER_OPERATOR_IN, + static::FILTER_OPERATOR_NOT_IN, + ]); + } + protected function whereNested(Closure $closure) { @@ -79,12 +116,28 @@ public function where(int|string|Closure $propertyName, ?string $operatorOrValue return $this->whereNested($propertyName); } - $operator = is_null($value) ? static::FILTER_OPERATOR_EQUAL : $operatorOrValue; - $value = is_null($value) ? $operatorOrValue : $value; + $operator = !static::isOperator($operatorOrValue) ? static::FILTER_OPERATOR_EQUAL : $operatorOrValue; + $value = is_null($value) && !static::isOperator($operatorOrValue) ? $operatorOrValue : $value; + + if(is_string($value)) { + $value = str_replace(array_keys(static::ESCAPES), array_values(static::ESCAPES), $value); + } + + if($value === null) { + $value = static::FILTER_VALUE_NULL; + } + + if($value === true) { + $value = 'true'; + } + + if($value === false) { + $value = 'false'; + } $this->filters[] = [ 'property' => $propertyName, - 'operator' => $operator, + 'operator' => $this->convertOperator($operator), 'value' => $value, ]; @@ -119,7 +172,7 @@ public function buildString(): string $string .= static::FILTER_RELATION_AND; } - $string .= $filter['property'].$this->convertOperator($filter['operator']).$filter['value']; + $string .= $filter['property'] . $filter['operator'] . $filter['value']; $strings[] = $string; } From bc9c8f09489ac9809efd6618215fb5bafcd108af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schadegg=20Br=C3=B8nniche?= Date: Thu, 29 Feb 2024 13:10:13 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Add=20201=20as=20allow?= =?UTF-8?q?ed=20status=20for=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Traits/Resources/Updatable.php | 4 ++-- tests/Unit/CustomerTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Traits/Resources/Updatable.php b/src/Traits/Resources/Updatable.php index 136ae9d..4162356 100644 --- a/src/Traits/Resources/Updatable.php +++ b/src/Traits/Resources/Updatable.php @@ -17,8 +17,8 @@ public function save(): static $response = EconomicApiService::put(static::getEndpoint(Update::class, $this), $this->toArray(true)); - if ($response->getStatusCode() !== 200) { - EconomicLoggerService::error('Economic API Service returned a non 200 status code when updating a resource', [ + if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { // 201 is for created, 200 is for updated -> both are valid here + EconomicLoggerService::error('Economic API Service returned a non 200 or 201 status code when updating a resource', [ 'status_code' => $response->getStatusCode(), 'response_body' => $response->getBody(), 'resource' => static::class, diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php index 7f5b6ad..95f7d3b 100644 --- a/tests/Unit/CustomerTest.php +++ b/tests/Unit/CustomerTest.php @@ -102,6 +102,32 @@ ->name->toBe('John Doe Renamed'); }); +it('can create a customer through update', function () { + $this->driver->expects()->put() + ->withArgs(function (string $url, array $body) { + return $url === 'https://restapi.e-conomic.com/customers/1' + && $body === [ + 'customerNumber' => 1, + 'name' => 'John Doe Renamed', + 'self' => 'https://restapi.e-conomic.com/customers/1', + ]; + }) + ->once() + ->andReturn(new EconomicResponse(201, fixture('Customers/update'))); + + $customer = new Customer([ + 'customerNumber' => 1, + 'name' => 'John Doe Renamed', + ]); + + $updatedCustomer = $customer->save(); + + expect($updatedCustomer) + ->toBeInstanceOf(Customer::class) + ->customerNumber->toBe(1) + ->name->toBe('John Doe Renamed'); +}); + it('filters null values', function () { $this->driver->expects()->post( 'https://restapi.e-conomic.com/customers', From 0f574abd9f5f338bb408956163230ab12fd1bb6a Mon Sep 17 00:00:00 2001 From: mschadegg Date: Thu, 29 Feb 2024 12:10:38 +0000 Subject: [PATCH 3/3] Fix styling --- src/Classes/EconomicQueryFilterBuilder.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Classes/EconomicQueryFilterBuilder.php b/src/Classes/EconomicQueryFilterBuilder.php index 85d2877..5d6d556 100644 --- a/src/Classes/EconomicQueryFilterBuilder.php +++ b/src/Classes/EconomicQueryFilterBuilder.php @@ -116,22 +116,22 @@ public function where(int|string|Closure $propertyName, ?string $operatorOrValue return $this->whereNested($propertyName); } - $operator = !static::isOperator($operatorOrValue) ? static::FILTER_OPERATOR_EQUAL : $operatorOrValue; - $value = is_null($value) && !static::isOperator($operatorOrValue) ? $operatorOrValue : $value; + $operator = ! static::isOperator($operatorOrValue) ? static::FILTER_OPERATOR_EQUAL : $operatorOrValue; + $value = is_null($value) && ! static::isOperator($operatorOrValue) ? $operatorOrValue : $value; - if(is_string($value)) { + if (is_string($value)) { $value = str_replace(array_keys(static::ESCAPES), array_values(static::ESCAPES), $value); } - if($value === null) { + if ($value === null) { $value = static::FILTER_VALUE_NULL; } - if($value === true) { + if ($value === true) { $value = 'true'; } - if($value === false) { + if ($value === false) { $value = 'false'; } @@ -172,7 +172,7 @@ public function buildString(): string $string .= static::FILTER_RELATION_AND; } - $string .= $filter['property'] . $filter['operator'] . $filter['value']; + $string .= $filter['property'].$filter['operator'].$filter['value']; $strings[] = $string; }