Files
myep/src/Form/Service/Trait/FormTraversalTrait.php
T

46 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service\Trait;
use App\Form\Model\BookingDto;
use Symfony\Component\Form\FormInterface;
/**
* Trait providing form tree traversal utilities.
*
* This trait contains common form navigation logic used across different
* form services and types. It centralizes the logic for finding root forms
* and extracting booking DTOs from form hierarchies.
*
* Note: Card-based flows pass BookingDto via form options instead of
* relying on form tree traversal.
*/
trait FormTraversalTrait
{
/**
* Gets the BookingDto from the root of the form tree.
*
* This helper method traverses up the form tree to find the root form
* and extracts the BookingDto data. This is used as a fallback when
* BookingDto is not passed explicitly via form options.
*
* @param FormInterface $form The form to start traversing from
*
* @return BookingDto|null The booking DTO or null if not found
*/
public function getBookingDtoFromForm(FormInterface $form): ?BookingDto
{
// Traverse up the form tree to get the root form's data
$rootForm = $form;
while ($rootForm->getParent()) {
$rootForm = $rootForm->getParent();
}
$data = $rootForm->getData();
return $data instanceof BookingDto ? $data : null;
}
}