1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/OSL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 25 */ 26 27namespace PrestaShopBundle\Form\Admin\Type; 28 29use PrestaShop\PrestaShop\Core\Form\FormChoiceAttributeProviderInterface; 30use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface; 31use Symfony\Component\Form\AbstractType; 32use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 33use Symfony\Component\Form\FormBuilderInterface; 34use Symfony\Component\OptionsResolver\OptionsResolver; 35 36/** 37 * Class CountryChoiceType is responsible for providing country choices with -- symbol in front of array. 38 */ 39class CountryChoiceType extends AbstractType 40{ 41 /** 42 * @var FormChoiceProviderInterface 43 */ 44 private $countriesChoiceProvider; 45 46 /** 47 * @var FormChoiceAttributeProviderInterface 48 */ 49 private $countriesAttrChoicesProvider; 50 51 /** 52 * @var array 53 */ 54 private $countriesAttr = []; 55 56 /** 57 * @var bool 58 */ 59 private $needDni = false; 60 61 /** 62 * @var bool 63 */ 64 private $needPostcode = false; 65 66 /** 67 * @param FormChoiceProviderInterface $countriesChoiceProvider 68 */ 69 public function __construct(FormChoiceProviderInterface $countriesChoiceProvider, FormChoiceAttributeProviderInterface $countriesAttrChoicesProvider) 70 { 71 $this->countriesChoiceProvider = $countriesChoiceProvider; 72 $this->countriesAttrChoicesProvider = $countriesAttrChoicesProvider; 73 } 74 75 public function buildForm(FormBuilderInterface $builder, array $options) 76 { 77 if ($options['with_dni_attr'] || $options['with_postcode_attr']) { 78 $this->needDni = $options['with_dni_attr']; 79 $this->needPostcode = $options['with_postcode_attr']; 80 $this->countriesAttr = $this->countriesAttrChoicesProvider->getChoicesAttributes(); 81 } 82 parent::buildForm($builder, $options); 83 } 84 85 /** 86 * {@inheritdoc} 87 */ 88 public function configureOptions(OptionsResolver $resolver) 89 { 90 $choices = array_merge( 91 ['--' => ''], 92 $this->countriesChoiceProvider->getChoices() 93 ); 94 95 $resolver->setDefaults([ 96 'choices' => $choices, 97 'choice_attr' => [$this, 'getChoiceAttr'], 98 'with_dni_attr' => false, 99 'with_postcode_attr' => false, 100 ]); 101 102 $resolver 103 ->setAllowedTypes('with_dni_attr', 'boolean') 104 ->setAllowedTypes('with_postcode_attr', 'boolean'); 105 } 106 107 public function getChoiceAttr($value, $key) 108 { 109 $attr = []; 110 if ($this->needDni && isset($this->countriesAttr[$key], $this->countriesAttr[$key]['need_dni'])) { 111 $attr['need_dni'] = 1; 112 } 113 if ($this->needPostcode && isset($this->countriesAttr[$key], $this->countriesAttr[$key]['need_postcode'])) { 114 $attr['need_postcode'] = 1; 115 } 116 117 return $attr; 118 } 119 120 /** 121 * {@inheritdoc} 122 */ 123 public function getParent() 124 { 125 return ChoiceType::class; 126 } 127} 128