Skip to content

Commit d50e2d3

Browse files
committed
started with query
1 parent 5af94b7 commit d50e2d3

File tree

7 files changed

+179
-22
lines changed

7 files changed

+179
-22
lines changed

src/Endpoints/Databases.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
class Databases extends Endpoint implements EndpointInterface
99
{
10+
private string $databaseId;
11+
12+
1013
public function __construct(Notion $notion)
1114
{
1215
$this->notion = $notion;
16+
parent::__construct();
1317
}
1418

15-
/**
19+
/**
1620
* List databases
1721
* url: https://api.notion.com/{version}/databases
1822
* notion-api-docs: https://developers.notion.com/reference/get-databases
19-
*
23+
*
2024
* @return array
2125
*/
2226
public function all(): array
@@ -28,21 +32,55 @@ public function all(): array
2832
* Retrieve a database
2933
* url: https://api.notion.com/{version}/databases/{database_id}
3034
* notion-api-docs: https://developers.notion.com/reference/get-database
31-
*
35+
*
3236
* @param string $databaseId
3337
* @return array
3438
*/
3539
public function find(string $databaseId): Database
3640
{
3741
$jsonArray = $this->getJson(
38-
$this->url(Endpoint::DATABASES . "/" . $databaseId)
42+
$this->url(Endpoint::DATABASES . "/{$databaseId}")
3943
);
4044
return new Database($jsonArray);
4145
}
4246

43-
public function query(): array
47+
public function query(string $databaseId): array
48+
{
49+
50+
$filterJson = '
51+
{
52+
"property": "Tags",
53+
"multi_select": {
54+
"contains": "great"
55+
}
56+
}';
57+
58+
$filter = json_decode($filterJson);
59+
$postData = ["filter" => $filter];
60+
61+
$response = $this->post(
62+
$this->url(Endpoint::DATABASES . "/{$databaseId}/query"),
63+
$postData
64+
)
65+
->json();
66+
67+
dump($response);
68+
return [];
69+
}
70+
71+
public function filterBy()
72+
{
73+
}
74+
75+
public function sortBy()
76+
{
77+
}
78+
79+
public function limit()
80+
{
81+
}
82+
83+
public function offset()
4484
{
45-
//toDo
46-
throw new \Exception("not implemented yet");
4785
}
4886
}

src/Endpoints/Endpoint.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ protected function getJson(string $url): array
5858
}
5959

6060
/**
61-
*
61+
*
6262
*/
6363
protected function get(string $url)
6464
{
6565
return $this->notion->getConnection()->get($url);
6666
}
67+
68+
/**
69+
*
70+
*/
71+
protected function post(string $url, array $body)
72+
{
73+
return $this->notion->getConnection()->post($url, $body);
74+
}
75+
6776
}

src/Entities/Database.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,57 @@
1212
class Database extends Entity
1313
{
1414
private string $title = "";
15-
private array $rawTitle = [];
15+
private array $rawTitle = []; // toDo why "raw"title?
1616
private array $rawProperties = [];
1717
private DateTime $createdTime;
1818
private DateTime $lastEditedTime;
1919

20+
// toDo Draft
21+
private array $responseData;
22+
23+
2024
protected function setRaw(array $raw): void
2125
{
2226
parent::setRaw($raw);
2327
if ($raw['object'] !== 'database') throw WrapperException::instance("invalid json-array: the given object is not a database");
2428

25-
if (Arr::exists($raw, 'title') && is_array($raw['title'])) {
26-
$this->title = Arr::first($raw['title'], null, ['plain_text' => ''])['plain_text'];
27-
$this->rawTitle = $raw['title'];
28-
}
29-
30-
if (Arr::exists($raw, 'properties')) {
31-
$this->rawProperties = $raw['properties'];
32-
}
29+
//toDo Draft
30+
$this->responseData = $raw;
31+
32+
$this->fillFromRaw();
33+
34+
// if (Arr::exists($raw, 'title') && is_array($raw['title'])) {
35+
// $this->title = Arr::first($raw['title'], null, ['plain_text' => ''])['plain_text'];
36+
// $this->rawTitle = $raw['title'];
37+
// }
38+
//
39+
// if (Arr::exists($raw, 'properties')) {
40+
// $this->rawProperties = $raw['properties'];
41+
// }
42+
//
43+
// if (Arr::exists($raw, 'created_time')) {
44+
// $this->createdTime = new Carbon($raw['created_time']);
45+
// }
46+
//
47+
// if (Arr::exists($raw, 'last_edited_time')) {
48+
// $this->lastEditedTime = new Carbon($raw['last_edited_time']);
49+
// }
50+
}
51+
//toDo Draft
3352

34-
if (Arr::exists($raw, 'created_time')) {
35-
$this->createdTime = new Carbon($raw['created_time']);
36-
}
53+
private function fillFromRaw()
54+
{
55+
$this->fillTitle();
56+
}
3757

38-
if (Arr::exists($raw, 'last_edited_time')) {
39-
$this->lastEditedTime = new Carbon($raw['last_edited_time']);
58+
private function fillTitle()
59+
{
60+
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
61+
$this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text'];
62+
$this->rawTitle = $this->responseData['title'];
4063
}
4164
}
65+
//toDo end Draft
4266

4367
public function getTitle(): string
4468
{

src/Query/CompoundFilter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Query;
4+
5+
class CompoundFilter extends QueryHelper
6+
{
7+
8+
}

src/Query/Filter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Query;
4+
5+
class Filter extends QueryHelper
6+
{
7+
8+
9+
10+
}

src/Query/QueryHelper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Query;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class QueryHelper
8+
{
9+
/**
10+
* Contains the property name the query helper works with.
11+
* @var
12+
*/
13+
protected string $property;
14+
15+
/**
16+
* Contains all valid timestamps to sort against.
17+
*
18+
* @see https://developers.notion.com/reference/post-database-query#post-database-query-sort
19+
* @var Collection
20+
*/
21+
protected Collection $validTimestamps;
22+
/**
23+
* Contains all valid directions to sort by.
24+
*
25+
* @see https://developers.notion.com/reference/post-database-query#post-database-query-sort
26+
* @var Collection
27+
*/
28+
protected Collection $validDirections;
29+
30+
31+
public function __construct()
32+
{
33+
$this->validTimestamps = collect(["created_time", "last_edited_time"]);
34+
$this->validDirections = collect(["ascending", "descending"]);
35+
}
36+
}

src/Query/Sorting.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Query;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
7+
class Sorting extends QueryHelper
8+
{
9+
10+
private string $timestamp;
11+
private string $direction;
12+
13+
public function __construct(string $timestamp, string $direction)
14+
{
15+
parent::__construct();
16+
17+
if ($this->validTimestamps->contains($timestamp))
18+
throw WrapperException::instance(
19+
"Invalid sorting timestamp provided.", ["invalidTimestamp" => $timestamp]
20+
);
21+
22+
if ($this->validDirections->contains($direction))
23+
throw WrapperException::instance(
24+
"Invalid sorting direction provided.", ["invalidDirection" => $direction]
25+
);
26+
27+
$this->timestamp = $timestamp;
28+
$this->direction = $direction;
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)