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 ⭐️

3.6 KB
Raw Blame
<?php

namespace HiEvents\Resources\Product;

use HiEvents\DomainObjects\Enums\ProductPriceType;
use HiEvents\DomainObjects\ProductDomainObject;
use HiEvents\Resources\Tax\TaxAndFeeResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin ProductDomainObject
 */
class ProductResource extends JsonResource
{
    public const DEFAULT_MIN_PRODUCTS = 1;

    public const DEFAULT_MAX_PRODUCTS = 10;

    public function toArray(Request $request): array
    {
        return [
            'id' => $this->getId(),
            'title' => $this->getTitle(),
            /** @var 'PAID'|'FREE'|'DONATION'|'TIERED'|'REGISTRATION' */
            'type' => $this->getType(),
            /** @var 'TICKET'|'GENERAL' */
            'product_type' => $this->getProductType(),
            'order' => $this->getOrder(),
            'description' => $this->getDescription(),
            $this->mergeWhen(
                condition: $this->getType() !== ProductPriceType::TIERED->name,
                value: fn () => [
                    'price' => $this->getPrice(),
                ]
            ),
            'max_per_order' => $this->getMaxPerOrder() ?? self::DEFAULT_MAX_PRODUCTS,
            'min_per_order' => $this->getMinPerOrder() ?? self::DEFAULT_MIN_PRODUCTS,
            'quantity_sold' => $this->getQuantitySold(),
            'sale_start_date' => $this->getSaleStartDate(),
            'sale_end_date' => $this->getSaleEndDate(),
            'event_id' => $this->getEventId(),
            'initial_quantity_available' => $this->getInitialQuantityAvailable(),
            'hide_before_sale_start_date' => $this->getHideBeforeSaleStartDate(),
            'hide_after_sale_end_date' => $this->getHideAfterSaleEndDate(),
            'start_collapsed' => $this->getStartCollapsed(),
            'show_quantity_remaining' => $this->getShowQuantityRemaining(),
            'hide_when_sold_out' => $this->getHideWhenSoldOut(),
            'sequential_tier_release_enabled' => $this->getSequentialTierReleaseEnabled(),
            'is_hidden_without_promo_code' => $this->getIsHiddenWithoutPromoCode(),
            'is_hidden' => $this->getIsHidden(),
            'is_before_sale_start_date' => $this->isBeforeSaleStartDate(),
            'is_after_sale_end_date' => $this->isAfterSaleEndDate(),
            'is_available' => $this->isAvailable(),
            $this->mergeWhen((bool) $this->getProductPrices(), fn () => [
                'is_sold_out' => $this->isSoldOut(),
            ]),
            'taxes_and_fees' => $this->when(
                (bool) $this->getTaxAndFees(),
                fn () => TaxAndFeeResource::collection($this->getTaxAndFees())
            ),
            'prices' => $this->when(
                (bool) $this->getProductPrices(),
                fn () => ProductPriceResource::collection($this->getProductPrices())
            ),
            'product_category_id' => $this->getProductCategoryId(),
            'is_highlighted' => $this->getIsHighlighted(),
            'highlight_message' => $this->getHighlightMessage(),
            'waitlist_enabled' => $this->getWaitlistEnabled(),
            'is_addon_only' => $this->getIsAddonOnly(),
            'addon_product_ids' => $this->when(
                $this->getAddons() !== null,
                fn () => $this->getAddonProductIds(),
            ),
            'addons' => $this->when(
                $this->getAddons() !== null,
                fn () => $this->getAddons()->map(fn (ProductDomainObject $addon) => [
                    'id' => $addon->getId(),
                    'title' => $addon->getTitle(),
                ]),
            ),
        ];
    }
}