Skip to content

Commit be7a946

Browse files
authored
Merge pull request #5 from keboola/pepa_PS-2536_filterOperator
PS-2536 Support for comparison operators in subscription filters
2 parents 412ce35 + 0b58cb1 commit be7a946

5 files changed

Lines changed: 82 additions & 6 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"php": "^7.4",
2929
"ext-json": "*",
3030
"guzzlehttp/guzzle": "^6.3|^7.2",
31+
"myclabs/php-enum": "^1.8",
3132
"psr/log": "^1.1",
3233
"symfony/validator": "^5.2"
3334
},

src/Requests/PostSubscription/Filter.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ class Filter implements JsonSerializable
1010
{
1111
private string $field;
1212
private string $value;
13+
private ?FilterOperator $operator;
1314

14-
public function __construct(string $field, string $value)
15+
public function __construct(string $field, string $value, ?FilterOperator $operator = null)
1516
{
1617
$this->field = $field;
1718
$this->value = $value;
19+
$this->operator = $operator;
1820
}
1921

20-
/**
21-
* @return mixed
22-
*/
23-
public function jsonSerialize()
22+
public function jsonSerialize(): array
2423
{
25-
return [
24+
$data = [
2625
'field' => $this->field,
2726
'value' => $this->value,
2827
];
28+
29+
if ($this->operator !== null) {
30+
$data['operator'] = $this->operator->getValue();
31+
}
32+
33+
return $data;
2934
}
3035
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostSubscription;
6+
7+
use DomainException;
8+
use MyCLabs\Enum\Enum;
9+
10+
/**
11+
* @method static self EQUAL()
12+
* @method static self LESS_THAN()
13+
* @method static self GREATER_THAN()
14+
* @method static self LESS_THAN_OR_EQUAL()
15+
* @method static self GREATER_THAN_OR_EQUAL()
16+
*
17+
* @phpstan-extends Enum<string>
18+
*/
19+
class FilterOperator extends Enum
20+
{
21+
// phpcs:disable SlevomatCodingStandard.Classes.UnusedPrivateElements
22+
private const EQUAL = '=='; // can't be called EQUALS because there is a method called `equals` on base Enum class
23+
private const LESS_THAN = '<';
24+
private const GREATER_THAN = '>';
25+
private const LESS_THAN_OR_EQUAL = '<=';
26+
private const GREATER_THAN_OR_EQUAL = '>=';
27+
// phpcs:enable SlevomatCodingStandard.Classes.UnusedPrivateElements
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Tests\Requests\PostSubscription;
6+
7+
use Keboola\NotificationClient\Requests\PostSubscription\FilterOperator;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class FilterOperatorTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider provideOperatorValues
14+
*/
15+
public function testOperatorValues(FilterOperator $operator, string $expectedValue): void
16+
{
17+
self::assertSame($expectedValue, $operator->getValue());
18+
}
19+
20+
public function provideOperatorValues(): iterable
21+
{
22+
yield 'equals' => [FilterOperator::EQUAL(), '=='];
23+
yield 'less than' => [FilterOperator::LESS_THAN(), '<'];
24+
yield 'greater than' => [FilterOperator::GREATER_THAN(), '>'];
25+
yield 'less than or equals' => [FilterOperator::LESS_THAN_OR_EQUAL(), '<='];
26+
yield 'greater than or equals' => [FilterOperator::GREATER_THAN_OR_EQUAL(), '>='];
27+
}
28+
}

tests/Requests/PostSubscription/FilterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Keboola\NotificationClient\Tests\Requests\PostSubscription;
66

77
use Keboola\NotificationClient\Requests\PostSubscription\Filter;
8+
use Keboola\NotificationClient\Requests\PostSubscription\FilterOperator;
89
use PHPUnit\Framework\TestCase;
910

1011
class FilterTest extends TestCase
@@ -20,4 +21,17 @@ public function testJsonSerialize(): void
2021
$filter->jsonSerialize()
2122
);
2223
}
24+
25+
public function testJsonSerializeWithOperator(): void
26+
{
27+
$filter = new Filter('someName', 'someValue', FilterOperator::GREATER_THAN_OR_EQUAL());
28+
self::assertSame(
29+
[
30+
'field' => 'someName',
31+
'value' => 'someValue',
32+
'operator' => '>=',
33+
],
34+
$filter->jsonSerialize()
35+
);
36+
}
2337
}

0 commit comments

Comments
 (0)