Skip to content

Commit de705ae

Browse files
committed
polish page and properties
- add parent information to page - check end-date of Date property for null - add "asText" method to Text and Title property
1 parent c8be31c commit de705ae

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/Entities/Page.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use FiveamCode\LaravelNotionApi\Entities\Properties\Url;
1919
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
2020
use Illuminate\Support\Arr;
21+
use Illuminate\Support\Str;
2122
use Illuminate\Support\Collection;
2223

2324
/**
@@ -55,6 +56,16 @@ class Page extends Entity
5556
*/
5657
private string $coverType = '';
5758

59+
/**
60+
* @var string
61+
*/
62+
private string $parentId = '';
63+
64+
/**
65+
* @var string
66+
*/
67+
private string $parentType = '';
68+
5869
/**
5970
* @var string
6071
*/
@@ -122,6 +133,7 @@ protected function setResponseData(array $responseData): void
122133
private function fillFromRaw(): void
123134
{
124135
$this->fillId();
136+
$this->fillParent();
125137
$this->fillObjectType();
126138
$this->fillProperties();
127139
$this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties
@@ -205,6 +217,20 @@ private function fillPageUrl(): void
205217
}
206218
}
207219

220+
private function fillParent(): void
221+
{
222+
if (Arr::exists($this->responseData, 'parent')) {
223+
$this->parentType = $this->responseData['parent']['type'];
224+
if (Arr::exists($this->responseData['parent'], 'database_id')) {
225+
$this->parentId = $this->responseData['parent']['database_id'];
226+
} elseif (Arr::exists($this->responseData['parent'], 'page_id')) {
227+
$this->parentId = $this->responseData['parent']['page_id'];
228+
} elseif (Arr::exists($this->responseData['parent'], 'workspace')) {
229+
$this->parentId = $this->responseData['parent']['workspace'];
230+
}
231+
}
232+
}
233+
208234
/**
209235
* @param $propertyTitle
210236
* @param $property
@@ -443,7 +469,7 @@ public function getProperties(): Collection
443469
*/
444470
public function getProperty(string $propertyKey): ?Property
445471
{
446-
if (! isset($this->propertyMap[$propertyKey])) {
472+
if (!isset($this->propertyMap[$propertyKey])) {
447473
return null;
448474
}
449475

@@ -458,6 +484,22 @@ public function getObjectType(): string
458484
return $this->objectType;
459485
}
460486

487+
/**
488+
* @return string
489+
*/
490+
public function getParentId(): string
491+
{
492+
return $this->parentId;
493+
}
494+
495+
/**
496+
* @return string
497+
*/
498+
public function getParentType(): string
499+
{
500+
return $this->parentType;
501+
}
502+
461503
/**
462504
* @return array
463505
*/

src/Entities/Properties/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function fillDate(): void
9797
$richDate->setHasTime($this->isIsoTimeString($startAsIsoString));
9898
}
9999

100-
if (Arr::exists($this->rawContent, 'end')) {
100+
if (Arr::exists($this->rawContent, 'end') && $this->rawContent['end'] !== null) {
101101
$endAsIsoString = $this->rawContent['end'];
102102
$richDate->setEnd(new DateTime($endAsIsoString));
103103
}

src/Entities/Properties/Text.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static function value($text): Text
5656
protected function fillFromRaw(): void
5757
{
5858
parent::fillFromRaw();
59-
if (! is_array($this->rawContent)) {
59+
if (!is_array($this->rawContent)) {
6060
throw HandlingException::instance('The property-type is text, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.');
6161
}
6262

@@ -77,6 +77,14 @@ public function getContent(): RichText
7777
return $this->getRichText();
7878
}
7979

80+
/**
81+
* @return string
82+
*/
83+
public function asText(): string
84+
{
85+
return $this->getPlainText();
86+
}
87+
8088
/**
8189
* @return RichText
8290
*/

src/Entities/Properties/Title.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function getContent(): RichText
7676
return $this->getRichText();
7777
}
7878

79+
/**
80+
* @return string
81+
*/
82+
public function asText(): string
83+
{
84+
return $this->getPlainText();
85+
}
86+
7987
/**
8088
* @return RichText
8189
*/

0 commit comments

Comments
 (0)