Files
myep-team/src/Form/ChoiceLoader/BpnCountryChoiceLoader.php
T

40 lines
1.2 KiB
PHP

<?php
namespace App\Form\ChoiceLoader;
use App\BusProNet\DataProvider\CountryDataProvider;
use App\BusProNet\Model\Country;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class BpnCountryChoiceLoader implements ChoiceLoaderInterface
{
public function __construct(private readonly CountryDataProvider $countries, private readonly string $property)
{}
public function loadChoiceList(callable $value = null): ChoiceListInterface
{
$choices = [];
/** @var Country[] $countries */
$countries = $this->countries->getAll();
foreach ($countries as $country) {
$key = 'nationality' === $this->property ? $country->getNationality() : $country->getName();
$choices[$key] = $country->getToken();
}
return new ArrayChoiceList($choices);
}
public function loadChoicesForValues(array $values, callable $value = null): array
{
return $values;
}
public function loadValuesForChoices(array $choices, callable $value = null): array
{
return $choices;
}
}