Add "in the last" parameter and schema to number and enum filters
[PREMIUM-253]
This commit is contained in:
@@ -26,16 +26,33 @@ class EnumArrayFilter implements Filter {
|
||||
}
|
||||
|
||||
public function getArgsSchema(string $condition): ObjectSchema {
|
||||
$paramsSchema = Builder::object([
|
||||
'in_the_last' => Builder::object([
|
||||
'number' => Builder::integer()->required()->minimum(1),
|
||||
'unit' => Builder::string()->required()->pattern('^(days)$')->default('days'),
|
||||
]),
|
||||
]);
|
||||
|
||||
return Builder::object([
|
||||
'value' => Builder::oneOf([
|
||||
Builder::array(Builder::string())->minItems(1),
|
||||
Builder::array(Builder::integer())->minItems(1),
|
||||
])->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getFieldParams(FilterData $data): array {
|
||||
return [];
|
||||
$paramData = $data->getArgs()['params'] ?? [];
|
||||
$params = [];
|
||||
|
||||
$inTheLastUnit = $paramData['in_the_last']['unit'] ?? null;
|
||||
$inTheLastNumber = $paramData['in_the_last']['number'] ?? null;
|
||||
if ($inTheLastUnit === 'days' && $inTheLastNumber !== null) {
|
||||
$params['in_the_last_seconds'] = $inTheLastNumber * DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function matches(FilterData $data, $value): bool {
|
||||
|
@@ -24,16 +24,33 @@ class EnumFilter implements Filter {
|
||||
}
|
||||
|
||||
public function getArgsSchema(string $condition): ObjectSchema {
|
||||
$paramsSchema = Builder::object([
|
||||
'in_the_last' => Builder::object([
|
||||
'number' => Builder::integer()->required()->minimum(1),
|
||||
'unit' => Builder::string()->required()->pattern('^(days)$')->default('days'),
|
||||
]),
|
||||
]);
|
||||
|
||||
return Builder::object([
|
||||
'value' => Builder::oneOf([
|
||||
Builder::array(Builder::string())->minItems(1),
|
||||
Builder::array(Builder::integer())->minItems(1),
|
||||
])->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getFieldParams(FilterData $data): array {
|
||||
return [];
|
||||
$paramData = $data->getArgs()['params'] ?? [];
|
||||
$params = [];
|
||||
|
||||
$inTheLastUnit = $paramData['in_the_last']['unit'] ?? null;
|
||||
$inTheLastNumber = $paramData['in_the_last']['number'] ?? null;
|
||||
if ($inTheLastUnit === 'days' && $inTheLastNumber !== null) {
|
||||
$params['in_the_last_seconds'] = $inTheLastNumber * DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function matches(FilterData $data, $value): bool {
|
||||
|
@@ -13,22 +13,44 @@ class IntegerFilter extends NumberFilter {
|
||||
}
|
||||
|
||||
public function getArgsSchema(string $condition): ObjectSchema {
|
||||
$paramsSchema = Builder::object([
|
||||
'in_the_last' => Builder::object([
|
||||
'number' => Builder::integer()->required()->minimum(1),
|
||||
'unit' => Builder::string()->required()->pattern('^(days)$')->default('days'),
|
||||
]),
|
||||
]);
|
||||
|
||||
switch ($condition) {
|
||||
case self::CONDITION_BETWEEN:
|
||||
case self::CONDITION_NOT_BETWEEN:
|
||||
return Builder::object([
|
||||
'value' => Builder::array(Builder::integer())->minItems(2)->maxItems(2)->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
case self::CONDITION_IS_SET:
|
||||
case self::CONDITION_IS_NOT_SET:
|
||||
return Builder::object([]);
|
||||
return Builder::object([
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
default:
|
||||
return Builder::object(['value' => Builder::integer()->required()]);
|
||||
return Builder::object([
|
||||
'value' => Builder::integer()->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFieldParams(FilterData $data): array {
|
||||
return [];
|
||||
$paramData = $data->getArgs()['params'] ?? [];
|
||||
$params = [];
|
||||
|
||||
$inTheLastUnit = $paramData['in_the_last']['unit'] ?? null;
|
||||
$inTheLastNumber = $paramData['in_the_last']['number'] ?? null;
|
||||
if ($inTheLastUnit === 'days' && $inTheLastNumber !== null) {
|
||||
$params['in_the_last_seconds'] = $inTheLastNumber * DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function matches(FilterData $data, $value): bool {
|
||||
|
@@ -40,22 +40,44 @@ class NumberFilter implements Filter {
|
||||
}
|
||||
|
||||
public function getArgsSchema(string $condition): ObjectSchema {
|
||||
$paramsSchema = Builder::object([
|
||||
'in_the_last' => Builder::object([
|
||||
'number' => Builder::integer()->required()->minimum(1),
|
||||
'unit' => Builder::string()->required()->pattern('^(days)$')->default('days'),
|
||||
]),
|
||||
]);
|
||||
|
||||
switch ($condition) {
|
||||
case self::CONDITION_BETWEEN:
|
||||
case self::CONDITION_NOT_BETWEEN:
|
||||
return Builder::object([
|
||||
'value' => Builder::array(Builder::number())->minItems(2)->maxItems(2)->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
case self::CONDITION_IS_SET:
|
||||
case self::CONDITION_IS_NOT_SET:
|
||||
return Builder::object([]);
|
||||
return Builder::object([
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
default:
|
||||
return Builder::object(['value' => Builder::number()->required()]);
|
||||
return Builder::object([
|
||||
'value' => Builder::number()->required(),
|
||||
'params' => $paramsSchema,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFieldParams(FilterData $data): array {
|
||||
return [];
|
||||
$paramData = $data->getArgs()['params'] ?? [];
|
||||
$params = [];
|
||||
|
||||
$inTheLastUnit = $paramData['in_the_last']['unit'] ?? null;
|
||||
$inTheLastNumber = $paramData['in_the_last']['number'] ?? null;
|
||||
if ($inTheLastUnit === 'days' && $inTheLastNumber !== null) {
|
||||
$params['in_the_last_seconds'] = $inTheLastNumber * DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -18,6 +18,19 @@ class EnumArrayFilterTest extends MailPoetUnitTest {
|
||||
'matches-none-of' => 'matches none of',
|
||||
], $filter->getConditions());
|
||||
|
||||
$paramsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'in_the_last' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'number' => ['type' => 'integer', 'required' => true, 'minimum' => 1],
|
||||
'unit' => ['type' => 'string', 'required' => true, 'pattern' => '^(days)$', 'default' => 'days'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$argsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
@@ -28,6 +41,7 @@ class EnumArrayFilterTest extends MailPoetUnitTest {
|
||||
],
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
@@ -96,6 +110,19 @@ class EnumArrayFilterTest extends MailPoetUnitTest {
|
||||
$this->assertNotMatches('unknown', [1, 2, 3], [1]);
|
||||
}
|
||||
|
||||
public function testFieldParams(): void {
|
||||
if (!defined('DAY_IN_SECONDS')) {
|
||||
define('DAY_IN_SECONDS', 24 * 60 * 60);
|
||||
}
|
||||
|
||||
$filter = new EnumArrayFilter();
|
||||
$params = ['in_the_last' => ['number' => 123, 'unit' => 'days']];
|
||||
$this->assertSame(
|
||||
['in_the_last' => 123 * DAY_IN_SECONDS],
|
||||
$filter->getFieldParams(new Filter('f', 'integer', '', 'equals', ['params' => $params]))
|
||||
);
|
||||
}
|
||||
|
||||
private function assertMatches(string $condition, $filterValue, $value): void {
|
||||
$this->assertTrue($this->matchesFilter($condition, $filterValue, $value));
|
||||
}
|
||||
|
@@ -17,6 +17,19 @@ class EnumFilterTest extends MailPoetUnitTest {
|
||||
'is-none-of' => 'is none of',
|
||||
], $filter->getConditions());
|
||||
|
||||
$paramsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'in_the_last' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'number' => ['type' => 'integer', 'required' => true, 'minimum' => 1],
|
||||
'unit' => ['type' => 'string', 'required' => true, 'pattern' => '^(days)$', 'default' => 'days'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$argsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
@@ -27,6 +40,7 @@ class EnumFilterTest extends MailPoetUnitTest {
|
||||
],
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
@@ -81,6 +95,19 @@ class EnumFilterTest extends MailPoetUnitTest {
|
||||
$this->assertNotMatches('unknown', [1, 2, 3], 1);
|
||||
}
|
||||
|
||||
public function testFieldParams(): void {
|
||||
if (!defined('DAY_IN_SECONDS')) {
|
||||
define('DAY_IN_SECONDS', 24 * 60 * 60);
|
||||
}
|
||||
|
||||
$filter = new EnumFilter();
|
||||
$params = ['in_the_last' => ['number' => 123, 'unit' => 'days']];
|
||||
$this->assertSame(
|
||||
['in_the_last' => 123 * DAY_IN_SECONDS],
|
||||
$filter->getFieldParams(new Filter('f', 'integer', '', 'equals', ['params' => $params]))
|
||||
);
|
||||
}
|
||||
|
||||
private function assertMatches(string $condition, $filterValue, $value): void {
|
||||
$this->assertTrue($this->matchesFilter($condition, $filterValue, $value));
|
||||
}
|
||||
|
@@ -24,6 +24,19 @@ class IntegerFilterTest extends MailPoetUnitTest {
|
||||
'is-not-set' => 'is not set',
|
||||
], $filter->getConditions());
|
||||
|
||||
$paramsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'in_the_last' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'number' => ['type' => 'integer', 'required' => true, 'minimum' => 1],
|
||||
'unit' => ['type' => 'string', 'required' => true, 'pattern' => '^(days)$', 'default' => 'days'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$singleValueArgsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
@@ -31,6 +44,7 @@ class IntegerFilterTest extends MailPoetUnitTest {
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
@@ -44,10 +58,16 @@ class IntegerFilterTest extends MailPoetUnitTest {
|
||||
'maxItems' => 2,
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
$emptyArgsSchema = ['type' => 'object', 'properties' => []];
|
||||
$emptyArgsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($singleValueArgsSchema, $filter->getArgsSchema('equals')->toArray());
|
||||
$this->assertSame($singleValueArgsSchema, $filter->getArgsSchema('not-equals')->toArray());
|
||||
@@ -283,6 +303,19 @@ class IntegerFilterTest extends MailPoetUnitTest {
|
||||
$this->assertNotMatches('is-set', null, 0.5);
|
||||
}
|
||||
|
||||
public function testFieldParams(): void {
|
||||
if (!defined('DAY_IN_SECONDS')) {
|
||||
define('DAY_IN_SECONDS', 24 * 60 * 60);
|
||||
}
|
||||
|
||||
$filter = new IntegerFilter();
|
||||
$params = ['in_the_last' => ['number' => 123, 'unit' => 'days']];
|
||||
$this->assertSame(
|
||||
['in_the_last' => 123 * DAY_IN_SECONDS],
|
||||
$filter->getFieldParams(new Filter('f', 'integer', '', 'equals', ['params' => $params]))
|
||||
);
|
||||
}
|
||||
|
||||
private function assertMatches(string $condition, $filterValue, $value): void {
|
||||
$this->assertTrue($this->matchesFilter($condition, $filterValue, $value));
|
||||
}
|
||||
|
@@ -24,6 +24,19 @@ class NumberFilterTest extends MailPoetUnitTest {
|
||||
'is-not-set' => 'is not set',
|
||||
], $filter->getConditions());
|
||||
|
||||
$paramsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'in_the_last' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'number' => ['type' => 'integer', 'required' => true, 'minimum' => 1],
|
||||
'unit' => ['type' => 'string', 'required' => true, 'pattern' => '^(days)$', 'default' => 'days'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$singleValueArgsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
@@ -31,6 +44,7 @@ class NumberFilterTest extends MailPoetUnitTest {
|
||||
'type' => 'number',
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
@@ -44,10 +58,16 @@ class NumberFilterTest extends MailPoetUnitTest {
|
||||
'maxItems' => 2,
|
||||
'required' => true,
|
||||
],
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
$emptyArgsSchema = ['type' => 'object', 'properties' => []];
|
||||
$emptyArgsSchema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'params' => $paramsSchema,
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($singleValueArgsSchema, $filter->getArgsSchema('equals')->toArray());
|
||||
$this->assertSame($singleValueArgsSchema, $filter->getArgsSchema('not-equals')->toArray());
|
||||
@@ -275,6 +295,19 @@ class NumberFilterTest extends MailPoetUnitTest {
|
||||
$this->assertNotMatches('is-not-set', null, -1);
|
||||
}
|
||||
|
||||
public function testFieldParams(): void {
|
||||
if (!defined('DAY_IN_SECONDS')) {
|
||||
define('DAY_IN_SECONDS', 24 * 60 * 60);
|
||||
}
|
||||
|
||||
$filter = new NumberFilter();
|
||||
$params = ['in_the_last' => ['number' => 123, 'unit' => 'days']];
|
||||
$this->assertSame(
|
||||
['in_the_last' => 123 * DAY_IN_SECONDS],
|
||||
$filter->getFieldParams(new Filter('f', 'integer', '', 'equals', ['params' => $params]))
|
||||
);
|
||||
}
|
||||
|
||||
private function assertMatches(string $condition, $filterValue, $value): void {
|
||||
$this->assertTrue($this->matchesFilter($condition, $filterValue, $value));
|
||||
}
|
||||
|
Reference in New Issue
Block a user