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..0fdc381 100644 --- a/Form/DataTransformer/EntitiesToPropertyTransformer.php +++ b/Form/DataTransformer/EntitiesToPropertyTransformer.php @@ -51,31 +51,28 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr /** * Transform initial entities to array - * - * @param mixed $entities - * @return array */ - public function transform($entities) + public function transform(mixed $value): mixed { - if (empty($entities)) { + if (empty($value)) { return array(); } $data = array(); - foreach ($entities as $entity) { + foreach ($value as $entity) { $text = is_null($this->textProperty) ? (string) $entity : $this->accessor->getValue($entity, $this->textProperty); if ($this->em->contains($entity)) { - $value = (string) $this->accessor->getValue($entity, $this->primaryKey); + $transformed = (string) $this->accessor->getValue($entity, $this->primaryKey); } else { - $value = $this->newTagPrefix . $text; + $transformed = $this->newTagPrefix . $text; $text = $text.$this->newTagText; } - $data[$value] = $text; + $data[$transformed] = $text; } return $data; @@ -83,27 +80,24 @@ public function transform($entities) /** * Transform array to a collection of entities - * - * @param array $values - * @return array */ - public function reverseTransform($values) + public function reverseTransform(mixed $value): mixed { - if (!is_array($values) || empty($values)) { - return array(); + if (!is_array($value) || empty($value)) { + return []; } // add new tag entries - $newObjects = array(); + $newObjects = []; $tagPrefixLength = strlen($this->newTagPrefix); - foreach ($values as $key => $value) { - $cleanValue = substr($value, $tagPrefixLength); - $valuePrefix = substr($value, 0, $tagPrefixLength); + foreach ($value as $key => $v) { + $cleanValue = substr($v, $tagPrefixLength); + $valuePrefix = substr($v, 0, $tagPrefixLength); if ($valuePrefix == $this->newTagPrefix) { $object = new $this->className; $this->accessor->setValue($object, $this->textProperty, $cleanValue); $newObjects[] = $object; - unset($values[$key]); + unset($value[$key]); } } @@ -112,12 +106,12 @@ public function reverseTransform($values) ->select('entity') ->from($this->className, 'entity') ->where('entity.'.$this->primaryKey.' IN (:ids)') - ->setParameter('ids', $values) + ->setParameter('ids', $value) ->getQuery() ->getResult(); // this will happen if the form submits invalid data - if (count($entities) != count($values)) { + if (count($entities) != count($value)) { throw new TransformationFailedException('One or more id values are invalid'); } diff --git a/Form/DataTransformer/EntityToPropertyTransformer.php b/Form/DataTransformer/EntityToPropertyTransformer.php index 72a30a9..23368de 100644 --- a/Form/DataTransformer/EntityToPropertyTransformer.php +++ b/Form/DataTransformer/EntityToPropertyTransformer.php @@ -18,19 +18,19 @@ 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; + protected string $primaryKey; /** @var string */ - protected $newTagPrefix; + protected string $newTagPrefix; /** @var string */ - protected $newTagText; + protected string $newTagText; /** @var PropertyAccessor */ - protected $accessor; + protected PropertyAccessor $accessor; /** * @param ObjectManager $em @@ -52,29 +52,26 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr /** * Transform entity to array - * - * @param mixed $entity - * @return array */ - public function transform($entity) + public function transform(mixed $value): mixed { - $data = array(); - if (empty($entity)) { + $data = []; + if (empty($value)) { return $data; } $text = is_null($this->textProperty) - ? (string) $entity - : $this->accessor->getValue($entity, $this->textProperty); + ? (string) $value + : $this->accessor->getValue($value, $this->textProperty); - if ($this->em->contains($entity)) { - $value = (string) $this->accessor->getValue($entity, $this->primaryKey); + if ($this->em->contains($value)) { + $transformed = (string) $this->accessor->getValue($value, $this->primaryKey); } else { - $value = $this->newTagPrefix . $text; + $transformed = $this->newTagPrefix . $text; $text = $text.$this->newTagText; } - $data[$value] = $text; + $data[$transformed] = $text; return $data; } @@ -85,7 +82,7 @@ public function transform($entity) * @param string $value * @return mixed|null|object */ - public function reverseTransform($value) + public function reverseTransform(mixed $value): mixed { if (empty($value)) { return null; 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..0c000f5 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 @@ -29,7 +29,7 @@ public function __construct(FormFactoryInterface $formFactory, ManagerRegistry $ { $this->formFactory = $formFactory; $this->doctrine = $doctrine; - } + } /** * @param Request $request @@ -37,9 +37,9 @@ public function __construct(FormFactoryInterface $formFactory, ManagerRegistry $ * * @return array */ - public function getAutocompleteResults(Request $request, $type) + public function getAutocompleteResults(Request $request, $type, $data = null) { - $form = $this->formFactory->create($type); + $form = $this->formFactory->create($type, $data); $fieldOptions = $form->get($request->get('field_name'))->getConfig()->getOptions(); /** @var EntityRepository $repo */ @@ -65,10 +65,23 @@ public function getAutocompleteResults(Request $request, $type) ->setFirstResult($offset) ; + if ($request->get('exclude') && $exclude = explode(',', $request->get('exclude'))) { + if (\count($exclude) > 0) { + $resultQb + ->andWhere('e.id NOT IN (:exclude)') + ->setParameter('exclude', $exclude); + } + } + if (is_callable($fieldOptions['callback'])) { $cb = $fieldOptions['callback']; $cb($countQB, $request); + //reset joined select from count + $countQB->resetDQLPart('select'); + $countQB + ->addSelect($countQB->expr()->count('e')); + $cb($resultQb, $request); } diff --git a/composer.json b/composer.json index 7a23ddd..542fa3d 100644 --- a/composer.json +++ b/composer.json @@ -11,15 +11,15 @@ } ], "require": { - "php": ">=7.1.3", + "php": ">=8.1", "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" + "symfony/property-access": ">=6.4", + "symfony/dependency-injection": ">=6.4", + "symfony/http-kernel": ">=6.4", + "symfony/form": ">=6.4", + "symfony/config": ">=6.4", + "symfony/routing": ">=6.4" }, "autoload": { "psr-4": { "Tetranz\\Select2EntityBundle\\": "" }