Files

116 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\DTOs;
use Illuminate\Http\Request;
readonly class CreateDeliveryDTO
{
public function __construct(
public int $restaurantId,
public string $restaurantName,
public string $pickupAddress,
public float $pickupLatitude,
public float $pickupLongitude,
public string $customerName,
public string $dropAddress,
public float $dropLatitude,
public float $dropLongitude,
public string $dropContactPhone,
public ?int $zoneId = null,
public ?string $orderableType = null,
public ?int $orderableId = null,
public ?string $pickupContactName = null,
public ?string $pickupContactPhone = null,
public ?string $pickupInstructions = null,
public ?string $dropContactName = null,
public ?string $dropInstructions = null,
public ?string $dropFloor = null,
public ?string $dropApartment = null,
public bool $isScheduled = false,
public ?\DateTimeInterface $scheduledFor = null,
public bool $isPriority = false,
public ?float $orderValue = null,
public ?float $tipAmount = null,
public ?string $tipType = null,
public ?array $meta = null,
) {}
/**
* Create DTO from request.
*
* IMPORTANT: restaurant_id is NEVER passed from request.
* It is always obtained via getUserRestaurantId().
*/
public static function fromRequest(Request $request): self
{
return new self(
restaurantId: (int) getUserRestaurantId(),
restaurantName: $request->string('restaurant_name')->toString(),
pickupAddress: $request->string('pickup_address')->toString(),
pickupLatitude: (float) $request->input('pickup_latitude'),
pickupLongitude: (float) $request->input('pickup_longitude'),
customerName: $request->string('customer_name')->toString(),
dropAddress: $request->string('drop_address')->toString(),
dropLatitude: (float) $request->input('drop_latitude'),
dropLongitude: (float) $request->input('drop_longitude'),
dropContactPhone: $request->string('drop_contact_phone')->toString(),
zoneId: $request->filled('zone_id') ? $request->integer('zone_id') : null,
orderableType: $request->filled('orderable_type') ? $request->string('orderable_type')->toString() : null,
orderableId: $request->filled('orderable_id') ? $request->integer('orderable_id') : null,
pickupContactName: $request->filled('pickup_contact_name') ? $request->string('pickup_contact_name')->toString() : null,
pickupContactPhone: $request->filled('pickup_contact_phone') ? $request->string('pickup_contact_phone')->toString() : null,
pickupInstructions: $request->filled('pickup_instructions') ? $request->string('pickup_instructions')->toString() : null,
dropContactName: $request->filled('drop_contact_name') ? $request->string('drop_contact_name')->toString() : null,
dropInstructions: $request->filled('drop_instructions') ? $request->string('drop_instructions')->toString() : null,
dropFloor: $request->filled('drop_floor') ? $request->string('drop_floor')->toString() : null,
dropApartment: $request->filled('drop_apartment') ? $request->string('drop_apartment')->toString() : null,
isScheduled: $request->boolean('is_scheduled'),
scheduledFor: $request->filled('scheduled_for') ? new \DateTime($request->input('scheduled_for')) : null,
isPriority: $request->boolean('is_priority'),
orderValue: $request->filled('order_value') ? (float) $request->input('order_value') : null,
tipAmount: $request->filled('tip_amount') ? (float) $request->input('tip_amount') : null,
tipType: $request->filled('tip_type') ? $request->string('tip_type')->toString() : null,
meta: $request->input('meta'),
);
}
/**
* Convert to array for model creation.
*/
public function toArray(): array
{
return array_filter([
'restaurant_id' => $this->restaurantId,
'restaurant_name' => $this->restaurantName,
'pickup_address' => $this->pickupAddress,
'pickup_latitude' => $this->pickupLatitude,
'pickup_longitude' => $this->pickupLongitude,
'customer_name' => $this->customerName,
'drop_address' => $this->dropAddress,
'drop_latitude' => $this->dropLatitude,
'drop_longitude' => $this->dropLongitude,
'drop_contact_phone' => $this->dropContactPhone,
'zone_id' => $this->zoneId,
'orderable_type' => $this->orderableType,
'orderable_id' => $this->orderableId,
'pickup_contact_name' => $this->pickupContactName,
'pickup_contact_phone' => $this->pickupContactPhone,
'pickup_instructions' => $this->pickupInstructions,
'drop_contact_name' => $this->dropContactName,
'drop_instructions' => $this->dropInstructions,
'drop_floor' => $this->dropFloor,
'drop_apartment' => $this->dropApartment,
'is_scheduled' => $this->isScheduled,
'scheduled_for' => $this->scheduledFor?->format('Y-m-d H:i:s'),
'is_priority' => $this->isPriority,
'order_value' => $this->orderValue,
'tip_amount' => $this->tipAmount,
'tip_type' => $this->tipType,
'meta' => $this->meta,
], fn ($value) => $value !== null);
}
}