Skip to content

Commit 33f7b88

Browse files
committed
Drafting general approach of query filters, moving Filter / Compound Filter into new namespace & adding valid filter operators
1 parent 30c4cec commit 33f7b88

File tree

3 files changed

+103
-7
lines changed

3 files changed

+103
-7
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace FiveamCode\LaravelNotionApi\Query;
3+
namespace FiveamCode\LaravelNotionApi\Query\Filter;
44

55
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6+
use FiveamCode\LaravelNotionApi\Query\QueryHelper;
67

78
/**
89
* Class CompoundFilter
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

3-
namespace FiveamCode\LaravelNotionApi\Query;
3+
namespace FiveamCode\LaravelNotionApi\Query\Filter;
44

5+
use FiveamCode\LaravelNotionApi\Query\Filter\Operators;
6+
use FiveamCode\LaravelNotionApi\Query\QueryHelper;
57
use Illuminate\Support\Collection;
68
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
79

@@ -35,8 +37,8 @@ class Filter extends QueryHelper
3537
public function __construct(
3638
string $property,
3739
string $filterType = null,
38-
array $filterConditions = null,
39-
array $filterDefinition = null
40+
array $filterConditions = null,
41+
array $filterDefinition = null
4042
)
4143
{
4244
parent::__construct();
@@ -53,12 +55,20 @@ public function __construct(
5355
* @see https://developers.notion.com/reference/post-database-query#text-filter-condition
5456
*
5557
* @param string $property
56-
* @param array $filterConditions
58+
* @param string $comparisonOperator
59+
* @param $value
5760
* @return Filter
5861
*/
59-
public static function textFilter(string $property, array $filterConditions): Filter
62+
public static function textFilter(string $property, string $comparisonOperator, string $value): Filter
6063
{
61-
return new Filter($property, 'text', $filterConditions);
64+
self::isValidComparisonOperatorFor('text', $comparisonOperator);
65+
return new Filter($property, 'text', [$comparisonOperator => $value]);
66+
}
67+
68+
public static function numberFilter(string $property, string $comparisonOperator, float|int|double $number): Filter
69+
{
70+
self::isValidComparisonOperatorFor('number', $comparisonOperator);
71+
return new Filter($property, 'number', [$comparisonOperator => $number]);
6272
}
6373

6474
/**
@@ -122,4 +132,13 @@ public static function filterQuery(Collection $filter): array
122132
}
123133

124134

135+
private static function isValidComparisonOperatorFor($filterType, $operator)
136+
{
137+
$validOperators = Operators::getValidComparisonOperators($filterType);
138+
139+
if (!in_array($operator, $validOperators))
140+
throw HandlingException::instance("Invalid comparison operator.", compact("operator"));
141+
}
142+
143+
125144
}

src/Query/Filter/Operators.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Query\Filter;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6+
7+
class Operators
8+
{
9+
# ToDo: Make this a enum with PHP 8.1
10+
const EQUALS = 'equals';
11+
const DOES_NOT_EQUAL = 'does_not_equal';
12+
const CONTAINS = 'contain';
13+
const DOES_NOT_CONTAIN = 'does_not_contain';
14+
const STARTS_WITH = 'starts_with';
15+
const ENDS_WITH = 'ends_with';
16+
const IS_EMPTY = 'is_empty';
17+
const IS_NOT_EMPTY = 'is_not_empty';
18+
const GREATER_THAN = 'greater_than';
19+
const LESS_THAN = 'less_than';
20+
const GREATER_THAN_OR_EQUAL_TO = 'greater_than_or_equal_to';
21+
const LESS_THAN_OR_EQUAL_TO = 'less_than_or_equal_to';
22+
const BEFORE = 'before';
23+
const AFTER = 'after';
24+
const ON_OR_BEFORE = 'on_or_before';
25+
const ON_OR_AFTER = 'on_or_after';
26+
const PAST_WEEK = 'past_week';
27+
const PAST_MONTH = 'past_month';
28+
const PAST_YEAR = 'past_year';
29+
const NEXT_WEEK = 'next_week';
30+
const NEXT_MONTH = 'next_month';
31+
const NEXT_YEAR = 'next_year';
32+
33+
// TODO: Formula filter condition
34+
35+
36+
static function getValidComparisonOperators($filterType)
37+
{
38+
switch ($filterType) {
39+
case 'text':
40+
return Operators::text();
41+
case 'number':
42+
return Operators::number();
43+
default:
44+
throw HandlingException::instance("Invalid filterType.", compact("filterType"));
45+
}
46+
}
47+
48+
private static function text()
49+
{
50+
return [
51+
Operators::EQUALS,
52+
Operators::DOES_NOT_EQUAL,
53+
Operators::CONTAINS,
54+
Operators::DOES_NOT_CONTAIN,
55+
Operators::STARTS_WITH,
56+
Operators::ENDS_WITH,
57+
Operators::IS_EMPTY,
58+
Operators::IS_NOT_EMPTY
59+
];
60+
}
61+
62+
private static function number()
63+
{
64+
return [
65+
Operators::EQUALS,
66+
Operators::DOES_NOT_EQUAL,
67+
Operators::GREATER_THAN,
68+
Operators::LESS_THAN,
69+
Operators::GREATER_THAN_OR_EQUAL_TO,
70+
Operators::LESS_THAN_OR_EQUAL_TO,
71+
Operators::IS_EMPTY,
72+
Operators::IS_NOT_EMPTY
73+
];
74+
}
75+
76+
}

0 commit comments

Comments
 (0)