]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Form/Extension/Type/HiddenEntityType.php
Add support for bundle named entity
[airbundle] / Form / Extension / Type / HiddenEntityType.php
index 3e85cc66ae4c6c7c87b08b20185a063a8a7f840b..7521633c25b4ddb993b13436097eab101372a8db 100644 (file)
@@ -35,44 +35,46 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                $this->dm = $doctrine;
        }
 
-    /**
+       /**
         * Configure options
         *
-     * {@inheritdoc}
-     */
-    public function configureOptions(OptionsResolver $resolver) {
+        * {@inheritdoc}
+        */
+       public function configureOptions(OptionsResolver $resolver) {
                //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
         *
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options): void {
-               #$this->entityClass = sprintf('App\Entity\%s', ucfirst($builder->getName()));
-               #var_dump($builder->getName());
-               #$this->entityClass = sprintf('App\Entity\%s', ucfirst($builder->getName()));
-               //$this->dataClass[$builder->getName()] = $options['data_class'];
-
                //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
@@ -80,8 +82,14 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                $builder->addModelTransformer(clone $this);
        }
 
+       /**
+        * Transform data to string
+        *
+        * @param mixed $data The data object
+        * @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 '';
                }
@@ -91,6 +99,12 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                return $res;
        }
 
+       /**
+        * Reverse transformation from string to data object
+        *
+        * @param string $data The object id
+        * @return mixed The data object
+        */
        public function reverseTransform($data) {
                if (!$data) {
                        return null;
@@ -105,7 +119,7 @@ class HiddenEntityType extends HiddenType implements DataTransformerInterface {
                }
 
                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, $data));
                }
 
                return $res;