Skip to content

Commit 2f75bbd

Browse files
authored
Merge pull request #93 from 5am-code/feature/allow-single-sorting-in-query
Allow single sorting property in sortBy method of database endpoint
2 parents 42b3670 + ed30338 commit 2f75bbd

File tree

9 files changed

+68
-34
lines changed

9 files changed

+68
-34
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: 3 additions & 3 deletions
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
},
@@ -47,8 +47,8 @@
4747
}
4848
},
4949
"scripts": {
50-
"test": "vendor/bin/phpunit",
51-
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
50+
"test": "vendor/bin/pest",
51+
"test-coverage": "vendor/bin/pest --coverage-html coverage"
5252

5353
},
5454
"config": {

src/Endpoints/Database.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
66
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
7+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
78
use FiveamCode\LaravelNotionApi\Notion;
89
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
910
use FiveamCode\LaravelNotionApi\Query\Sorting;
@@ -96,12 +97,24 @@ public function filterBy(Collection $filter): Database
9697
}
9798

9899
/**
99-
* @param Collection $sorts
100-
* @return $this
100+
* @param Collection|Sorting $sorts
101+
* @return Database $this
102+
*
103+
* @throws HandlingException
101104
*/
102-
public function sortBy(Collection $sorts): Database
105+
public function sortBy(Sorting|Collection $sorts): Database
103106
{
104-
$this->sorts = $sorts;
107+
$sortInstance = get_class($sorts);
108+
switch ($sortInstance) {
109+
case Sorting::class:
110+
$this->sorts->push($sorts);
111+
break;
112+
case Collection::class:
113+
$this->sorts = $sorts;
114+
break;
115+
default:
116+
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings.");
117+
}
105118

106119
return $this;
107120
}

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/NotionApiTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use FiveamCode\LaravelNotionApi\Notion;
66
use FiveamCode\LaravelNotionApi\NotionFacade;
77
use Illuminate\Support\Collection;
8-
use Orchestra\Testbench\TestCase;
8+
use Orchestra\Testbench\TestCase as Orchestra;
99

1010
/**
1111
* Class EndpointPageTest.
@@ -14,7 +14,7 @@
1414
*
1515
* @see https://developers.notion.com/reference/get-page
1616
*/
17-
class NotionApiTest extends TestCase
17+
class NotionApiTest extends Orchestra
1818
{
1919
/**
2020
* @param \Illuminate\Foundation\Application $app
@@ -50,10 +50,4 @@ protected function assertContainsInstanceOf(string $class, $haystack): bool
5050

5151
return false;
5252
}
53-
54-
/** @test */
55-
public function it_asserts_true()
56-
{
57-
$this->assertTrue(true);
58-
}
5953
}

tests/Pest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use FiveamCode\LaravelNotionApi\Tests\NotionApiTest;
4+
5+
uses(NotionApiTest::class)->in(__DIR__);

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)