1010
1111/**
1212 * Data transformer for multiple mode (i.e., multiple = true)
13- *
14- * Class EntitiesToPropertyTransformer
15- * @package Tetranz\Select2EntityBundle\Form\DataTransformer
1613 */
1714class 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
0 commit comments