Skip to content
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
38 changes: 16 additions & 22 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,53 @@ 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;
}

/**
* 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]);
}
}

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

Expand Down
37 changes: 17 additions & 20 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
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
23 changes: 18 additions & 5 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 All @@ -29,17 +29,17 @@ public function __construct(FormFactoryInterface $formFactory, ManagerRegistry $
{
$this->formFactory = $formFactory;
$this->doctrine = $doctrine;
}
}

/**
* @param Request $request
* @param string|FormTypeInterface $type
*
* @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 */
Expand All @@ -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);
}

Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\\": "" }
Expand Down