Skip to content

v0.8.0 🍉 - Bugfixes, Pagination and General Improvements #77

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

Merged
merged 25 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2bfcee4
fix: check if content is set for rollup properties
johguentner Jul 9, 2022
2507d0f
Apply fixes from StyleCI (#67)
mechelon Jul 9, 2022
7a9da2d
polish/fix: control over 'includeTime' of Date
johguentner Jul 9, 2022
8111720
add: test for edge case of empty Select in Rollup
johguentner Jul 9, 2022
32fbfc6
Apply fixes from StyleCI (#70)
mechelon Jul 9, 2022
0283feb
fix test: breaking-changes in Date prop handling
johguentner Jul 9, 2022
420c037
add tests: for Date Property with forced time
johguentner Jul 9, 2022
85a8ff8
Apply fixes from StyleCI (#71)
mechelon Jul 9, 2022
0fdd0c0
add: better pagination handling
johguentner Jul 10, 2022
5d11817
Apply fixes from StyleCI (#72)
mechelon Jul 10, 2022
dafd6de
polish: handling for empty next_cursor
johguentner Jul 10, 2022
e1b3aca
add tests for improved pagination handling
johguentner Jul 10, 2022
a64227c
Merge branch 'feature/better-pagination-handling' of https://github.c…
johguentner Jul 10, 2022
bc8a3e6
Apply fixes from StyleCI (#74)
mechelon Jul 10, 2022
c541080
polish/fix: read (fetch) hasTime in date property
johguentner Jul 11, 2022
0a07217
modify tests: evaluate 'hasTime' in date props
johguentner Jul 11, 2022
a77289e
clarify: move edge-case label to test method
johguentner Jul 11, 2022
9aa463a
Apply fixes from StyleCI (#75)
mechelon Jul 11, 2022
b935959
Merge pull request #68 from 5am-code/65-rollup-with-empty-select-fix
johguentner Jul 11, 2022
461b1dd
Merge pull request #69 from 5am-code/61-choose-time-format
johguentner Jul 11, 2022
66ca7af
Merge branch 'dev' into feature/better-pagination-handling
johguentner Jul 11, 2022
2451387
Apply fixes from StyleCI (#76)
mechelon Jul 11, 2022
f1e0af9
Merge pull request #73 from 5am-code/feature/better-pagination-handling
johguentner Jul 11, 2022
40ed8ac
fix: check if content is null in date property
johguentner Aug 1, 2022
1963906
Apply fixes from StyleCI (#78)
mechelon Aug 1, 2022
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
14 changes: 13 additions & 1 deletion src/Endpoints/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FiveamCode\LaravelNotionApi\Endpoints;

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

if ($this->startCursor !== null) {
$postData['start_cursor'] = $this->startCursor;
$postData['start_cursor'] = $this->startCursor->__toString();
}

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

return $this;
}

/**
* @param EntityCollection $entityCollection
* @return $this
*/
public function offsetByResponse(EntityCollection $entityCollection): Database
{
$this->offset($entityCollection->nextCursor());

return $this;
}
}
51 changes: 51 additions & 0 deletions src/Entities/Collections/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Query\StartCursor;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

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

/**
* @var bool
*/
protected bool $hasMore = false;

/**
* @var string
*/
protected ?string $nextCursor = null;

/**
* @var Collection
*/
Expand Down Expand Up @@ -96,13 +107,24 @@ protected function collectChildren(): void
protected function fillFromRaw()
{
$this->fillResult();
$this->fillCursorInformation();
}

protected function fillResult()
{
$this->rawResults = $this->responseData['results'];
}

protected function fillCursorInformation()
{
if (Arr::exists($this->responseData, 'has_more')) {
$this->hasMore = $this->responseData['has_more'];
}
if (Arr::exists($this->responseData, 'next_cursor')) {
$this->nextCursor = $this->responseData['next_cursor'];
}
}

/**
* @return array
*/
Expand All @@ -111,6 +133,14 @@ public function getRawResponse(): array
return $this->responseData;
}

/**
* @return string
*/
public function getRawNextCursor(): ?string
{
return $this->nextCursor;
}

/**
* @return Collection
*/
Expand All @@ -128,4 +158,25 @@ public function asJson(): string
return $item->toArray();
});
}

/**
* @return bool
*/
public function hasMoreEntries(): bool
{
return $this->hasMore;
}

/**
* @return StartCursor
*/
public function nextCursor(): ?StartCursor
{
$rawNextCursor = $this->getRawNextCursor();
if ($rawNextCursor === null) {
return null;
}

return new StartCursor($rawNextCursor);
}
}
13 changes: 13 additions & 0 deletions src/Entities/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ public function setDate(string $propertyTitle, DateTime $start, ?DateTime $end =
return $this;
}

/**
* @param $propertyTitle
* @param $start
* @param $end
* @return Page
*/
public function setDateTime(string $propertyTitle, DateTime $start, ?DateTime $end = null): Page
{
$this->set($propertyTitle, Date::valueWithTime($start, $end));

return $this;
}

/**
* @param $propertyTitle
* @param $relationIds
Expand Down
69 changes: 65 additions & 4 deletions src/Entities/Properties/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use FiveamCode\LaravelNotionApi\Entities\Contracts\Modifiable;
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use Illuminate\Support\Arr;

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

if ($richDate->isRange()) {
$dateProperty->rawContent = [
'date' => [
'start' => $start->format('Y-m-d'),
'end' => $end->format('Y-m-d'),
],
];
} else {
$dateProperty->rawContent = [
'date' => [
'start' => $start->format('Y-m-d'),
],
];
}

return $dateProperty;
}

/**
* @param $start
* @param $end
* @return Date
*/
public static function valueWithTime(?DateTime $start, ?DateTime $end = null): Date
{
$richDate = new RichDate();
$richDate->setStart($start);
$richDate->setEnd($end);
$richDate->setHasTime(true);

$dateProperty = new Date();
$dateProperty->content = $richDate;

if ($richDate->isRange()) {
$dateProperty->rawContent = [
'date' => [
Expand Down Expand Up @@ -57,19 +91,26 @@ protected function fillDate(): void
{
$richDate = new RichDate();

if (isset($this->rawContent['start'])) {
if (Arr::exists($this->rawContent, 'start')) {
$startAsIsoString = $this->rawContent['start'];
$richDate->setStart(new DateTime($startAsIsoString));
$richDate->setHasTime($this->isIsoTimeString($startAsIsoString));
}

if (isset($this->rawContent['end'])) {
if (Arr::exists($this->rawContent, 'end')) {
$endAsIsoString = $this->rawContent['end'];
$richDate->setEnd(new DateTime($endAsIsoString));
}

$this->content = $richDate;
}

// function for checking if ISO datetime string includes time or not
private function isIsoTimeString(string $isoTimeDateString): bool
{
return strpos($isoTimeDateString, 'T') !== false;
}

/**
* @return RichDate
*/
Expand All @@ -91,14 +132,34 @@ public function isRange(): bool
*/
public function getStart(): DateTime
{
if ($this->getContent() === null) {
throw new HandlingException('Invalid content: The content of the Date Property is null.');
}

return $this->getContent()->getStart();
}

/**
* @return DateTime
* @return ?DateTime
*/
public function getEnd(): DateTime
public function getEnd(): ?DateTime
{
if ($this->getContent() === null) {
return null;
}

return $this->getContent()->getEnd();
}

/**
* @return bool
*/
public function hasTime(): bool
{
if ($this->getContent() === null) {
return false;
}

return $this->getContent()->hasTime();
}
}
17 changes: 13 additions & 4 deletions src/Entities/Properties/Rollup.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function fillFromRaw(): void
break;
default:
throw new HandlingException("Unexpected rollupType {$this->rollupType}");
}
}
}
}

Expand Down Expand Up @@ -89,12 +89,21 @@ private function setRollupContentArray()
// TODO
$rollupPropertyItem['id'] = 'undefined';

$this->content->add(
Property::fromResponse('', $rollupPropertyItem)
);
if ($this->isRollupPropertyContentSet($rollupPropertyItem)) {
$this->content->add(
Property::fromResponse('', $rollupPropertyItem)
);
}
}
}

private function isRollupPropertyContentSet($rollupPropertyItem): bool
{
return Arr::exists($rollupPropertyItem, 'type')
&& Arr::exists($rollupPropertyItem, $rollupPropertyItem['type'])
&& ! is_null($rollupPropertyItem[$rollupPropertyItem['type']]);
}

private function setRollupContentDate()
{
$this->content = new RichDate();
Expand Down
19 changes: 15 additions & 4 deletions src/Entities/PropertyItems/RichDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
*/
class RichDate extends Entity
{
/**
* @var string
*/
protected DateTime $start;
protected ?DateTime $end = null;
protected bool $hasTime = false;

/**
* @param array $responseData
Expand Down Expand Up @@ -63,13 +61,21 @@ public function getStart(): ?DateTime
}

/**
* @return DateTime
* @return ?DateTime
*/
public function getEnd(): ?DateTime
{
return $this->end;
}

/**
* @return bool
*/
public function hasTime(): bool
{
return $this->hasTime;
}

public function setStart($start): void
{
$this->start = $start;
Expand All @@ -79,4 +85,9 @@ public function setEnd($end): void
{
$this->end = $end;
}

public function setHasTime($hasTime): void
{
$this->hasTime = $hasTime;
}
}
2 changes: 1 addition & 1 deletion src/Query/StartCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $cursor)
$this->cursor = $cursor;
}

public function __toString()
public function __toString(): string
{
return $this->cursor;
}
Expand Down
Loading