Skip to content

Commit 64fe9c2

Browse files
authored
Merge pull request #5 from SebLevDev/feature/symfony7
Feature/symfony7
2 parents 01ffc3c + 9866245 commit 64fe9c2

File tree

4 files changed

+39
-110
lines changed

4 files changed

+39
-110
lines changed

Form/DataTransformer/EntitiesToPropertyTransformer.php

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,19 @@
1010

1111
/**
1212
* Data transformer for multiple mode (i.e., multiple = true)
13-
*
14-
* Class EntitiesToPropertyTransformer
15-
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
1613
*/
1714
class EntitiesToPropertyTransformer implements DataTransformerInterface
1815
{
19-
/** @var ObjectManager */
20-
protected $em;
21-
/** @var string */
22-
protected $className;
23-
/** @var string */
24-
protected $textProperty;
25-
/** @var string */
26-
protected $primaryKey;
27-
/** @var string */
28-
protected $newTagPrefix;
29-
/** @var string */
30-
protected $newTagText;
31-
/** @var PropertyAccessor */
32-
protected $accessor;
16+
protected ObjectManager $em;
17+
protected string $className;
18+
protected string $textProperty;
19+
protected string $primaryKey;
20+
protected string $newTagPrefix;
21+
protected string $newTagText;
22+
protected PropertyAccessor $accessor;
3323

34-
/**
35-
* @param ObjectManager $em
36-
* @param string $class
37-
* @param string|null $textProperty
38-
* @param string $primaryKey
39-
* @param string $newTagPrefix
40-
*/
41-
public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)')
24+
25+
public function __construct(ObjectManager $em, string $class, ?string $textProperty = null, string $primaryKey = 'id', string $newTagPrefix = '__', string $newTagText = ' (NEW)')
4226
{
4327
$this->em = $em;
4428
$this->className = $class;
@@ -51,11 +35,8 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr
5135

5236
/**
5337
* Transform initial entities to array
54-
*
55-
* @param mixed $entities
56-
* @return array
5738
*/
58-
public function transform($entities)
39+
public function transform(mixed $value): mixed
5940
{
6041
if (empty($entities)) {
6142
return array();
@@ -83,27 +64,24 @@ public function transform($entities)
8364

8465
/**
8566
* Transform array to a collection of entities
86-
*
87-
* @param array $values
88-
* @return array
8967
*/
90-
public function reverseTransform($values)
68+
public function reverseTransform(mixed $value): mixed
9169
{
92-
if (!is_array($values) || empty($values)) {
70+
if (!is_array($value) || empty($value)) {
9371
return array();
9472
}
9573

9674
// add new tag entries
9775
$newObjects = array();
9876
$tagPrefixLength = strlen($this->newTagPrefix);
99-
foreach ($values as $key => $value) {
100-
$cleanValue = substr($value, $tagPrefixLength);
101-
$valuePrefix = substr($value, 0, $tagPrefixLength);
77+
foreach ($value as $key => $item) {
78+
$cleanValue = substr($item, $tagPrefixLength);
79+
$valuePrefix = substr($item, 0, $tagPrefixLength);
10280
if ($valuePrefix == $this->newTagPrefix) {
10381
$object = new $this->className;
10482
$this->accessor->setValue($object, $this->textProperty, $cleanValue);
10583
$newObjects[] = $object;
106-
unset($values[$key]);
84+
unset($value[$key]);
10785
}
10886
}
10987

@@ -112,12 +90,12 @@ public function reverseTransform($values)
11290
->select('entity')
11391
->from($this->className, 'entity')
11492
->where('entity.'.$this->primaryKey.' IN (:ids)')
115-
->setParameter('ids', $values)
93+
->setParameter('ids', $value)
11694
->getQuery()
11795
->getResult();
11896

11997
// this will happen if the form submits invalid data
120-
if (count($entities) != count($values)) {
98+
if (count($entities) != count($value)) {
12199
throw new TransformationFailedException('One or more id values are invalid');
122100
}
123101

Form/DataTransformer/EntityToPropertyTransformer.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,18 @@
1010

1111
/**
1212
* Data transformer for single mode (i.e., multiple = false)
13-
*
14-
* Class EntityToPropertyTransformer
15-
*
16-
* @package Tetranz\Select2EntityBundle\Form\DataTransformer
1713
*/
1814
class EntityToPropertyTransformer implements DataTransformerInterface
1915
{
20-
/** @var ObjectManager */
21-
protected $em;
22-
/** @var string */
23-
protected $className;
24-
/** @var string */
25-
protected $textProperty;
26-
/** @var string */
27-
protected $primaryKey;
28-
/** @var string */
29-
protected $newTagPrefix;
30-
/** @var string */
31-
protected $newTagText;
32-
/** @var PropertyAccessor */
33-
protected $accessor;
16+
protected ObjectManager $em;
17+
protected string $className;
18+
protected ?string $textProperty;
19+
protected string $primaryKey;
20+
protected string $newTagPrefix;
21+
protected string $newTagText;
22+
protected PropertyAccessor $accessor;
3423

35-
/**
36-
* @param ObjectManager $em
37-
* @param string $class
38-
* @param string|null $textProperty
39-
* @param string $primaryKey
40-
* @param string $newTagPrefix
41-
*/
42-
public function __construct(ObjectManager $em, $class, $textProperty = null, $primaryKey = 'id', $newTagPrefix = '__', $newTagText = ' (NEW)')
24+
public function __construct(ObjectManager $em, string $class, ?string $textProperty = null, string $primaryKey = 'id', string $newTagPrefix = '__', string $newTagText = ' (NEW)')
4325
{
4426
$this->em = $em;
4527
$this->className = $class;
@@ -52,11 +34,8 @@ public function __construct(ObjectManager $em, $class, $textProperty = null, $pr
5234

5335
/**
5436
* Transform entity to array
55-
*
56-
* @param mixed $entity
57-
* @return array
5837
*/
59-
public function transform($entity)
38+
public function transform(mixed $value): mixed
6039
{
6140
$data = array();
6241
if (empty($entity)) {
@@ -81,11 +60,8 @@ public function transform($entity)
8160

8261
/**
8362
* Transform single id value to an entity
84-
*
85-
* @param string $value
86-
* @return mixed|null|object
8763
*/
88-
public function reverseTransform($value)
64+
public function reverseTransform(mixed $value): mixed
8965
{
9066
if (empty($value)) {
9167
return null;

Form/Type/Select2EntityType.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,12 @@
1616
use Symfony\Component\Routing\RouterInterface;
1717
use Symfony\Component\PropertyAccess\PropertyAccess;
1818

19-
/**
20-
*
21-
* Class Select2EntityType
22-
* @package Tetranz\Select2EntityBundle\Form\Type
23-
*/
2419
class Select2EntityType extends AbstractType
2520
{
26-
/** @var ManagerRegistry */
27-
protected $registry;
28-
/** @var ObjectManager */
29-
protected $em;
30-
/** @var RouterInterface */
31-
protected $router;
32-
/** @var array */
33-
protected $config;
21+
protected ManagerRegistry $registry;
22+
protected ObjectManager $em;
23+
protected RouterInterface $router;
24+
protected array $config;
3425

3526
/**
3627
* @param ManagerRegistry $registry
@@ -163,7 +154,7 @@ public function configureOptions(OptionsResolver $resolver): void
163154
'text_property' => null,
164155
'placeholder' => false,
165156
'language' => $this->config['language'],
166-
'theme' => $this->config['theme'],
157+
'theme' => $this->config['theme'],
167158
'required' => false,
168159
'cache' => $this->config['cache'],
169160
'cache_timeout' => $this->config['cache_timeout'],
@@ -183,7 +174,7 @@ public function configureOptions(OptionsResolver $resolver): void
183174
/**
184175
* @return string
185176
*/
186-
public function getBlockPrefix()
177+
public function getBlockPrefix(): string
187178
{
188179
return 'tetranz_select2entity';
189180
}

Service/AutocompleteService.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,17 @@
1111

1212
class AutocompleteService
1313
{
14-
/**
15-
* @var FormFactoryInterface
16-
*/
17-
private $formFactory;
18-
19-
/**
20-
* @var ManagerRegistry
21-
*/
22-
private $doctrine;
23-
24-
/**
25-
* @param FormFactoryInterface $formFactory
26-
* @param ManagerRegistry $doctrine
27-
*/
14+
private FormFactoryInterface $formFactory;
15+
16+
private ManagerRegistry $doctrine;
17+
2818
public function __construct(FormFactoryInterface $formFactory, ManagerRegistry $doctrine)
2919
{
3020
$this->formFactory = $formFactory;
3121
$this->doctrine = $doctrine;
3222
}
3323

34-
/**
35-
* @param Request $request
36-
* @param string|FormTypeInterface $type
37-
*
38-
* @return array
39-
*/
40-
public function getAutocompleteResults(Request $request, $type)
24+
public function getAutocompleteResults(Request $request, string|FormTypeInterface $type): array
4125
{
4226
$form = $this->formFactory->create($type);
4327
$fieldOptions = $form->get($request->get('field_name'))->getConfig()->getOptions();

0 commit comments

Comments
 (0)