1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fresh\DoctrineEnumBundle\Util; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* LegacyFormHelper |
17
|
|
|
* |
18
|
|
|
* This class based on LegacyFormHelper in FOSUserBundle: |
19
|
|
|
* @see https://github.com/FriendsOfSymfony/FOSUserBundle/blob/c533a233b52c1d3843e816a35677561330ddbc74/Util/LegacyFormHelper.php |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @author Gabor Egyed <[email protected]> |
24
|
|
|
* @author Jaik Dean <[email protected]> |
25
|
|
|
* @author Artem Genvald <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class LegacyFormHelper |
28
|
|
|
{ |
29
|
|
|
const MINIMUM_MAJOR_VERSION = 3; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array $map Mapping form type classes to legacy form types |
33
|
|
|
* @static |
34
|
|
|
*/ |
35
|
|
|
private static $map = [ |
36
|
|
|
'Symfony\Component\Form\Extension\Core\Type\ChoiceType' => 'choice', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get a form field type compatible with the current version of Symfony |
41
|
|
|
* |
42
|
|
|
* @param string $class Class |
43
|
|
|
* @param int $majorVersion Major version |
44
|
|
|
* |
45
|
|
|
* @return string Class or type name |
46
|
|
|
*/ |
47
|
|
|
public static function getType($class, $majorVersion = Kernel::MAJOR_VERSION) |
48
|
|
|
{ |
49
|
|
|
if (!self::isLegacy($majorVersion)) { |
50
|
|
|
return $class; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!isset(self::$map[$class])) { |
54
|
|
|
throw new \InvalidArgumentException( |
55
|
|
|
sprintf( |
56
|
|
|
'Form type with class "%s" can not be found. Please check for typos or add it to the map in LegacyFormHelper', |
57
|
|
|
$class |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return self::$map[$class]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check whether to use legacy form behaviour from Symfony <3.0 |
67
|
|
|
* |
68
|
|
|
* @param int $majorVersion Major version |
69
|
|
|
* |
70
|
|
|
* @static |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public static function isLegacy($majorVersion = Kernel::MAJOR_VERSION) |
75
|
|
|
{ |
76
|
|
|
return $majorVersion < self::MINIMUM_MAJOR_VERSION; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|