Skip to content

Commit

Permalink
feat(appointment): add calculated and base start/end-date #176
Browse files Browse the repository at this point in the history
  • Loading branch information
DumbergerL committed Nov 2, 2023
1 parent 0a62fa1 commit d44154a
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 29 deletions.
126 changes: 97 additions & 29 deletions src/Models/Calendars/Appointment/Appointment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use CTApi\Models\AbstractModel;
use CTApi\Models\Calendars\Calendar\Calendar;
use CTApi\Models\Common\Domain\Meta;
use CTApi\Traits\Model\FillWithData;
use CTApi\Models\Common\File\File;
use CTApi\Traits\Model\FillWithData;
use CTApi\Utils\CTDateTimeService;

class Appointment extends AbstractModel
Expand All @@ -24,8 +24,14 @@ class Appointment extends AbstractModel
protected ?File $image = null;
protected ?string $link = null;
protected ?bool $isInternal = null;
protected ?string $startDate = null;
protected ?string $endDate = null;

protected ?string $base_startDate = null;
protected ?string $base_endDate = null;

protected ?string $calculated_startDate = null;
protected ?string $calculated_endDate = null;


protected ?string $allDay = null;
protected ?string $repeatId = null;
protected ?string $repeatFrequency = null;
Expand All @@ -37,9 +43,11 @@ protected function fillArrayType(string $key, array $data): void
{
switch ($key) {
case "base":
$this->processStartAndEndDate(true, $data);
$this->fillWithData($data); // inline "base"-array attributes
break;
case "calculated":
$this->processStartAndEndDate(false, $data);
$this->fillWithData($data); // inline "calculated"-array attributes
break;
case "calendar":
Expand All @@ -59,14 +67,48 @@ protected function fillArrayType(string $key, array $data): void
}
}

private function processStartAndEndDate(bool $isBase, array &$data)
{
if ($isBase) {
$this->base_startDate = $data['startDate'] ?? null;
$this->base_endDate = $data['endDate'] ?? null;
} else {
$this->calculated_startDate = $data['startDate'] ?? null;
$this->calculated_endDate = $data['endDate'] ?? null;
}

unset($data['startDate']);
unset($data['endDate']);
}

public function getStartDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->startDate);
return CTDateTimeService::stringToDateTime($this->getStartDate());
}

public function getEndDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->endDate);
return CTDateTimeService::stringToDateTime($this->getEndDate());
}

public function getCalculatedStartDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->getCalculatedStartDate());
}

public function getCalculatedEndDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->getCalculatedEndDate());
}

public function getBaseStartDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->getBaseStartDate());
}

public function getBaseEndDateAsDateTime(): ?\DateTimeImmutable
{
return CTDateTimeService::stringToDateTime($this->getBaseEndDate());
}

/**
Expand Down Expand Up @@ -111,10 +153,10 @@ public function getImage(): ?File
*/
public function setImage(?File $image): Appointment
{
$this->image= $image;
$this->image = $image;
return $this;
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -246,35 +288,21 @@ public function setIsInternal(?bool $isInternal): Appointment
*/
public function getStartDate(): ?string
{
return $this->startDate;
}

/**
* @param string|null $startDate
* @return Appointment
*/
public function setStartDate(?string $startDate): Appointment
{
$this->startDate = $startDate;
return $this;
if ($this->calculated_startDate != null) {
return $this->calculated_startDate;
}
return $this->base_startDate;
}

/**
* @return string|null
*/
public function getEndDate(): ?string
{
return $this->endDate;
}

/**
* @param string|null $endDate
* @return Appointment
*/
public function setEndDate(?string $endDate): Appointment
{
$this->endDate = $endDate;
return $this;
if ($this->calculated_endDate != null) {
return $this->calculated_endDate;
}
return $this->base_endDate;
}

/**
Expand Down Expand Up @@ -384,4 +412,44 @@ public function setMeta(?Meta $meta): Appointment
$this->meta = $meta;
return $this;
}

public function getBaseStartDate(): ?string
{
return $this->base_startDate;
}

public function setBaseStartDate(?string $base_startDate): void
{
$this->base_startDate = $base_startDate;
}

public function getBaseEndDate(): ?string
{
return $this->base_endDate;
}

public function setBaseEndDate(?string $base_endDate): void
{
$this->base_endDate = $base_endDate;
}

public function getCalculatedStartDate(): ?string
{
return $this->calculated_startDate;
}

public function setCalculatedStartDate(?string $calculated_startDate): void
{
$this->calculated_startDate = $calculated_startDate;
}

public function getCalculatedEndDate(): ?string
{
return $this->calculated_endDate;
}

public function setCalculatedEndDate(?string $calculated_endDate): void
{
$this->calculated_endDate = $calculated_endDate;
}
}
90 changes: 90 additions & 0 deletions tests/Unit/Models/AppointmentModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace CTApi\Test\Unit\Models;

use CTApi\Models\Calendars\Appointment\Appointment;
use CTApi\Utils\CTDateTimeService;
use Monolog\Test\TestCase;

class AppointmentModelTest extends TestCase
{

public function testProcessBaseAttributes()
{
$dataA = [
"base" => [
"id" => 848,
"caption" => "Service",
"startDate" => "2022-08-07T15:00:00Z",
"endDate" => "2022-08-07T16:00:00Z",
],
];

$appointment = Appointment::createModelFromData($dataA);

$this->assertEquals(848, $appointment->getId());
$this->assertEquals("Service", $appointment->getCaption());
$this->assertEquals("2022-08-07T15:00:00Z", $appointment->getStartDate());
$this->assertEquals("2022-08-07T16:00:00Z", $appointment->getEndDate());
}

public function testOverwriteBaseAttributes()
{
$dataA = [
"base" => [
"id" => 848,
"caption" => "Service",
"startDate" => "2022-08-07T15:00:00Z",
"endDate" => "2022-08-07T16:00:00Z",
],
"calculated" => [
"startDate" => "2022-01-07T15:00:00Z",
"endDate" => "2022-01-07T16:00:00Z",
]
];

$appointment = Appointment::createModelFromData($dataA);

$this->assertEquals(848, $appointment->getId());
$this->assertEquals("Service", $appointment->getCaption());
$this->assertEquals("2022-01-07T15:00:00Z", $appointment->getStartDate());
$this->assertEquals("2022-01-07T16:00:00Z", $appointment->getEndDate());
}

public function testOverwriteBaseAttributes_WrongOrder()
{
$dataA = [
"calculated" => [
"startDate" => "2022-01-07T15:00:00Z",
"endDate" => "2022-01-07T16:00:00Z",
],
"base" => [
"id" => 848,
"caption" => "Service",
"startDate" => "2022-08-07T15:00:00Z",
"endDate" => "2022-08-07T16:00:00Z",
]
];

$appointment = Appointment::createModelFromData($dataA);

$this->assertEquals(848, $appointment->getId());
$this->assertEquals("Service", $appointment->getCaption());
$this->assertEquals("2022-01-07T15:00:00Z", $appointment->getStartDate());
$this->assertEquals("2022-01-07T16:00:00Z", $appointment->getEndDate());

$this->assertEquals(CTDateTimeService::stringToDateTime("2022-01-07T15:00:00Z"), $appointment->getStartDateAsDateTime());
$this->assertEquals(CTDateTimeService::stringToDateTime("2022-01-07T16:00:00Z"), $appointment->getEndDateAsDateTime());

// Advanced properties base / calculated
$this->assertEquals("2022-01-07T15:00:00Z", $appointment->getCalculatedStartDate());
$this->assertEquals("2022-01-07T16:00:00Z", $appointment->getCalculatedEndDate());
$this->assertEquals(CTDateTimeService::stringToDateTime("2022-01-07T15:00:00Z"), $appointment->getCalculatedStartDateAsDateTime());
$this->assertEquals(CTDateTimeService::stringToDateTime("2022-01-07T16:00:00Z"), $appointment->getCalculatedEndDateAsDateTime());

$this->assertEquals("2022-08-07T15:00:00Z", $appointment->getBaseStartDate());
$this->assertEquals("2022-08-07T16:00:00Z", $appointment->getBaseEndDate());
$this->assertEquals(CTDateTimeService::stringToDateTime("2022-08-07T15:00:00Z"), $appointment->getBaseStartDateAsDateTime());
$this->assertEquals(CTDateTimeService::stringToDateTime("2022-08-07T16:00:00Z"), $appointment->getBaseEndDateAsDateTime());
}
}

0 comments on commit d44154a

Please sign in to comment.