feat: show room prices in room assignment form field

This commit is contained in:
Björn Fromme
2025-12-09 12:55:00 +01:00
parent 9c6860ecc6
commit 31f414b4d8
22 changed files with 193 additions and 92 deletions
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace App\Form\DataTransformer;
use App\Form\Model\RoomSelectionDto;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
* Transforms between RoomSelectionDto objects and integer room IDs.
*
* This transformer enables using RoomSelectionDto objects as choices while
* binding integer IDs to the model. This allows the template to access
* the full object (including price) via choiceData while the form binds
* the scalar ID to the participant's assignedRoomId property.
*
* @implements DataTransformerInterface<int|null, RoomSelectionDto|null>
*/
class RoomSelectionToIdTransformer implements DataTransformerInterface
{
/**
* @param RoomSelectionDto[] $roomSelections Available room selections for reverse lookup
*/
public function __construct(
private readonly array $roomSelections,
) {
}
/**
* Transforms an integer room ID to a RoomSelectionDto for form display.
*
* @param int|null $value The room ID from the model
*
* @return RoomSelectionDto|null The matching RoomSelectionDto or null
*/
public function transform(mixed $value): ?RoomSelectionDto
{
if (null === $value) {
return null;
}
foreach ($this->roomSelections as $roomSelection) {
if ($roomSelection->id === $value) {
return $roomSelection;
}
}
return null;
}
/**
* Transforms a RoomSelectionDto back to an integer room ID for the model.
*
* @param RoomSelectionDto|null $value The selected RoomSelectionDto from the form
*
* @return int|null The room ID or null
*
* @throws TransformationFailedException If an unexpected value type is received
*/
public function reverseTransform(mixed $value): ?int
{
if (null === $value) {
return null;
}
if ($value instanceof RoomSelectionDto) {
return $value->id;
}
// Handle case where form submits scalar ID directly
if (is_int($value) || is_string($value)) {
return (int) $value;
}
throw new TransformationFailedException(sprintf('Expected RoomSelectionDto, int, or null, got %s', get_debug_type($value)));
}
}