Skip to content

Commit

Permalink
🔀 Merge pull request #20 from Morning-Train/hotfix/filters
Browse files Browse the repository at this point in the history
♻️ Filters
  • Loading branch information
mschadegg authored Feb 29, 2024
2 parents d362324 + 0f574ab commit 91d030f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
61 changes: 57 additions & 4 deletions src/Classes/EconomicQueryFilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class EconomicQueryFilterBuilder

const FILTER_VALUE_NULL = '$null:';

const ESCAPES = [
'$' => '$$',
'(' => '$(',
')' => '$)',
'*' => '$*',
',' => '$,',
'[' => '$[',
']' => '$]',
];

protected array $filters = [];

public function __construct(protected string $relation)
Expand Down Expand Up @@ -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)
{

Expand All @@ -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,
];

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Resources/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 91d030f

Please sign in to comment.