Skip to content

Commit

Permalink
Rename Query::addSort() (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao authored Jan 9, 2023
1 parent b0a54b3 commit a4a7218
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
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

0 comments on commit a4a7218

Please sign in to comment.