Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make compatible to symfony 6 #191

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/TetranzSelect2EntityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 23 additions & 17 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,7 +58,7 @@ 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)
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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');
}
Expand Down
63 changes: 35 additions & 28 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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)
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -183,7 +183,7 @@ public function configureOptions(OptionsResolver $resolver)
/**
* @return string
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'tetranz_select2entity';
}
Expand Down
4 changes: 2 additions & 2 deletions Service/AutocompleteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class AutocompleteService
/**
* @var FormFactoryInterface
*/
private $formFactory;
private FormFactoryInterface $formFactory;

/**
* @var ManagerRegistry
*/
private $doctrine;
private ManagerRegistry $doctrine;

/**
* @param FormFactoryInterface $formFactory
Expand Down
1 change: 1 addition & 0 deletions TetranzSelect2EntityBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tetranz\Select2EntityBundle;


use Symfony\Component\HttpKernel\Bundle\Bundle;

class TetranzSelect2EntityBundle extends Bundle
Expand Down
63 changes: 35 additions & 28 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
}
}
}