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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ app/config/parameters.yml

# Composer
composer.phar
composer.lock

#PhpStorm
.idea
Expand Down
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
41 changes: 11 additions & 30 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,18 @@

/**
* Data transformer for multiple mode (i.e., multiple = true)
*
* Class EntitiesToPropertyTransformer
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
*/
class EntitiesToPropertyTransformer implements DataTransformerInterface
{
/** @var ObjectManager */
protected $em;
/** @var string */
protected $className;
/** @var string */
protected $textProperty;
/** @var string */
protected $primaryKey;
/** @var string */
protected $newTagPrefix;
/** @var string */
protected $newTagText;
/** @var PropertyAccessor */
protected $accessor;
protected ObjectManager $em;
protected string $className;
protected ?string $textProperty;
protected string $primaryKey;
protected string $newTagPrefix;
protected string $newTagText;
protected PropertyAccessor $accessor;

/**
* @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, string $class, ?string $textProperty = null, string $primaryKey = 'id', string $newTagPrefix = '__', $newTagText = ' (NEW)')
{
$this->em = $em;
$this->className = $class;
Expand All @@ -53,9 +36,8 @@ 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($entities): array
{
if (empty($entities)) {
return array();
Expand Down Expand Up @@ -85,9 +67,8 @@ public function transform($entities)
* Transform array to a collection of entities
*
* @param array $values
* @return array
*/
public function reverseTransform($values)
public function reverseTransform($values): array
{
if (!is_array($values) || empty($values)) {
return array();
Expand Down Expand Up @@ -117,7 +98,7 @@ public function reverseTransform($values)
->getResult();

// this will happen if the form submits invalid data
if (count($entities) != count($values)) {
if (count($entities) !== count($values)) {
throw new TransformationFailedException('One or more id values are invalid');
}

Expand Down
37 changes: 9 additions & 28 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,18 @@

/**
* Data transformer for single mode (i.e., multiple = false)
*
* Class EntityToPropertyTransformer
*
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
*/
class EntityToPropertyTransformer implements DataTransformerInterface
{
/** @var ObjectManager */
protected $em;
/** @var string */
protected $className;
/** @var string */
protected $textProperty;
/** @var string */
protected $primaryKey;
/** @var string */
protected $newTagPrefix;
/** @var string */
protected ObjectManager $em;
protected string $className;
protected ?string $textProperty;
protected string $primaryKey;
protected string $newTagPrefix;
protected $newTagText;
/** @var PropertyAccessor */
protected $accessor;
protected PropertyAccessor $accessor;

/**
* @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, string $class, string $textProperty = null, string $primaryKey = 'id', string $newTagPrefix = '__', $newTagText = ' (NEW)')
{
$this->em = $em;
$this->className = $class;
Expand All @@ -54,9 +36,8 @@ 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($entity): array
{
$data = array();
if (empty($entity)) {
Expand All @@ -71,7 +52,7 @@ public function transform($entity)
$value = (string) $this->accessor->getValue($entity, $this->primaryKey);
} else {
$value = $this->newTagPrefix . $text;
$text = $text.$this->newTagText;
$text .= $this->newTagText;
}

$data[$value] = $text;
Expand Down
42 changes: 11 additions & 31 deletions Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,22 @@
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
*
* Class Select2EntityType
* @package Tetranz\Select2EntityBundle\Form\Type
*/
class Select2EntityType extends AbstractType
{
/** @var ManagerRegistry */
protected $registry;
/** @var ObjectManager */
protected $em;
/** @var RouterInterface */
protected $router;
/** @var array */
protected $config;

/**
* @param ManagerRegistry $registry
* @param RouterInterface $router
* @param array $config
*/
public function __construct(ManagerRegistry $registry, RouterInterface $router, $config)
protected ManagerRegistry $registry;
protected ObjectManager $em;
protected RouterInterface $router;
protected array $config;

public function __construct(ManagerRegistry $registry, RouterInterface $router, array $config)
{
$this->registry = $registry;
$this->em = $registry->getManager();
$this->router = $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 +83,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 @@ -134,10 +120,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
$view->vars['class_type'] = $options['class_type'];
}

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'object_manager' => null,
Expand All @@ -163,7 +146,7 @@ public function configureOptions(OptionsResolver $resolver)
'text_property' => null,
'placeholder' => false,
'language' => $this->config['language'],
'theme' => $this->config['theme'],
'theme' => $this->config['theme'],
'required' => false,
'cache' => $this->config['cache'],
'cache_timeout' => $this->config['cache_timeout'],
Expand All @@ -180,10 +163,7 @@ public function configureOptions(OptionsResolver $resolver)
);
}

/**
* @return string
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'tetranz_select2entity';
}
Expand Down
20 changes: 3 additions & 17 deletions Service/AutocompleteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,19 @@

class AutocompleteService
{
/**
* @var FormFactoryInterface
*/
private $formFactory;
private FormFactoryInterface $formFactory;
private ManagerRegistry $doctrine;

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

/**
* @param FormFactoryInterface $formFactory
* @param ManagerRegistry $doctrine
*/
public function __construct(FormFactoryInterface $formFactory, ManagerRegistry $doctrine)
{
$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): array
{
$form = $this->formFactory->create($type);
$fieldOptions = $form->get($request->get('field_name'))->getConfig()->getOptions();
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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"],
"keywords": ["symfony", "select2", "typeahead", "autocomplete"],
"license": "MIT",
"authors": [
{
Expand All @@ -11,7 +11,8 @@
}
],
"require": {
"php": ">=7.1.3",
"php": ">=7.4",
"ext-json": "*",
"doctrine/orm": ">=2.4",
"twig/twig": ">=2.9",
"symfony/property-access": ">=4.0",
Expand All @@ -23,9 +24,5 @@
},
"autoload": {
"psr-4": { "Tetranz\\Select2EntityBundle\\": "" }
},
"extra": {
"branch-alias": {
}
}
}