| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\EventTickets\DataTransferObjects; |
| 4 |
|
| 5 |
use Give\EventTickets\Models\Event; |
| 6 |
use Give\EventTickets\Models\EventTicketType; |
| 7 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 8 |
use Give\Framework\Support\Facades\DateTime\Temporal; |
| 9 |
use stdClass; |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 3.6.0 |
| 13 |
*/ |
| 14 |
class TicketPurchaseData |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @since 3.6.0 |
| 18 |
* @var int |
| 19 |
*/ |
| 20 |
protected $quantity; |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 3.6.0 |
| 24 |
* @var EventTicketType |
| 25 |
*/ |
| 26 |
protected $ticketType; |
| 27 |
|
| 28 |
/** |
| 29 |
* @since 3.6.0 |
| 30 |
*/ |
| 31 |
public function __get($name) |
| 32 |
{ |
| 33 |
return $this->$name; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 4.16.8.1 Require the ticket type to belong to the given event and that event to not have already ended, and clamp quantity to a non-negative integer. |
| 38 |
* @since 3.6.0 |
| 39 |
* |
| 40 |
* @throws InvalidArgumentException |
| 41 |
*/ |
| 42 |
public static function fromFieldValueObject(stdClass $object, Event $event): self |
| 43 |
{ |
| 44 |
$ticketType = EventTicketType::find($object->ticketId ?? 0); |
| 45 |
|
| 46 |
if (!$ticketType || $ticketType->eventId !== $event->id) { |
| 47 |
throw new InvalidArgumentException('Ticket type does not belong to this event.'); |
| 48 |
} |
| 49 |
|
| 50 |
if ($event->endDateTime < Temporal::getCurrentDateTime()) { |
| 51 |
throw new InvalidArgumentException('Event has already ended.'); |
| 52 |
} |
| 53 |
|
| 54 |
$self = new self(); |
| 55 |
|
| 56 |
$self->quantity = max(0, (int) ($object->quantity ?? 0)); |
| 57 |
$self->ticketType = $ticketType; |
| 58 |
|
| 59 |
return $self; |
| 60 |
} |
| 61 |
} |
| 62 |
|