Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

5.0 KB
Raw Blame
<?php

namespace HiEvents\Resources\Event;

use HiEvents\DomainObjects\Enums\EventType;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventOccurrenceDomainObject;
use HiEvents\Resources\BaseResource;
use HiEvents\Resources\EventLocation\EventLocationResourcePublic;
use HiEvents\Resources\EventOccurrence\EventOccurrenceResourcePublic;
use HiEvents\Resources\Image\ImageResource;
use HiEvents\Resources\Organizer\OrganizerResourcePublic;
use HiEvents\Resources\ProductCategory\ProductCategoryResourcePublic;
use HiEvents\Resources\Question\QuestionResource;
use Illuminate\Http\Request;

/**
 * @mixin EventDomainObject
 */
class EventResourcePublic extends BaseResource
{
    private readonly bool $includePostCheckoutData;

    public function __construct(
        mixed $resource,
        mixed $includePostCheckoutData = false,
    ) {
        $this->includePostCheckoutData = is_bool($includePostCheckoutData)
            ? $includePostCheckoutData
            : false;

        parent::__construct($resource);
    }

    public function toArray(Request $request): array
    {
        $isRecurring = $this->getType() === EventType::RECURRING->name;

        return [
            'id' => $this->getId(),
            'title' => $this->getTitle(),
            'category' => $this->getCategory(),
            'description' => $this->getDescription(),
            'description_preview' => $this->getDescriptionPreview(),
            'start_date' => $this->getStartDate(),
            'end_date' => $this->getEndDate(),
            'next_occurrence_start_date' => $this->getNextOccurrenceStartDate(),
            'upcoming_occurrences_sold_out' => $this->getUpcomingOccurrencesSoldOut(),
            'has_seat_map' => $this->getHasSeatMap(),
            'products_sold_out' => $this->when(
                condition: ! is_null($this->getProductsSoldOut()),
                value: fn () => $this->getProductsSoldOut(),
            ),
            'last_occurrence_date' => $this->when($isRecurring, fn () => $this->getLastOccurrenceStartDate()),
            'occurrences_month' => $this->when($isRecurring, fn () => $this->getOccurrencesMonth()),
            /** @var 'SINGLE'|'RECURRING' */
            'type' => $this->getType(),
            'currency' => $this->getCurrency(),
            'slug' => $this->getSlug(),
            /** @var 'DRAFT'|'LIVE'|'ARCHIVED'|'PENDING_MANUAL_REVIEW'|null */
            'status' => $this->getStatus(),
            /** @var 'UPCOMING'|'ONGOING'|'ENDED' */
            'lifecycle_status' => $this->getLifecycleStatus(),
            'timezone' => $this->getTimezone(),
            'event_location' => $this->when(
                condition: $this->getEventLocation() !== null,
                value: fn () => new EventLocationResourcePublic($this->getEventLocation(), $this->includePostCheckoutData),
            ),
            'product_categories' => $this->when(
                condition: ! is_null($this->getProductCategories()) && $this->getProductCategories()->isNotEmpty(),
                value: fn () => ProductCategoryResourcePublic::collection($this->getProductCategories()),
            ),
            'settings' => $this->when(
                condition: ! is_null($this->getEventSettings()),
                value: fn () => new EventSettingsResourcePublic(
                    $this->getEventSettings(),
                    $this->includePostCheckoutData
                ),
            ),
            // @TODO - public question resource
            'questions' => $this->when(
                condition: ! is_null($this->getQuestions()),
                value: fn () => QuestionResource::collection($this->getQuestions())
            ),
            'attributes' => $this->when(
                condition: ! is_null($this->getAttributes()),
                value: fn () => collect($this->getAttributes())->reject(fn ($attribute) => ! $attribute['is_public'])),
            'images' => $this->when(
                condition: ! is_null($this->getImages()),
                value: fn () => ImageResource::collection($this->getImages())
            ),
            'organizer' => $this->when(
                condition: ! is_null($this->getOrganizer()),
                value: fn () => new OrganizerResourcePublic($this->getOrganizer()),
            ),
            'occurrences' => $this->when(
                condition: ! is_null($this->getEventOccurrences()) && $this->getEventOccurrences()->isNotEmpty(),
                value: function () use ($isRecurring) {
                    $showCapacity = $this->getEventSettings()?->getShowAvailableOccurrenceCapacity() ?? false;

                    return $this->getEventOccurrences()
                        ->filter(fn (EventOccurrenceDomainObject $occ) => ! $occ->isCancelled() && (! $isRecurring || ! $occ->isPast()))
                        ->sortBy(fn (EventOccurrenceDomainObject $occ) => $occ->getStartDate())
                        ->values()
                        ->map(fn (EventOccurrenceDomainObject $occ) => new EventOccurrenceResourcePublic($occ, $showCapacity));
                },
            ),
        ];
    }
}