]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Form/Extension/Type/HiddenEntityType.php
Reorder constructor arguments
[airbundle] / Form / Extension / Type / HiddenEntityType.php
index 2dfabf7fccd8b22f38ca312bd3f701c678692f21..f9b27d1447bd06c0e0b6b4a74c33a8ae42b2e9f0 100644 (file)
@@ -35,26 +35,26 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                $this->dm = $doctrine;
        }
 
-    /**
+       /**
         * Configure options
         *
-     * {@inheritdoc}
-     */
-    public function configureOptions(OptionsResolver $resolver) {
+        * {@inheritdoc}
+        */
+       public function configureOptions(OptionsResolver $resolver): void {
                //Call parent
-        parent::configureOptions($resolver);
+               parent::configureOptions($resolver);
 
-        // Derive "data_class" option from passed "data" object
-        $class = function (Options $options) {
-            return isset($options['data']) && \is_object($options['data']) ? \get_class($options['data']) : null;
-        };
+               //Derive "data_class" option from passed "data" object
+               $class = function (Options $options) {
+                       return isset($options['data']) && \is_object($options['data']) ? \get_class($options['data']) : null;
+               };
 
-        $resolver->setDefaults([
-            'class' => $class
+               $resolver->setDefaults([
+                       #'data_class' => null,
+                       'class' => $class
                ]);
        }
 
-
        /**
         * Build form
         *
@@ -64,10 +64,17 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                //Set class from options['class']
                $this->class = $options['class'];
 
-               //Check class
+               //Without class
                if (empty($this->class)) {
                        //Set class from namespace and field name
                        $this->class = str_replace('Form\\Extension\\Type', 'Entity\\' ,__NAMESPACE__).ucfirst($builder->getName());
+               //With bundle named entity
+               } elseif (
+                       ($pos = strpos($this->class, ':')) !== false &&
+                       !empty($entity = substr($this->class, $pos + 1))
+               ) {
+                       //Set class from namespace and entity name
+                       $this->class = str_replace('Form\\Extension\\Type', 'Entity\\' ,__NAMESPACE__).$entity;
                }
 
                //Set this as model transformer
@@ -82,7 +89,7 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
         * @return string The object id
         */
        public function transform($data): string {
-               // Modified from comments to use instanceof so that base classes or interfaces can be specified
+               //Modified from comments to use instanceof so that base classes or interfaces can be specified
                if ($data === null || !$data instanceof $this->class) {
                        return '';
                }
@@ -95,24 +102,24 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
        /**
         * Reverse transformation from string to data object
         *
-        * @param string $data The object id
+        * @param mixed $value The object id
         * @return mixed The data object
         */
-       public function reverseTransform($data) {
-               if (!$data) {
+       public function reverseTransform(mixed $value): mixed {
+               if (!$value) {
                        return null;
                }
 
                $res = null;
                try {
                        $rep = $this->dm->getRepository($this->class);
-                       $res = $rep->findOneById($data);
+                       $res = $rep->findOneById($value);
                } catch (\Exception $e) {
                        throw new TransformationFailedException($e->getMessage());
                }
 
                if ($res === null) {
-                       throw new TransformationFailedException(sprintf('A %s with id %s does not exist!', $this->class, $data));
+                       throw new TransformationFailedException(sprintf('A %s with id %s does not exist!', $this->class, $value));
                }
 
                return $res;