src/Form/Type/BackOffice/AnalysisMethodSimulationType.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\BackOffice;
  3. use App\Entity\Assay\AnalysisMethod;
  4. use Closure;
  5. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Component\Validator\Constraints\NotNull;
  10. class AnalysisMethodSimulationType extends AbstractType
  11. {
  12.     public function buildForm(FormBuilderInterface $builder, array $options)
  13.     {
  14.         $builder->add(
  15.             'analysisMethod',
  16.             EntityType::class,
  17.             [
  18.                 'class' => AnalysisMethod::class,
  19.                 'choices' => $options['analysisMethods'],
  20.                 'choice_label' => Closure::fromCallable([$this'analysisMethodChoiceLabel']),
  21.                 'constraints' => [new NotNull()],
  22.             ]
  23.         );
  24.     }
  25.     public function configureOptions(OptionsResolver $resolver)
  26.     {
  27.         $resolver->setDefaults(
  28.             [
  29.                 'analysisMethods' => null,
  30.             ]
  31.         );
  32.     }
  33.     /** @noinspection PhpUnusedPrivateMethodInspection */
  34.     private function analysisMethodChoiceLabel(?AnalysisMethod $analysisMethod): string
  35.     {
  36.         if (!$analysisMethod) {
  37.             return '';
  38.         }
  39.         return sprintf(
  40.             '[%s] %s (%s)',
  41.             $analysisMethod->getRef(),
  42.             $analysisMethod->getName(),
  43.             $analysisMethod->getVersion()
  44.         );
  45.     }
  46.     /** @noinspection PhpUnusedPrivateMethodInspection */
  47.     private function analysisMethodChoiceValue(?AnalysisMethod $analysisMethod): string
  48.     {
  49.         if (!$analysisMethod) {
  50.             return '';
  51.         }
  52.         return $analysisMethod->getAnalysisMethodId();
  53.     }
  54. }