← All changes
|
src/EventTickets/DataTransferObjects/TicketPurchaseData.php
+19
-3
4.15.0
→
4.16.9
View file →
| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\EventTickets\DataTransferObjects; |
| 4 | 4 | |
| 5 | +use Give\EventTickets\Models\Event; | |
| 5 | 6 | use Give\EventTickets\Models\EventTicketType; |
| 7 | +use Give\Framework\Exceptions\Primitives\InvalidArgumentException; | |
| 8 | +use Give\Framework\Support\Facades\DateTime\Temporal; | |
| 6 | 9 | use stdClass; |
| 7 | 10 | |
| 8 | 11 | /** |
| 9 | 12 | * @since 3.6.0 |
| @@ -30,16 +33,29 @@ | ||
| 30 | 33 | return $this->$name; |
| 31 | 34 | } |
| 32 | 35 | |
| 33 | 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. | |
| 34 | 38 | * @since 3.6.0 |
| 39 | + * | |
| 40 | + * @throws InvalidArgumentException | |
| 35 | 41 | */ |
| 36 | - public static function fromFieldValueObject(stdClass $object): self | |
| 42 | + public static function fromFieldValueObject(stdClass $object, Event $event): self | |
| 37 | 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 | + | |
| 38 | 54 | $self = new self(); |
| 39 | 55 | |
| 40 | - $self->quantity = $object->quantity; | |
| 41 | - $self->ticketType = EventTicketType::find($object->ticketId); | |
| 56 | + $self->quantity = max(0, (int) ($object->quantity ?? 0)); | |
| 57 | + $self->ticketType = $ticketType; | |
| 42 | 58 | |
| 43 | 59 | return $self; |
| 44 | 60 | } |
| 45 | 61 | } |