Skip to content

polish/fix: control over 'includeTime' of Date #69

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 6 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
57 changes: 53 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 @@ -95,10 +136,18 @@ public function getStart(): DateTime
}

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

/**
* @return bool
*/
public function hasTime(): bool
{
return $this->getContent()->hasTime();
}
}
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;
}
}
58 changes: 52 additions & 6 deletions tests/EndpointPagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ public function it_returns_page_entity_with_filled_properties()
// check properties
$this->assertSame('Notion Is Awesome', $pageResult->getTitle());
$this->assertSame('page', $pageResult->getObjectType());
$this->assertCount(7, $pageResult->getRawProperties());
$this->assertCount(7, $pageResult->getProperties());
$this->assertCount(7, $pageResult->getPropertyKeys());
$this->assertCount(9, $pageResult->getRawProperties());
$this->assertCount(9, $pageResult->getProperties());
$this->assertCount(9, $pageResult->getPropertyKeys());

// check date and datetime properties
$this->assertTrue($pageResult->getProperty('DateWithTime')->hasTime());
$this->assertFalse($pageResult->getProperty('DateWithoutTime')->hasTime());

$this->assertInstanceOf(Carbon::class, $pageResult->getCreatedTime());
$this->assertInstanceOf(Carbon::class, $pageResult->getLastEditedTime());
Expand Down Expand Up @@ -106,9 +110,11 @@ public function it_assembles_properties_for_a_new_page()
$checkboxKey = 'CheckboxProperty';
$checkboxValue = true;
$dateRangeKey = 'DateRangeProperty';
$dateTimeRangeKey = 'DateTimeRangeProperty';
$dateRangeStartValue = Carbon::now()->toDateTime();
$dateRangeEndValue = Carbon::tomorrow()->toDateTime();
$dateKey = 'DateProperty';
$dateTimeKey = 'DateTimeProperty';
$dateValue = Carbon::yesterday()->toDateTime();
$emailKey = 'EmailProperty';
$emailValue = 'notion-is-awesome@example.org';
Expand All @@ -135,7 +141,9 @@ public function it_assembles_properties_for_a_new_page()
$page->setTitle('Name', $pageTitle);
$page->setCheckbox($checkboxKey, $checkboxValue);
$page->setDate($dateRangeKey, $dateRangeStartValue, $dateRangeEndValue);
$page->setDateTime($dateTimeRangeKey, $dateRangeStartValue, $dateRangeEndValue);
$page->setDate($dateKey, $dateValue);
$page->setDateTime($dateTimeKey, $dateValue);
$page->setEmail($emailKey, $emailValue);
$page->setMultiSelect($multiSelectKey, $multiSelectValues);
$page->setNumber($numberKey, $numberValue);
Expand Down Expand Up @@ -175,27 +183,65 @@ public function it_assembles_properties_for_a_new_page()
$this->assertTrue($dateRangeProp->isRange());
$this->assertEquals($dateRangeStartValue, $dateRangeProp->getStart());
$this->assertEquals($dateRangeEndValue, $dateRangeProp->getEnd());
$this->assertFalse($dateRangeProp->hasTime());
$this->assertJson($dateRangeProp->asText());
$this->assertStringContainsString($dateRangeStartValue->format('Y-m-d H:i:s'), $dateRangeProp->asText());
$this->assertStringContainsString($dateRangeEndValue->format('Y-m-d H:i:s'), $dateRangeProp->asText());
$dateRangeContent = $dateRangeProp->getRawContent();
$this->assertArrayHasKey('date', $dateRangeContent);
$this->assertCount(2, $dateRangeContent['date']);
$this->assertArrayHasKey('start', $dateRangeContent['date']);
$this->assertEquals($dateRangeStartValue->format('c'), $dateRangeContent['date']['start']);
$this->assertEquals($dateRangeStartValue->format('Y-m-d'), $dateRangeContent['date']['start']);
$this->assertArrayHasKey('end', $dateRangeContent['date']);
$this->assertEquals($dateRangeEndValue->format('c'), $dateRangeContent['date']['end']);
$this->assertEquals($dateRangeEndValue->format('Y-m-d'), $dateRangeContent['date']['end']);

// date range (with time)
$this->assertTrue(
$this->assertContainsInstanceOf(Date::class, $properties)
);
$dateTimeRangeProp = $page->getProperty($dateTimeRangeKey);
$this->assertInstanceOf(RichDate::class, $dateTimeRangeProp->getContent());
$dateTimeRangeContent = $dateTimeRangeProp->getContent();
$this->assertTrue($dateTimeRangeProp->isRange());
$this->assertEquals($dateRangeStartValue, $dateTimeRangeProp->getStart());
$this->assertEquals($dateRangeEndValue, $dateTimeRangeProp->getEnd());
$this->assertTrue($dateTimeRangeProp->hasTime());
$this->assertJson($dateTimeRangeProp->asText());
$this->assertStringContainsString($dateRangeStartValue->format('Y-m-d H:i:s'), $dateTimeRangeProp->asText());
$this->assertStringContainsString($dateRangeEndValue->format('Y-m-d H:i:s'), $dateTimeRangeProp->asText());
$dateTimeRangeContent = $dateTimeRangeProp->getRawContent();
$this->assertArrayHasKey('date', $dateTimeRangeContent);
$this->assertCount(2, $dateTimeRangeContent['date']);
$this->assertArrayHasKey('start', $dateTimeRangeContent['date']);
$this->assertEquals($dateRangeStartValue->format('c'), $dateTimeRangeContent['date']['start']);
$this->assertArrayHasKey('end', $dateTimeRangeContent['date']);
$this->assertEquals($dateRangeEndValue->format('c'), $dateTimeRangeContent['date']['end']);

// date
$dateProp = $page->getProperty($dateKey);
$this->assertInstanceOf(RichDate::class, $dateProp->getContent());
$this->assertFalse($dateProp->isRange());
$this->assertEquals($dateValue, $dateProp->getStart());
$this->assertNull($dateProp->getEnd());
$this->assertFalse($dateProp->hasTime());
$dateContent = $dateProp->getRawContent();
$this->assertArrayHasKey('date', $dateContent);
$this->assertCount(1, $dateContent['date']);
$this->assertArrayHasKey('start', $dateContent['date']);
$this->assertEquals($dateValue->format('c'), $dateContent['date']['start']);
$this->assertEquals($dateValue->format('Y-m-d'), $dateContent['date']['start']);

// date (with time)
$dateTimeProp = $page->getProperty($dateTimeKey);
$this->assertInstanceOf(RichDate::class, $dateTimeProp->getContent());
$this->assertFalse($dateTimeProp->isRange());
$this->assertEquals($dateValue, $dateTimeProp->getStart());
$this->assertNull($dateTimeProp->getEnd());
$this->assertTrue($dateTimeProp->hasTime());
$dateTimeContent = $dateTimeProp->getRawContent();
$this->assertArrayHasKey('date', $dateTimeContent);
$this->assertCount(1, $dateTimeContent['date']);
$this->assertArrayHasKey('start', $dateTimeContent['date']);
$this->assertEquals($dateValue->format('c'), $dateTimeContent['date']['start']);

// email
$this->assertTrue($this->assertContainsInstanceOf(Email::class, $properties));
Expand Down
18 changes: 18 additions & 0 deletions tests/stubs/endpoints/pages/response_specific_200.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@
}
]
},
"DateWithTime":{
"id": ">d{D",
"type": "date",
"date": {
"start": "2021-05-14T00:00:00.000+00:00",
"end": "2021-06-14T00:00:00.000+00:00",
"time_zone": null
}
},
"DateWithoutTime":{
"id": ">c{d",
"type": "date",
"date": {
"start": "2021-05-14",
"end": "2021-06-14",
"time_zone": null
}
},
"SelectColumn": {
"id": "nKff",
"type": "select",
Expand Down