WIP: Implement profile editing
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\Form\ChoiceLoader\BpnCountryChoiceLoader;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceList;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class BpnCountryType extends AbstractType
|
||||
{
|
||||
public function __construct(private readonly ApiClient $apiClient)
|
||||
{}
|
||||
|
||||
public function getParent(): string
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefined(['property']);
|
||||
$resolver->setAllowedValues('property', ['country', 'nationality']);
|
||||
$resolver->setDefaults([
|
||||
'property' => 'country',
|
||||
'choice_loader' => function (Options $options) {
|
||||
return ChoiceList::loader(
|
||||
$this,
|
||||
new BpnCountryChoiceLoader($this->apiClient, $options['property']),
|
||||
[$options['property']]
|
||||
);
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form\ChoiceLoader;
|
||||
|
||||
use App\BusProNet\ApiClient;
|
||||
use App\BusProNet\ApiClientException;
|
||||
use App\BusProNet\Model\CountriesResponse;
|
||||
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 ApiClient $apiClient, private readonly string $property)
|
||||
{}
|
||||
|
||||
public function loadChoiceList(callable $value = null): ChoiceListInterface
|
||||
{
|
||||
try {
|
||||
/** @var CountriesResponse $response */
|
||||
$response = $this->apiClient->getCountries();
|
||||
$countries = $response->getCountries();
|
||||
} catch (ApiClientException $e) {
|
||||
$countries = [];
|
||||
}
|
||||
|
||||
$choices = [];
|
||||
|
||||
foreach ($countries as $country) {
|
||||
$key = 'nationality' === $this->property ? $country->getNationality() : $country->getName();
|
||||
$choices[$key] = $country->getId();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Teamer;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class ProfileType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('gender', ChoiceType::class, [
|
||||
'label' => 'Geschlecht',
|
||||
'choices' => [
|
||||
'weiblich' => 'W',
|
||||
'männlich' => 'M',
|
||||
'divers' => 'D',
|
||||
],
|
||||
])
|
||||
->add('firstName', TextType::class, [
|
||||
'label' => 'Vorname',
|
||||
])
|
||||
->add('lastName', TextType::class,[
|
||||
'label' => 'Nachname',
|
||||
])
|
||||
->add('academicTitle', TextType::class, [
|
||||
'label' => 'Titel',
|
||||
'required' => false,
|
||||
])
|
||||
->add('dateOfBirth', BirthdayType::class, [
|
||||
'label' => 'Geburtsdatum',
|
||||
'input_format' => 'datetime_immutable',
|
||||
])
|
||||
->add('nationality', BpnCountryType::class, [
|
||||
'label' => 'Nationalität',
|
||||
'property' => 'nationality',
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Teamer::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user