Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Query::addSort() #144

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/how-to/query-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $query = Query::create()
TextFilter::property("Name")->contains("John"),
)
)
->withAddedSort(Sort::property("Name")->ascending())
->addSort(Sort::property("Name")->ascending())
->withPageSize(20);

$result = $notion->databases()->query($database, $query);
Expand Down
13 changes: 11 additions & 2 deletions src/Databases/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ public function changeFilter(Filter $filter): self
return new self($filter, $this->sorts, $this->startCursor, $this->pageSize);
}

/** Add new sort change lowest priority */
public function changeAddedSort(Sort $sort): self
/** Add new sort with lowest priority */
public function addSort(Sort $sort): self
{
$sorts = $this->sorts;
$sorts[] = $sort;

return new self($this->filter, $sorts, $this->startCursor, $this->pageSize);
}

/**
* @deprecated 1.1.0 This method will be removed in future versions. Use 'addSort' instead.
* @see \Notion\Databases\Query::addSort()
*/
public function changeAddedSort(Sort $sort): self
{
return $this->addSort($sort);
}

/** Replace all sorts */
public function changeSorts(Sort ...$sorts): self
{
Expand Down
20 changes: 15 additions & 5 deletions tests/Unit/Databases/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,32 @@ public function test_query_change_filter(): void
public function test_add_sort(): void
{
$query = Query::create()
->changeAddedSort(Sort::createdTime()->descending())
->changeAddedSort(Sort::property("Title")->ascending());
->addSort(Sort::createdTime()->descending())
->addSort(Sort::property("Title")->ascending());

$this->assertCount(2, $query->sorts);
}

public function test_replace_sorts(): void
{
$query = Query::create()
->changeAddedSort(Sort::createdTime()->descending())
->changeAddedSort(Sort::property("Title")->ascending())
->addSort(Sort::createdTime()->descending())
->addSort(Sort::property("Title")->ascending())
->changeSorts(Sort::lastEditedTime()->descending());

$this->assertCount(1, $query->sorts);
}

/** @psalm-suppress DeprecatedMethod */
public function test_deprecated_change_added_sort(): void
{
$query = Query::create()
->changeAddedSort(Sort::createdTime()->descending())
->changeAddedSort(Sort::property("Title")->ascending());

$this->assertCount(2, $query->sorts);
}

public function test_query_change_start_cursor(): void
{
$query = Query::create()
Expand Down Expand Up @@ -87,7 +97,7 @@ public function test_complete_query_to_array(): void
{
$query = Query::create()
->changeFilter(TextFilter::property("Title")->contains("abc"))
->changeAddedSort(Sort::property("Title")->ascending())
->addSort(Sort::property("Title")->ascending())
->changeStartCursor("889431ed-4f50-460b-a926-36f6cf0f9669")
->changePageSize(20);

Expand Down