Skip to content

Commit c541080

Browse files
committed
polish/fix: read (fetch) hasTime in date property
- refactor 'isset' to Arr::exists - evaluate if time is included, based on 'T' in datetime-string (when reading a date property from Notion) - allow return of null within ->getEnd() getter (allow same in RichDate::class) - add ->hasTime() getter, which calls ->hasTime() from RichDate::class (content) - refactor ->getHasTime() from RichDate::class to ->hasTime()
1 parent 85a8ff8 commit c541080

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/Entities/Properties/Date.php

Lines changed: 20 additions & 4 deletions
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.
@@ -90,19 +91,26 @@ protected function fillDate(): void
9091
{
9192
$richDate = new RichDate();
9293

93-
if (isset($this->rawContent['start'])) {
94+
if (Arr::exists($this->rawContent, 'start')) {
9495
$startAsIsoString = $this->rawContent['start'];
9596
$richDate->setStart(new DateTime($startAsIsoString));
97+
$richDate->setHasTime($this->isIsoTimeString($startAsIsoString));
9698
}
9799

98-
if (isset($this->rawContent['end'])) {
100+
if (Arr::exists($this->rawContent, 'end')) {
99101
$endAsIsoString = $this->rawContent['end'];
100102
$richDate->setEnd(new DateTime($endAsIsoString));
101103
}
102104

103105
$this->content = $richDate;
104106
}
105107

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+
106114
/**
107115
* @return RichDate
108116
*/
@@ -128,10 +136,18 @@ public function getStart(): DateTime
128136
}
129137

130138
/**
131-
* @return DateTime
139+
* @return ?DateTime
132140
*/
133-
public function getEnd(): DateTime
141+
public function getEnd(): ?DateTime
134142
{
135143
return $this->getContent()->getEnd();
136144
}
145+
146+
/**
147+
* @return bool
148+
*/
149+
public function hasTime(): bool
150+
{
151+
return $this->getContent()->hasTime();
152+
}
137153
}

src/Entities/PropertyItems/RichDate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getStart(): ?DateTime
6161
}
6262

6363
/**
64-
* @return DateTime
64+
* @return ?DateTime
6565
*/
6666
public function getEnd(): ?DateTime
6767
{
@@ -71,7 +71,7 @@ public function getEnd(): ?DateTime
7171
/**
7272
* @return bool
7373
*/
74-
public function getHasTime(): bool
74+
public function hasTime(): bool
7575
{
7676
return $this->hasTime;
7777
}

0 commit comments

Comments
 (0)