Skip to content

Commit

Permalink
Refactored the getFormTypeFqcn() method
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Dec 21, 2015
1 parent c17bcdc commit c4dfcf7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
22 changes: 13 additions & 9 deletions Form/Type/EasyAdminFormType.php
Expand Up @@ -185,7 +185,7 @@ private function getAttributesNormalizer(array $config)
}

/**
* It returns the FQCN of the given short type.
* It returns the FQCN of the given short type name.
* Example: 'text' -> 'Symfony\Component\Form\Extension\Core\Type\TextType'
*
* @param string $shortType
Expand All @@ -194,24 +194,28 @@ private function getAttributesNormalizer(array $config)
*/
private function getFormTypeFqcn($shortType)
{
$typeNames = array(
$builtinTypes = array(
'birthday', 'button', 'checkbox', 'choice', 'collection', 'country',
'currency', 'datetime', 'date', 'email', 'entity', 'file', 'form',
'hidden', 'integer', 'language', 'locale', 'money', 'number',
'password', 'percent', 'radio', 'range', 'repeated', 'reset',
'search', 'submit', 'textarea', 'text', 'time', 'timezone', 'url',
);

if (!in_array($shortType, $typeNames)) {
if (!in_array($shortType, $builtinTypes)) {
return $shortType;
}

return 'entity' === $shortType
? 'Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType'
: ('datetime' === $shortType
? 'Symfony\\Component\\Form\\Extension\\Core\\Type\\DateTimeType'
: sprintf('Symfony\\Component\\Form\\Extension\\Core\\Type\\%sType', ucfirst($shortType))
);
$irregularTypeFqcn = array(
'entity' => 'Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType',
'datetime' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\DateTimeType',
);

if (array_key_exists($shortType, $irregularTypeFqcn)) {
return $irregularTypeFqcn[$shortType];
}

return sprintf('Symfony\\Component\\Form\\Extension\\Core\\Type\\%sType', ucfirst($shortType));
}

/**
Expand Down
21 changes: 16 additions & 5 deletions Tests/Form/Type/EasyAdminFormTypeTest.php
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JavierEguiluz\Bundle\EasyAdminBundle\Tests\Form\Type;

use JavierEguiluz\Bundle\EasyAdminBundle\Form\Type\EasyAdminFormType;
Expand All @@ -9,11 +18,11 @@ class EasyAdminFormTypeTest extends \PHPUnit_Framework_TestCase
public function shortTypesToFqcnProvider()
{
return array(
'Symfony native form type' => array('integer', 'Symfony\Component\Form\Extension\Core\Type\IntegerType'),
'Symfony DateTime form type' => array('datetime', 'Symfony\Component\Form\Extension\Core\Type\DateTimeType'),
'Doctrine Bridge Entity form type' => array('entity', 'Symfony\Bridge\Doctrine\Form\Type\EntityType'),
'Custom form type' => array('foo', 'foo'),
'FQCN' => array('Foo\Bar', 'Foo\Bar'),
'Symfony Type (regular name)' => array('integer', 'Symfony\Component\Form\Extension\Core\Type\IntegerType'),
'Symfony Type (irregular name)' => array('datetime', 'Symfony\Component\Form\Extension\Core\Type\DateTimeType'),
'Doctrine Bridge Type' => array('entity', 'Symfony\Bridge\Doctrine\Form\Type\EntityType'),
'Custom Type (short name)' => array('foo', 'foo'),
'Custom Type (FQCN)' => array('Foo\Bar', 'Foo\Bar'),
);
}

Expand All @@ -30,7 +39,9 @@ public function testGetFormTypeFqcn($shortType, $expected)

$method = new \ReflectionMethod($type, 'getFormTypeFqcn');
$method->setAccessible(true);

$this->assertSame($expected, $method->invoke($type, $shortType));

$method->setAccessible(false);
}
}

0 comments on commit c4dfcf7

Please sign in to comment.