Skip to content

Commit

Permalink
Use BookingAvailability inside legacy BookingInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
LucWollants committed Nov 25, 2024
1 parent 090a48e commit 8e1366b
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions src/BookingInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use CultuurNet\UDB3\Model\Serializer\ValueObject\Web\TranslatedWebsiteLabelDenormalizer;
use CultuurNet\UDB3\Model\Serializer\ValueObject\Web\TranslatedWebsiteLabelNormalizer;
use CultuurNet\UDB3\Model\ValueObject\Contact\BookingAvailability;
use CultuurNet\UDB3\Model\ValueObject\Contact\BookingInfo as Udb3ModelBookingInfo;
use CultuurNet\UDB3\Model\ValueObject\Contact\TelephoneNumber;
use CultuurNet\UDB3\Model\ValueObject\Web\EmailAddress;
use CultuurNet\UDB3\Model\ValueObject\Web\TranslatedWebsiteLabel;
use CultuurNet\UDB3\Model\ValueObject\Web\Url;
use CultuurNet\UDB3\Model\ValueObject\Web\WebsiteLink;
use DateTimeImmutable;

/**
* @deprecated
Expand All @@ -26,22 +26,18 @@ final class BookingInfo implements JsonLdSerializableInterface

private ?WebsiteLink $website;

private ?DateTimeImmutable $availabilityStarts;

private ?DateTimeImmutable $availabilityEnds;
private ?BookingAvailability $bookingAvailability;

public function __construct(
?WebsiteLink $website = null,
?TelephoneNumber $phone = null,
?EmailAddress $email = null,
?DateTimeImmutable $availabilityStarts = null,
?DateTimeImmutable $availabilityEnds = null
?BookingAvailability $bookingAvailability = null
) {
$this->website = $website;
$this->phone = $phone;
$this->email = $email;
$this->availabilityStarts = $availabilityStarts;
$this->availabilityEnds = $availabilityEnds;
$this->bookingAvailability = $bookingAvailability;
}

public function getPhone(): ?TelephoneNumber
Expand All @@ -59,14 +55,9 @@ public function getWebsite(): ?WebsiteLink
return $this->website;
}

public function getAvailabilityStarts(): ?DateTimeImmutable
public function getAvailability(): ?BookingAvailability
{
return $this->availabilityStarts;
}

public function getAvailabilityEnds(): ?DateTimeImmutable
{
return $this->availabilityEnds;
return $this->bookingAvailability;
}

public function serialize(): array
Expand All @@ -79,12 +70,12 @@ public function serialize(): array
]
);

if ($this->availabilityStarts) {
$serialized['availabilityStarts'] = $this->availabilityStarts->format(\DATE_ATOM);
if ($this->bookingAvailability && $this->bookingAvailability->getFrom()) {
$serialized['availabilityStarts'] = $this->bookingAvailability->getFrom()->format(\DATE_ATOM);
}

if ($this->availabilityEnds) {
$serialized['availabilityEnds'] = $this->availabilityEnds->format(\DATE_ATOM);
if ($this->bookingAvailability && $this->bookingAvailability->getTo()) {
$serialized['availabilityEnds'] = $this->bookingAvailability->getTo()->format(\DATE_ATOM);
}

if ($this->website) {
Expand All @@ -107,16 +98,6 @@ public static function deserialize(array $data): BookingInfo

$data = array_merge($defaults, $data);

$availabilityStarts = null;
if ($data['availabilityStarts']) {
$availabilityStarts = DateTimeFactory::fromISO8601($data['availabilityStarts']);
}

$availabilityEnds = null;
if ($data['availabilityEnds']) {
$availabilityEnds = DateTimeFactory::fromISO8601($data['availabilityEnds']);
}

$website = null;
if (!empty($data['url']) && !empty($data['urlLabel'])) {
$url = new Url($data['url']);
Expand All @@ -134,8 +115,7 @@ public static function deserialize(array $data): BookingInfo
$website,
!empty($data['phone']) ? new TelephoneNumber($data['phone']) : null,
!empty($data['email']) ? new EmailAddress($data['email']) : null,
$availabilityStarts,
$availabilityEnds
self::createBookingAvailability($data['availabilityStarts'], $data['availabilityEnds'])
);
}

Expand All @@ -151,35 +131,28 @@ public function sameAs(BookingInfo $otherBookingInfo): bool

public static function fromUdb3ModelBookingInfo(Udb3ModelBookingInfo $udb3ModelBookingInfo): BookingInfo
{
$website = null;
$phone = null;
$email = null;
$availabilityStarts = null;
$availabilityEnds = null;

if ($udb3ModelWebsite = $udb3ModelBookingInfo->getWebsite()) {
$website = $udb3ModelWebsite;
}
return new self(
$udb3ModelBookingInfo->getWebsite(),
$udb3ModelBookingInfo->getTelephoneNumber(),
$udb3ModelBookingInfo->getEmailAddress(),
$udb3ModelBookingInfo->getAvailability()
);
}

if ($udb3ModelPhone = $udb3ModelBookingInfo->getTelephoneNumber()) {
$phone = $udb3ModelPhone;
private static function createBookingAvailability(?string $from, ?string $to): ?BookingAvailability
{
if ($from && $to) {
return BookingAvailability::fromTo(DateTimeFactory::fromISO8601($from), DateTimeFactory::fromISO8601($to));
}

if ($udb3ModelEmail = $udb3ModelBookingInfo->getEmailAddress()) {
$email = $udb3ModelEmail;
if ($from) {
return BookingAvailability::from(DateTimeFactory::fromISO8601($from));
}

if ($udb3ModelAvailability = $udb3ModelBookingInfo->getAvailability()) {
$availabilityStarts = $udb3ModelAvailability->getFrom();
$availabilityEnds = $udb3ModelAvailability->getTo();
if ($to) {
return BookingAvailability::to(DateTimeFactory::fromISO8601($to));
}

return new self(
$website,
$phone,
$email,
$availabilityStarts,
$availabilityEnds
);
return null;
}
}

0 comments on commit 8e1366b

Please sign in to comment.