-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from mariosimao/release-0.5.0
- Loading branch information
Showing
30 changed files
with
2,264 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# List database pages | ||
|
||
```php | ||
<?php | ||
|
||
use Notion\Notion; | ||
|
||
$token = $_ENV["NOTION_SECRET"]; | ||
|
||
$notion = Notion::create($token); | ||
|
||
$databaseId = "c986d7b0-7051-4f18-b165-cc0b9503ffc2"; | ||
$database = $notion->databases()->find($pageId); | ||
|
||
$pages = $notion->databases()->queryAllPages($database); | ||
|
||
count($pages); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Query database | ||
|
||
```php | ||
<?php | ||
|
||
use Notion\Notion; | ||
use Notion\Databases\Query; | ||
use Notion\Databases\Query\CompoundFilter; | ||
use Notion\Databases\Query\DateFilter; | ||
use Notion\Databases\Query\Sort; | ||
use Notion\Databases\Query\TextFilter; | ||
|
||
$token = $_ENV["NOTION_SECRET"]; | ||
|
||
$notion = Notion::create($token); | ||
|
||
$databaseId = "c986d7b0-7051-4f18-b165-cc0b9503ffc2"; | ||
$database = $notion->databases()->find($pageId); | ||
|
||
$query = Query::create() | ||
->withFilter( | ||
CompoundFilter::and( | ||
DateFilter::createdTime::pastWeek(), | ||
TextFilter::property("Name")->contains("John"), | ||
) | ||
) | ||
->withAddedSort(Sort::property("Name")->ascending()) | ||
->withPageSize(20); | ||
|
||
$result = $notion->databases()->query($database, $query); | ||
|
||
$pages = $result->pages(); // array of Page | ||
$result->hasMore(); // true or false | ||
$result->nextCursor() // cursor ID or null | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Notion\Databases; | ||
|
||
use Exception; | ||
use Notion\Databases\Query\Filter; | ||
use Notion\Databases\Query\Sort; | ||
|
||
/** @psalm-immutable */ | ||
class Query | ||
{ | ||
public const MAX_PAGE_SIZE = 100; | ||
|
||
private Filter|null $filter; | ||
|
||
/** @var list<Sort> */ | ||
private array $sorts; | ||
|
||
private string|null $startCursor; | ||
|
||
private int $pageSize; | ||
|
||
/** @param list<Sort> $sorts */ | ||
private function __construct( | ||
Filter|null $filter, | ||
array $sorts, | ||
string|null $startCursor, | ||
int $pageSize | ||
) { | ||
$this->filter = $filter; | ||
$this->sorts = $sorts; | ||
$this->startCursor = $startCursor; | ||
$this->pageSize = $pageSize; | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
return new self(null, [], null, self::MAX_PAGE_SIZE); | ||
} | ||
|
||
public function withFilter(Filter $filter): self | ||
{ | ||
return new self($filter, $this->sorts, $this->startCursor, $this->pageSize); | ||
} | ||
|
||
/** Add new sort with lowest priority */ | ||
public function withAddedSort(Sort $sort): self | ||
{ | ||
$sorts = $this->sorts; | ||
$sorts[] = $sort; | ||
|
||
return new self($this->filter, $sorts, $this->startCursor, $this->pageSize); | ||
} | ||
|
||
/** | ||
* Replace all sorts | ||
* | ||
* @param list<Sort> $sorts | ||
*/ | ||
public function withSorts(array $sorts): self | ||
{ | ||
return new self($this->filter, $sorts, $this->startCursor, $this->pageSize); | ||
} | ||
|
||
public function withStartCursor(string $startCursor): self | ||
{ | ||
return new self($this->filter, $this->sorts, $startCursor, $this->pageSize); | ||
} | ||
|
||
public function withPageSize(int $pageSize): self | ||
{ | ||
if ($pageSize < 0 || $pageSize > self::MAX_PAGE_SIZE) { | ||
throw new Exception("Maximum page size: " . self::MAX_PAGE_SIZE); | ||
} | ||
|
||
return new self($this->filter, $this->sorts, $this->startCursor, $pageSize); | ||
} | ||
|
||
public function filter(): Filter|null | ||
{ | ||
return $this->filter; | ||
} | ||
|
||
/** @return list<Sort> */ | ||
public function sorts(): array | ||
{ | ||
return $this->sorts; | ||
} | ||
|
||
public function startCursor(): string|null | ||
{ | ||
return $this->startCursor; | ||
} | ||
|
||
public function pageSize(): int | ||
{ | ||
return $this->pageSize; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = [ | ||
"sorts" => array_map(fn (Sort $s) => $s->toArray(), $this->sorts), | ||
"page_size" => $this->pageSize, | ||
]; | ||
|
||
if ($this->filter !== null) { | ||
$array["filter"] = $this->filter->toArray(); | ||
} | ||
|
||
if ($this->startCursor !== null) { | ||
$array["start_cursor"] = $this->startCursor; | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Notion\Databases\Query; | ||
|
||
/** @psalm-immutable */ | ||
class CheckboxFilter implements Filter, Condition | ||
{ | ||
private const OPERATOR_EQUALS = "equals"; | ||
private const OPERATOR_DOES_NOT_EQUAL = "does_not_equal"; | ||
|
||
private string $propertyType = "property"; | ||
private string $propertyName; | ||
/** @var self::OPERATOR_* */ | ||
private string $operator; | ||
private bool $value; | ||
|
||
/** @param self::OPERATOR_* $operator */ | ||
private function __construct( | ||
string $propertyName, | ||
string $operator, | ||
bool $value, | ||
) { | ||
$this->propertyName = $propertyName; | ||
$this->operator = $operator; | ||
$this->value = $value; | ||
} | ||
|
||
public static function property(string $propertyName): self | ||
{ | ||
return new self( | ||
$propertyName, | ||
self::OPERATOR_EQUALS, | ||
true | ||
); | ||
} | ||
|
||
public function propertyType(): string | ||
{ | ||
return $this->propertyType; | ||
} | ||
|
||
public function propertyName(): string | ||
{ | ||
return $this->propertyName; | ||
} | ||
|
||
/** @return static::OPERATOR_* */ | ||
public function operator(): string | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
public function value(): bool | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
$this->propertyType() => $this->propertyName, | ||
"checkbox" => [ | ||
$this->operator => $this->value | ||
], | ||
]; | ||
} | ||
|
||
public function equals(bool $value): self | ||
{ | ||
return new self($this->propertyName, self::OPERATOR_EQUALS, $value); | ||
} | ||
|
||
public function doesNotEqual(bool $value): self | ||
{ | ||
return new self($this->propertyName, self::OPERATOR_DOES_NOT_EQUAL, $value); | ||
} | ||
} |
Oops, something went wrong.