Skip to content

Commit eb5d16f

Browse files
committed
Merge branch 'dev' into feature/compound-filters
2 parents 456186e + 2f75bbd commit eb5d16f

File tree

7 files changed

+58
-24
lines changed

7 files changed

+58
-24
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
php:
1313
- '8.1'
1414
- '8.0'
15-
- '7.4'
1615
laravel:
1716
- '8.*'
1817
testbench:
@@ -45,4 +44,4 @@ jobs:
4544
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
4645
4746
- name: Execute tests
48-
run: vendor/bin/phpunit tests
47+
run: vendor/bin/pest tests

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.4|^8.0",
28+
"php": "^8.0",
2929
"guzzlehttp/guzzle": "^7.0.1",
3030
"illuminate/support": "^8.0|^9.0"
3131
},

src/Endpoints/Database.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,24 @@ public function filterByBag(FilterBag $filterBag): Database
134134
}
135135

136136
/**
137-
* @param Collection $sorts
138-
* @return $this
137+
* @param Collection|Sorting $sorts
138+
* @return Database $this
139+
*
140+
* @throws HandlingException
139141
*/
140-
public function sortBy(Collection $sorts): Database
142+
public function sortBy(Sorting|Collection $sorts): Database
141143
{
142-
$this->sorts = $sorts;
144+
$sortInstance = get_class($sorts);
145+
switch ($sortInstance) {
146+
case Sorting::class:
147+
$this->sorts->push($sorts);
148+
break;
149+
case Collection::class:
150+
$this->sorts = $sorts;
151+
break;
152+
default:
153+
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings.");
154+
}
143155

144156
return $this;
145157
}

src/Endpoints/Search.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,4 @@ public function filterBy(string $filter): Search
159159

160160
return $this;
161161
}
162-
163-
/**
164-
* @param Sorting $sort
165-
* @return $this
166-
*/
167-
public function sortBy(Sorting $sort): Search
168-
{
169-
$this->sort = $sort;
170-
171-
return $this;
172-
}
173162
}

src/Notion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function buildRequestHeader(): array
227227
}
228228

229229
/**
230-
* Due to the inconsistency of the Notion API requiring a endpoint url
230+
* Due to the inconsistency of the Notion API requiring an endpoint url
231231
* with v* as well as a dated version in the request header, this method
232232
* maps the given version (e.g. v1) to the version date Notion requires
233233
* in the header (e.g. "2021-05-13").

src/Query/Sorting.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,20 @@ public function toArray(): array
9393
}
9494

9595
/**
96-
* @param Collection $sortings
96+
* @param Sorting|Collection $sortings
9797
* @return array
9898
*/
99-
public static function sortQuery(Collection $sortings): array
99+
public static function sortQuery(Sorting|Collection $sortings): array
100100
{
101101
$querySortings = new Collection();
102102

103-
$sortings->each(function (Sorting $sorting) use ($querySortings) {
104-
$querySortings->add($sorting->toArray());
105-
});
103+
if ($sortings instanceof Collection) {
104+
$sortings->each(function (Sorting $sorting) use ($querySortings) {
105+
$querySortings->push($sorting->toArray());
106+
});
107+
} else {
108+
$querySortings->push($sortings->toArray());
109+
}
106110

107111
return $querySortings->toArray();
108112
}

tests/SortingTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use FiveamCode\LaravelNotionApi\Query\Sorting;
4+
use Illuminate\Support\Collection;
5+
6+
it('can sort by a single property', function () {
7+
$expectedSortQuery = '[{"property":"Birth year","direction":"ascending"}]';
8+
9+
$sortBy = Sorting::propertySort('Birth year', 'ascending');
10+
$this->assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortBy)));
11+
});
12+
13+
it('can sort by multiple properties', function () {
14+
$expectedSortQuery = '[{"timestamp":"created_time","direction":"ascending"},{"property":"Birth year","direction":"ascending"}]';
15+
16+
$sortings = new Collection();
17+
18+
$sortings->add(Sorting::timestampSort('created_time', 'ascending'));
19+
$sortings->add(Sorting::propertySort('Birth year', 'ascending'));
20+
21+
$this->assertEquals($expectedSortQuery, json_encode(Sorting::sortQuery($sortings)));
22+
});
23+
24+
it('refuses other classes than sorting or collection in the sortBy() method', function () {
25+
$this->expectException(TypeError::class);
26+
27+
Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')
28+
->sortBy(new stdClass())
29+
->query();
30+
});

0 commit comments

Comments
 (0)