Skip to content

Commit 7c12a43

Browse files
committed
Merge branch 'feature/compound-filters' of https://github.com/5am-code/laravel-notion-api into feature/compound-filters
2 parents 4802f81 + 5d1f3dc commit 7c12a43

File tree

5 files changed

+53
-57
lines changed

5 files changed

+53
-57
lines changed

src/Endpoints/Database.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class Database extends Endpoint
3838
/**
3939
* Database constructor.
4040
*
41-
* @param string $databaseId
42-
* @param Notion $notion
41+
* @param string $databaseId
42+
* @param Notion $notion
4343
*
4444
* @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException
4545
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
@@ -67,10 +67,9 @@ public function query(): PageCollection
6767
$postData['sorts'] = Sorting::sortQuery($this->sorts);
6868
}
6969

70-
if($this->filter !== null && !is_null($this->filterBag)) {
71-
throw new HandlingException("Please provide either a filter bag or a single filter.");
72-
}
73-
elseif ($this->filter !== null || !is_null($this->filterBag)) {
70+
if ($this->filter !== null && ! is_null($this->filterBag)) {
71+
throw new HandlingException('Please provide either a filter bag or a single filter.');
72+
} elseif ($this->filter !== null || ! is_null($this->filterBag)) {
7473
$postData['filter'] = $this->filterData;
7574
}
7675

@@ -84,7 +83,7 @@ public function query(): PageCollection
8483

8584
$response = $this
8685
->post(
87-
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
86+
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
8887
$postData
8988
)
9089
->json();
@@ -95,17 +94,19 @@ public function query(): PageCollection
9594
/**
9695
* @param $filter
9796
* @return Database $this
97+
*
9898
* @throws HandlingException
99+
*
99100
* @todo As soon as this package drops PHP 7.4 support, we can use union types here (FilterBag and Filter)
100101
*/
101102
public function filterBy($filter): Database // TODO that's a breaking change
102103
{
103104
$this->checkFilterType($filter);
104105

105-
if($filter instanceof FilterBag) {
106+
if ($filter instanceof FilterBag) {
106107
return $this->filterByBag($filter);
107108
}
108-
if($filter instanceof Filter) {
109+
if ($filter instanceof Filter) {
109110
return $this->filterBySingleFilter($filter);
110111
}
111112

@@ -115,13 +116,13 @@ public function filterBy($filter): Database // TODO that's a breaking change
115116
public function filterBySingleFilter(Filter $filter): Database
116117
{
117118
$this->filter = $filter;
118-
$this->filterData = ["or" => [$filter->toQuery()]];
119+
$this->filterData = ['or' => [$filter->toQuery()]];
119120

120121
return $this;
121122
}
122123

123124
/**
124-
* @param FilterBag $filterBag
125+
* @param FilterBag $filterBag
125126
* @return $this
126127
*/
127128
public function filterByBag(FilterBag $filterBag): Database
@@ -133,7 +134,7 @@ public function filterByBag(FilterBag $filterBag): Database
133134
}
134135

135136
/**
136-
* @param Collection $sorts
137+
* @param Collection $sorts
137138
* @return $this
138139
*/
139140
public function sortBy(Collection $sorts): Database
@@ -156,8 +157,8 @@ public function offsetByResponse(EntityCollection $entityCollection): Database
156157

157158
private function checkFilterType($filter): void
158159
{
159-
if (!($filter instanceof Filter || $filter instanceof FilterBag)) {
160-
throw new HandlingException("Please provide either a filter bag or a single filter.");
160+
if (! ($filter instanceof Filter || $filter instanceof FilterBag)) {
161+
throw new HandlingException('Please provide either a filter bag or a single filter.');
161162
}
162163
}
163164
}

src/Query/Filters/FilterBag.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
use Throwable;
99

1010
/**
11-
* Class FilterBag
11+
* Class FilterBag.
1212
*/
1313
class FilterBag extends QueryHelper
1414
{
15-
1615
/**
1716
* @var string|mixed
1817
*/
19-
protected string $operator = "and"; // TODO shortcut instances + type checking + pretty operators
18+
protected string $operator = 'and'; // TODO shortcut instances + type checking + pretty operators
2019

2120
/**
2221
* @var Collection
@@ -28,15 +27,14 @@ class FilterBag extends QueryHelper
2827
*/
2928
public ?FilterBag $parentFilterBag = null;
3029

31-
3230
/**
3331
* Creates a FilterBag instance with an "or" operator.
3432
*
3533
* @return FilterBag
3634
*/
3735
public static function or(): FilterBag
3836
{
39-
return new FilterBag("or");
37+
return new FilterBag('or');
4038
}
4139

4240
/**
@@ -46,26 +44,26 @@ public static function or(): FilterBag
4644
*/
4745
public static function and(): FilterBag
4846
{
49-
return new FilterBag("and");
47+
return new FilterBag('and');
5048
}
5149

5250
/**
53-
* @param string $operator
51+
* @param string $operator
5452
*/
55-
public function __construct(string $operator = "and")
53+
public function __construct(string $operator = 'and')
5654
{
5755
$this->isValidOperator($operator);
5856

5957
$this->content = new Collection;
6058
$this->operator = $operator;
6159
}
6260

63-
6461
/**
65-
* @param Filter $filter
62+
* @param Filter $filter
6663
* @return $this
6764
*/
68-
public function addFilter(Filter $filter): self {
65+
public function addFilter(Filter $filter): self
66+
{
6967
$this->content->add($filter);
7068

7169
return $this;
@@ -74,13 +72,14 @@ public function addFilter(Filter $filter): self {
7472
/**
7573
* @throws HandlingException|Throwable
7674
*/
77-
public function addFilterBag(FilterBag $filterBag): self {
75+
public function addFilterBag(FilterBag $filterBag): self
76+
{
7877
// A filter bag can only be added to another filter bag if it does not have a parent yet and does not
7978
// contain any other filter bags.
80-
throw_if($this->parentFilterBag !== null, new HandlingException("The maximum nesting level of compound filters must not exceed 2."));
79+
throw_if($this->parentFilterBag !== null, new HandlingException('The maximum nesting level of compound filters must not exceed 2.'));
8180

8281
$filterBag->content->each(function ($bag) {
83-
throw_if($bag instanceof FilterBag, new HandlingException("The maximum nesting level of compound filters must not exceed 2."));
82+
throw_if($bag instanceof FilterBag, new HandlingException('The maximum nesting level of compound filters must not exceed 2.'));
8483
});
8584

8685
$filterBag->parentFilterBag = $this;
@@ -92,25 +91,24 @@ public function addFilterBag(FilterBag $filterBag): self {
9291
/**
9392
* @return array
9493
*/
95-
public function toQuery() {
96-
97-
$filters = $this->content->map(function($set) {
94+
public function toQuery()
95+
{
96+
$filters = $this->content->map(function ($set) {
9897
return $set->toQuery();
9998
})->toArray();
10099

101100
return [
102-
$this->operator => $filters
101+
$this->operator => $filters,
103102
];
104103
}
105104

106-
private function isValidOperator($operator) {
107-
$validOperators = ["and", "or"];
105+
private function isValidOperator($operator)
106+
{
107+
$validOperators = ['and', 'or'];
108108

109109
throw_if(
110-
!in_array($operator, $validOperators),
111-
new HandlingException("Invalid operator for FilterBag: " . $operator)
110+
! in_array($operator, $validOperators),
111+
new HandlingException('Invalid operator for FilterBag: '.$operator)
112112
);
113113
}
114-
115-
116-
}
114+
}

tests/FilterBagTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
4-
use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
54
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
5+
use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
66
use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
77

88
it('creates a FilterBag with an "or" operator with the instance method', function () {
@@ -25,45 +25,44 @@
2525
$this->assertArrayHasKey('and', $queryFilter);
2626
});
2727

28-
it('throws an exception when providing an invalid operator', function() {
28+
it('throws an exception when providing an invalid operator', function () {
2929
$this->expectException(HandlingException::class);
3030
$this->expectExceptionMessage('Invalid operator for FilterBag: invalid');
3131

3232
new FilterBag('invalid');
3333
});
3434

35-
it('only allows the nesting of FilterBags up to two levels', function() {
36-
35+
it('only allows the nesting of FilterBags up to two levels', function () {
3736
$this->expectException(HandlingException::class);
3837
$this->expectExceptionMessage('The maximum nesting level of compound filters must not exceed 2.');
3938

40-
$filterBag = new FilterBag("and");
39+
$filterBag = new FilterBag('and');
4140

4241
$filterBag->addFilter(
43-
Filter::rawFilter("Known for", [
44-
"multi_select" => ["contains" => "UNIVAC"],
42+
Filter::rawFilter('Known for', [
43+
'multi_select' => ['contains' => 'UNIVAC'],
4544
])
4645
);
4746

48-
$nameFilterBag = new FilterBag("or");
47+
$nameFilterBag = new FilterBag('or');
4948
$nameFilterBag
50-
->addFilter(Filter::textFilter("Name", Operators::CONTAINS, "Grace"))
51-
->addFilter(Filter::textFilter("Name", Operators::CONTAINS, "Jean"));
49+
->addFilter(Filter::textFilter('Name', Operators::CONTAINS, 'Grace'))
50+
->addFilter(Filter::textFilter('Name', Operators::CONTAINS, 'Jean'));
5251

5352
$anotherBag = new FilterBag();
5453
$nameFilterBag->addFilterBag($anotherBag);
5554

5655
$filterBag->addFilterBag($nameFilterBag);
5756
});
5857

59-
it('allows the nesting of multiple FilterBags inside one FilterBag', function() {
58+
it('allows the nesting of multiple FilterBags inside one FilterBag', function () {
6059
// TODO
6160
});
6261

63-
it('creates the correct query structure for a nested FilterBag', function() {
62+
it('creates the correct query structure for a nested FilterBag', function () {
6463
// TODO
6564
});
6665

67-
it('creates the correct query structure for a FilterBag with one level', function() {
66+
it('creates the correct query structure for a FilterBag with one level', function () {
6867
// TODO
6968
});

tests/FilterTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
54
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
65
use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
@@ -13,10 +12,9 @@
1312
$this->assertEquals('Name', $filter->toQuery()['property']);
1413
$this->assertArrayHasKey('text', $filter->toQuery());
1514
$this->assertArrayHasKey('equals', $filter->toQuery()['text']);
16-
$this->assertEquals('Ada Lovelace', $filter->toQuery()['text']['equals']);#
15+
$this->assertEquals('Ada Lovelace', $filter->toQuery()['text']['equals']); //
1716
});
1817

19-
2018
it('creates a number filter with the given data', function () {
2119
$filter = Filter::numberFilter('Awesomeness Level', Operators::GREATER_THAN_OR_EQUAL_TO, 9000);
2220

@@ -40,4 +38,4 @@
4038
$this->expectException(HandlingException::class);
4139
$this->expectExceptionMessage('Invalid filter definition.');
4240
$filter->toArray();
43-
});
41+
});

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
use FiveamCode\LaravelNotionApi\Tests\TestCase;
44

5-
uses(TestCase::class)->in(__DIR__);
5+
uses(TestCase::class)->in(__DIR__);

0 commit comments

Comments
 (0)