81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Exception\ParticipantNotFoundException;
|
|
use App\Form\Model\BookingDto;
|
|
use App\Form\Model\ParticipantDto;
|
|
use App\Form\Model\ParticipantEditDto;
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
|
|
/**
|
|
* Shared helpers for participant edit forms in create and edit booking flows.
|
|
*/
|
|
class ParticipantFormSupportService
|
|
{
|
|
public function __construct(
|
|
private readonly ParameterBagInterface $parameterBag,
|
|
) {
|
|
}
|
|
|
|
public function ensureParticipantExists(BookingDto $bookingDto, int $index): ParticipantDto
|
|
{
|
|
$participant = $bookingDto->participants[$index] ?? null;
|
|
|
|
if (null === $participant) {
|
|
throw new ParticipantNotFoundException($index);
|
|
}
|
|
|
|
return $participant;
|
|
}
|
|
|
|
public function createParticipantEditDto(BookingDto $bookingDto, int $index): ParticipantEditDto
|
|
{
|
|
return new ParticipantEditDto(
|
|
participant: $this->ensureParticipantExists($bookingDto, $index),
|
|
bookingContext: $bookingDto,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function getParticipantFormOptions(BookingDto $bookingDto, bool $disableValidation = false): array
|
|
{
|
|
$options = [
|
|
'booking_context' => $bookingDto,
|
|
'height_choices' => $this->parameterBag->get('body_dimensions.height_choices'),
|
|
'weight_choices' => $this->parameterBag->get('body_dimensions.weight_choices'),
|
|
'shoe_size_min' => $this->parameterBag->get('body_dimensions.shoe_size_min'),
|
|
'shoe_size_max' => $this->parameterBag->get('body_dimensions.shoe_size_max'),
|
|
];
|
|
|
|
if (true === $disableValidation) {
|
|
$options['validation_groups'] = false;
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Collects participant notifications and clears them from the DTO.
|
|
*
|
|
* @return array<array{type: string, message: string}>
|
|
*/
|
|
public function collectAndClearNotifications(BookingDto $bookingDto): array
|
|
{
|
|
$notifications = [];
|
|
|
|
foreach ($bookingDto->participants as $participant) {
|
|
if (false === empty($participant->notifications)) {
|
|
$notifications = array_merge($notifications, $participant->notifications);
|
|
$participant->notifications = [];
|
|
}
|
|
}
|
|
|
|
return array_values($notifications);
|
|
}
|
|
}
|