Files
myep/src/Form/ChoiceLoader/ParticipantRoomChoiceLoader.php
T

81 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\ChoiceLoader;
use App\Form\Model\RoomSelectionDto;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
/**
* Choice loader for participant room assignments.
*
* Generates room choices for each participant from all selected room types
* in Step 1, regardless of current capacity or assignments. Capacity conflicts
* are automatically resolved by the field handler through intelligent
* auto-unassignment when needed.
*/
class ParticipantRoomChoiceLoader implements ChoiceLoaderInterface
{
private ?ChoiceListInterface $choiceList = null;
/**
* @param RoomSelectionDto[] $selectedRooms the rooms selected in the previous step
*/
public function __construct(
private readonly ChoiceListFactoryInterface $factory,
private readonly array $selectedRooms,
) {
}
public function loadChoiceList(?callable $value = null): ChoiceListInterface
{
if (null === $this->choiceList) {
$choices = $this->generateRoomChoicesForParticipant();
$this->choiceList = $this->factory->createListFromChoices($choices, $value);
}
return $this->choiceList;
}
public function loadChoicesForValues(array $values, ?callable $value = null): array
{
if (empty($values)) {
return [];
}
return $this->loadChoiceList($value)->getChoicesForValues($values);
}
public function loadValuesForChoices(array $choices, ?callable $value = null): array
{
if (empty($choices)) {
return [];
}
return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
/**
* Generates room choices for the specific participant.
*
* Always includes all selected rooms from Step 1 regardless of current capacity.
* Capacity conflicts are handled by the field handler during assignment.
*
* @return array<string, int>
*/
private function generateRoomChoicesForParticipant(): array
{
$participantRoomChoices = [];
foreach ($this->selectedRooms as $roomSelection) {
// Always include all selected rooms - capacity conflicts handled in field handler
$participantRoomChoices[$roomSelection->roomLabel] = $roomSelection->roomId;
}
return $participantRoomChoices;
}
}