Skip to content

Commit 4f75db6

Browse files
authored
Merge pull request #77 from 5am-code/dev
v0.8.0 🍉 - Bugfixes, Pagination and General Improvements
2 parents 21e07d4 + 1963906 commit 4f75db6

13 files changed

+802
-20
lines changed

src/Endpoints/Database.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5+
use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
56
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
67
use FiveamCode\LaravelNotionApi\Notion;
78
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
@@ -66,7 +67,7 @@ public function query(): PageCollection
6667
} // TODO Compound filters!
6768

6869
if ($this->startCursor !== null) {
69-
$postData['start_cursor'] = $this->startCursor;
70+
$postData['start_cursor'] = $this->startCursor->__toString();
7071
}
7172

7273
if ($this->pageSize !== null) {
@@ -104,4 +105,15 @@ public function sortBy(Collection $sorts): Database
104105

105106
return $this;
106107
}
108+
109+
/**
110+
* @param EntityCollection $entityCollection
111+
* @return $this
112+
*/
113+
public function offsetByResponse(EntityCollection $entityCollection): Database
114+
{
115+
$this->offset($entityCollection->nextCursor());
116+
117+
return $this;
118+
}
107119
}

src/Entities/Collections/EntityCollection.php

+51
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FiveamCode\LaravelNotionApi\Entities\Page;
88
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
99
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
10+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\Collection;
1213

@@ -25,6 +26,16 @@ class EntityCollection
2526
*/
2627
protected array $rawResults = [];
2728

29+
/**
30+
* @var bool
31+
*/
32+
protected bool $hasMore = false;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected ?string $nextCursor = null;
38+
2839
/**
2940
* @var Collection
3041
*/
@@ -96,13 +107,24 @@ protected function collectChildren(): void
96107
protected function fillFromRaw()
97108
{
98109
$this->fillResult();
110+
$this->fillCursorInformation();
99111
}
100112

101113
protected function fillResult()
102114
{
103115
$this->rawResults = $this->responseData['results'];
104116
}
105117

118+
protected function fillCursorInformation()
119+
{
120+
if (Arr::exists($this->responseData, 'has_more')) {
121+
$this->hasMore = $this->responseData['has_more'];
122+
}
123+
if (Arr::exists($this->responseData, 'next_cursor')) {
124+
$this->nextCursor = $this->responseData['next_cursor'];
125+
}
126+
}
127+
106128
/**
107129
* @return array
108130
*/
@@ -111,6 +133,14 @@ public function getRawResponse(): array
111133
return $this->responseData;
112134
}
113135

136+
/**
137+
* @return string
138+
*/
139+
public function getRawNextCursor(): ?string
140+
{
141+
return $this->nextCursor;
142+
}
143+
114144
/**
115145
* @return Collection
116146
*/
@@ -128,4 +158,25 @@ public function asJson(): string
128158
return $item->toArray();
129159
});
130160
}
161+
162+
/**
163+
* @return bool
164+
*/
165+
public function hasMoreEntries(): bool
166+
{
167+
return $this->hasMore;
168+
}
169+
170+
/**
171+
* @return StartCursor
172+
*/
173+
public function nextCursor(): ?StartCursor
174+
{
175+
$rawNextCursor = $this->getRawNextCursor();
176+
if ($rawNextCursor === null) {
177+
return null;
178+
}
179+
180+
return new StartCursor($rawNextCursor);
181+
}
131182
}

src/Entities/Page.php

+13
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ public function setDate(string $propertyTitle, DateTime $start, ?DateTime $end =
344344
return $this;
345345
}
346346

347+
/**
348+
* @param $propertyTitle
349+
* @param $start
350+
* @param $end
351+
* @return Page
352+
*/
353+
public function setDateTime(string $propertyTitle, DateTime $start, ?DateTime $end = null): Page
354+
{
355+
$this->set($propertyTitle, Date::valueWithTime($start, $end));
356+
357+
return $this;
358+
}
359+
347360
/**
348361
* @param $propertyTitle
349362
* @param $relationIds

src/Entities/Properties/Date.php

+65-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
77
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
88
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
9+
use Illuminate\Support\Arr;
910

1011
/**
1112
* Class Date.
@@ -26,6 +27,39 @@ public static function value(?DateTime $start, ?DateTime $end = null): Date
2627
$dateProperty = new Date();
2728
$dateProperty->content = $richDate;
2829

30+
if ($richDate->isRange()) {
31+
$dateProperty->rawContent = [
32+
'date' => [
33+
'start' => $start->format('Y-m-d'),
34+
'end' => $end->format('Y-m-d'),
35+
],
36+
];
37+
} else {
38+
$dateProperty->rawContent = [
39+
'date' => [
40+
'start' => $start->format('Y-m-d'),
41+
],
42+
];
43+
}
44+
45+
return $dateProperty;
46+
}
47+
48+
/**
49+
* @param $start
50+
* @param $end
51+
* @return Date
52+
*/
53+
public static function valueWithTime(?DateTime $start, ?DateTime $end = null): Date
54+
{
55+
$richDate = new RichDate();
56+
$richDate->setStart($start);
57+
$richDate->setEnd($end);
58+
$richDate->setHasTime(true);
59+
60+
$dateProperty = new Date();
61+
$dateProperty->content = $richDate;
62+
2963
if ($richDate->isRange()) {
3064
$dateProperty->rawContent = [
3165
'date' => [
@@ -57,19 +91,26 @@ protected function fillDate(): void
5791
{
5892
$richDate = new RichDate();
5993

60-
if (isset($this->rawContent['start'])) {
94+
if (Arr::exists($this->rawContent, 'start')) {
6195
$startAsIsoString = $this->rawContent['start'];
6296
$richDate->setStart(new DateTime($startAsIsoString));
97+
$richDate->setHasTime($this->isIsoTimeString($startAsIsoString));
6398
}
6499

65-
if (isset($this->rawContent['end'])) {
100+
if (Arr::exists($this->rawContent, 'end')) {
66101
$endAsIsoString = $this->rawContent['end'];
67102
$richDate->setEnd(new DateTime($endAsIsoString));
68103
}
69104

70105
$this->content = $richDate;
71106
}
72107

108+
// function for checking if ISO datetime string includes time or not
109+
private function isIsoTimeString(string $isoTimeDateString): bool
110+
{
111+
return strpos($isoTimeDateString, 'T') !== false;
112+
}
113+
73114
/**
74115
* @return RichDate
75116
*/
@@ -91,14 +132,34 @@ public function isRange(): bool
91132
*/
92133
public function getStart(): DateTime
93134
{
135+
if ($this->getContent() === null) {
136+
throw new HandlingException('Invalid content: The content of the Date Property is null.');
137+
}
138+
94139
return $this->getContent()->getStart();
95140
}
96141

97142
/**
98-
* @return DateTime
143+
* @return ?DateTime
99144
*/
100-
public function getEnd(): DateTime
145+
public function getEnd(): ?DateTime
101146
{
147+
if ($this->getContent() === null) {
148+
return null;
149+
}
150+
102151
return $this->getContent()->getEnd();
103152
}
153+
154+
/**
155+
* @return bool
156+
*/
157+
public function hasTime(): bool
158+
{
159+
if ($this->getContent() === null) {
160+
return false;
161+
}
162+
163+
return $this->getContent()->hasTime();
164+
}
104165
}

src/Entities/Properties/Rollup.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function fillFromRaw(): void
3737
break;
3838
default:
3939
throw new HandlingException("Unexpected rollupType {$this->rollupType}");
40-
}
40+
}
4141
}
4242
}
4343

@@ -89,12 +89,21 @@ private function setRollupContentArray()
8989
// TODO
9090
$rollupPropertyItem['id'] = 'undefined';
9191

92-
$this->content->add(
93-
Property::fromResponse('', $rollupPropertyItem)
94-
);
92+
if ($this->isRollupPropertyContentSet($rollupPropertyItem)) {
93+
$this->content->add(
94+
Property::fromResponse('', $rollupPropertyItem)
95+
);
96+
}
9597
}
9698
}
9799

100+
private function isRollupPropertyContentSet($rollupPropertyItem): bool
101+
{
102+
return Arr::exists($rollupPropertyItem, 'type')
103+
&& Arr::exists($rollupPropertyItem, $rollupPropertyItem['type'])
104+
&& ! is_null($rollupPropertyItem[$rollupPropertyItem['type']]);
105+
}
106+
98107
private function setRollupContentDate()
99108
{
100109
$this->content = new RichDate();

src/Entities/PropertyItems/RichDate.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
*/
1212
class RichDate extends Entity
1313
{
14-
/**
15-
* @var string
16-
*/
1714
protected DateTime $start;
1815
protected ?DateTime $end = null;
16+
protected bool $hasTime = false;
1917

2018
/**
2119
* @param array $responseData
@@ -63,13 +61,21 @@ public function getStart(): ?DateTime
6361
}
6462

6563
/**
66-
* @return DateTime
64+
* @return ?DateTime
6765
*/
6866
public function getEnd(): ?DateTime
6967
{
7068
return $this->end;
7169
}
7270

71+
/**
72+
* @return bool
73+
*/
74+
public function hasTime(): bool
75+
{
76+
return $this->hasTime;
77+
}
78+
7379
public function setStart($start): void
7480
{
7581
$this->start = $start;
@@ -79,4 +85,9 @@ public function setEnd($end): void
7985
{
8086
$this->end = $end;
8187
}
88+
89+
public function setHasTime($hasTime): void
90+
{
91+
$this->hasTime = $hasTime;
92+
}
8293
}

src/Query/StartCursor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(string $cursor)
2222
$this->cursor = $cursor;
2323
}
2424

25-
public function __toString()
25+
public function __toString(): string
2626
{
2727
return $this->cursor;
2828
}

0 commit comments

Comments
 (0)