diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5bd9542..9e267c8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -15,7 +15,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritDoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('tetranz_select2_entity'); $rootNode = $treeBuilder->getRootNode(); diff --git a/DependencyInjection/TetranzSelect2EntityExtension.php b/DependencyInjection/TetranzSelect2EntityExtension.php index c1cd2a3..e64c73e 100644 --- a/DependencyInjection/TetranzSelect2EntityExtension.php +++ b/DependencyInjection/TetranzSelect2EntityExtension.php @@ -17,7 +17,7 @@ class TetranzSelect2EntityExtension extends Extension /** * {@inheritDoc} */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/Form/DataTransformer/EntitiesToPropertyTransformer.php b/Form/DataTransformer/EntitiesToPropertyTransformer.php index d6a9fd8..c66bcec 100644 --- a/Form/DataTransformer/EntitiesToPropertyTransformer.php +++ b/Form/DataTransformer/EntitiesToPropertyTransformer.php @@ -26,20 +26,26 @@ class EntitiesToPropertyTransformer implements DataTransformerInterface protected $primaryKey; /** @var string */ protected $newTagPrefix; - /** @var string */ + /** @var string */ protected $newTagText; /** @var PropertyAccessor */ protected $accessor; /** - * @param ObjectManager $em - * @param string $class - * @param string|null $textProperty - * @param string $primaryKey - * @param string $newTagPrefix + * @param ObjectManager $em + * @param string $class + * @param string|null $textProperty + * @param string $primaryKey + * @param string $newTagPrefix */ - public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)') - { + public function __construct( + ObjectManager $em, + $class, + $textProperty = null, + $primaryKey = 'id', + $newTagPrefix = '__', + $newTagText = ' (NEW)' + ) { $this->em = $em; $this->className = $class; $this->textProperty = $textProperty; @@ -52,10 +58,10 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr /** * Transform initial entities to array * - * @param mixed $entities + * @param mixed $entities * @return array */ - public function transform($entities) + public function transform($entities): array { if (empty($entities)) { return array(); @@ -65,13 +71,13 @@ public function transform($entities) foreach ($entities as $entity) { $text = is_null($this->textProperty) - ? (string) $entity + ? (string)$entity : $this->accessor->getValue($entity, $this->textProperty); if ($this->em->contains($entity)) { - $value = (string) $this->accessor->getValue($entity, $this->primaryKey); + $value = (string)$this->accessor->getValue($entity, $this->primaryKey); } else { - $value = $this->newTagPrefix . $text; + $value = $this->newTagPrefix.$text; $text = $text.$this->newTagText; } @@ -84,17 +90,17 @@ public function transform($entities) /** * Transform array to a collection of entities * - * @param array $values + * @param array $values * @return array */ - public function reverseTransform($values) + public function reverseTransform($values): array { if (!is_array($values) || empty($values)) { - return array(); + return []; } // add new tag entries - $newObjects = array(); + $newObjects = []; $tagPrefixLength = strlen($this->newTagPrefix); foreach ($values as $key => $value) { $cleanValue = substr($value, $tagPrefixLength); @@ -116,7 +122,7 @@ public function reverseTransform($values) ->getQuery() ->getResult(); - // this will happen if the form submits invalid data + // this will happen if the form submits invalid data if (count($entities) != count($values)) { throw new TransformationFailedException('One or more id values are invalid'); } diff --git a/Form/DataTransformer/EntityToPropertyTransformer.php b/Form/DataTransformer/EntityToPropertyTransformer.php index 72a30a9..e8d23ff 100644 --- a/Form/DataTransformer/EntityToPropertyTransformer.php +++ b/Form/DataTransformer/EntityToPropertyTransformer.php @@ -18,29 +18,35 @@ class EntityToPropertyTransformer implements DataTransformerInterface { /** @var ObjectManager */ - protected $em; + protected ObjectManager $em; /** @var string */ - protected $className; + protected string $className; /** @var string */ - protected $textProperty; + protected string $textProperty; /** @var string */ - protected $primaryKey; - /** @var string */ - protected $newTagPrefix; - /** @var string */ - protected $newTagText; + protected string $primaryKey; + /** @var string */ + protected string $newTagPrefix; + /** @var string */ + protected string $newTagText; /** @var PropertyAccessor */ - protected $accessor; + protected PropertyAccessor $accessor; /** - * @param ObjectManager $em - * @param string $class - * @param string|null $textProperty - * @param string $primaryKey - * @param string $newTagPrefix + * @param ObjectManager $em + * @param string $class + * @param string|null $textProperty + * @param string $primaryKey + * @param string $newTagPrefix */ - public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)') - { + public function __construct( + ObjectManager $em, + $class, + $textProperty = null, + $primaryKey = 'id', + $newTagPrefix = '__', + $newTagText = ' (NEW)' + ) { $this->em = $em; $this->className = $class; $this->textProperty = $textProperty; @@ -53,24 +59,24 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr /** * Transform entity to array * - * @param mixed $entity + * @param mixed $entity * @return array */ - public function transform($entity) + public function transform($entity): array { - $data = array(); + $data = []; if (empty($entity)) { return $data; } $text = is_null($this->textProperty) - ? (string) $entity + ? (string)$entity : $this->accessor->getValue($entity, $this->textProperty); if ($this->em->contains($entity)) { - $value = (string) $this->accessor->getValue($entity, $this->primaryKey); + $value = (string)$this->accessor->getValue($entity, $this->primaryKey); } else { - $value = $this->newTagPrefix . $text; + $value = $this->newTagPrefix.$text; $text = $text.$this->newTagText; } @@ -82,10 +88,10 @@ public function transform($entity) /** * Transform single id value to an entity * - * @param string $value - * @return mixed|null|object + * @param string $value + * @return null|object */ - public function reverseTransform($value) + public function reverseTransform($value): null|object { if (empty($value)) { return null; @@ -109,10 +115,11 @@ public function reverseTransform($value) ->setParameter('id', $value) ->getQuery() ->getSingleResult(); - } - catch (\Doctrine\ORM\UnexpectedResultException $ex) { + } catch (\Doctrine\ORM\UnexpectedResultException $ex) { // this will happen if the form submits invalid data - throw new TransformationFailedException(sprintf('The choice "%s" does not exist or is not unique', $value)); + throw new TransformationFailedException( + sprintf('The choice "%s" does not exist or is not unique', $value) + ); } } diff --git a/Form/Type/Select2EntityType.php b/Form/Type/Select2EntityType.php index ad7f8d4..a007f17 100644 --- a/Form/Type/Select2EntityType.php +++ b/Form/Type/Select2EntityType.php @@ -45,7 +45,7 @@ public function __construct(ManagerRegistry $registry, RouterInterface $router, $this->config = $config; } - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { // custom object manager for this entity, override the default entity manager ? if (isset($options['object_manager'])) { @@ -97,7 +97,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder->addViewTransformer($transformer, true); } - public function finishView(FormView $view, FormInterface $form, array $options) + public function finishView(FormView $view, FormInterface $form, array $options): void { parent::finishView($view, $form, $options); // make variables available to the view @@ -137,7 +137,7 @@ public function finishView(FormView $view, FormInterface $form, array $options) /** * @param OptionsResolver $resolver */ - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'object_manager' => null, @@ -183,7 +183,7 @@ public function configureOptions(OptionsResolver $resolver) /** * @return string */ - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'tetranz_select2entity'; } diff --git a/Service/AutocompleteService.php b/Service/AutocompleteService.php index c4ba50a..de2bddd 100644 --- a/Service/AutocompleteService.php +++ b/Service/AutocompleteService.php @@ -14,12 +14,12 @@ class AutocompleteService /** * @var FormFactoryInterface */ - private $formFactory; + private FormFactoryInterface $formFactory; /** * @var ManagerRegistry */ - private $doctrine; + private ManagerRegistry $doctrine; /** * @param FormFactoryInterface $formFactory diff --git a/TetranzSelect2EntityBundle.php b/TetranzSelect2EntityBundle.php index a327226..499f746 100644 --- a/TetranzSelect2EntityBundle.php +++ b/TetranzSelect2EntityBundle.php @@ -2,6 +2,7 @@ namespace Tetranz\Select2EntityBundle; + use Symfony\Component\HttpKernel\Bundle\Bundle; class TetranzSelect2EntityBundle extends Bundle diff --git a/composer.json b/composer.json index 7a23ddd..3d0707c 100644 --- a/composer.json +++ b/composer.json @@ -1,31 +1,38 @@ { - "name": "tetranz/select2entity-bundle", - "type": "symfony-bundle", - "description": "A Symfony bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.", - "keywords": ["symfony","select2", "typeahead","autocomplete"], - "license": "MIT", - "authors": [ - { - "name": "Ross Keatinge", - "email": "tetranz@gmail.com" - } - ], - "require": { - "php": ">=7.1.3", - "doctrine/orm": ">=2.4", - "twig/twig": ">=2.9", - "symfony/property-access": ">=4.0", - "symfony/dependency-injection": ">=4.0", - "symfony/http-kernel": ">=4.0", - "symfony/form": ">=4.0", - "symfony/config": ">=4.0", - "symfony/routing": ">=4.0" - }, - "autoload": { - "psr-4": { "Tetranz\\Select2EntityBundle\\": "" } - }, - "extra": { - "branch-alias": { - } + "name": "tetranz/select2entity-bundle", + "type": "symfony-bundle", + "description": "A Symfony bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.", + "keywords": [ + "symfony", + "select2", + "typeahead", + "autocomplete" + ], + "license": "MIT", + "authors": [ + { + "name": "Ross Keatinge", + "email": "tetranz@gmail.com" } + ], + "require": { + "php": ">=8.0", + "doctrine/orm": ">=2.4", + "twig/twig": ">=2.9", + "symfony/property-access": ">=4.0", + "symfony/dependency-injection": ">=4.0", + "symfony/http-kernel": ">=4.0", + "symfony/form": ">=4.0", + "symfony/config": ">=4.0", + "symfony/routing": ">=4.0" + }, + "autoload": { + "psr-4": { + "Tetranz\\Select2EntityBundle\\": "" + } + }, + "extra": { + "branch-alias": { + } + } }