1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TheAlternativeZurich/events project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Enum\Base; |
13
|
|
|
|
14
|
|
|
use ReflectionClass; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
abstract class BaseEnum |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* returns an array fit to be used by the ChoiceType. |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public static function getChoicesForBuilder() |
26
|
|
|
{ |
27
|
|
|
$elem = new static(); |
28
|
|
|
|
29
|
|
|
return $elem->getChoicesForBuilderInternal(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* returns a translation string for the passed enum value. |
34
|
|
|
* |
35
|
|
|
* @param $enumValue |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function getTranslationForValue($enumValue, TranslatorInterface $translator) |
40
|
|
|
{ |
41
|
|
|
$elem = new static(); |
42
|
|
|
|
43
|
|
|
return $elem->getTranslationForValueInternal($enumValue, $translator); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* makes from camelCase => camel_case. |
48
|
|
|
* |
49
|
|
|
* @param $camelCase |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
private static function camelCaseToTranslation($camelCase) |
54
|
|
|
{ |
55
|
|
|
return mb_strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $camelCase)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* generates an array to be used in form fields. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
private function getChoicesForBuilderInternal() |
64
|
|
|
{ |
65
|
|
|
$res = []; |
66
|
|
|
try { |
67
|
|
|
$reflection = new ReflectionClass(static::class); |
68
|
|
|
$choices = $reflection->getConstants(); |
69
|
|
|
|
70
|
|
|
foreach ($choices as $name => $value) { |
71
|
|
|
$res[mb_strtolower($name)] = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return ['choices' => $res, 'choice_translation_domain' => 'enum_'.$this->camelCaseToTranslation($reflection->getShortName())]; |
75
|
|
|
} catch (ReflectionException $e) { |
76
|
|
|
//this never happens due to ReflectionClass is passed the class of the $this object (always valid) |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $res; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* returns a translation string for the passed enum value. |
84
|
|
|
* |
85
|
|
|
* @param $enumValue |
86
|
|
|
* |
87
|
|
|
* @return bool|string |
88
|
|
|
*/ |
89
|
|
|
private function getTranslationForValueInternal($enumValue, TranslatorInterface $translator) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$reflection = new ReflectionClass(static::class); |
93
|
|
|
$choices = $reflection->getConstants(); |
94
|
|
|
|
95
|
|
|
foreach ($choices as $name => $value) { |
96
|
|
|
if ($value === $enumValue) { |
97
|
|
|
return $translator->trans(mb_strtolower($name), [], 'enum_'.$this->camelCaseToTranslation($reflection->getShortName())); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} catch (ReflectionException $e) { |
101
|
|
|
//this never happens due to ReflectionClass is passed the class of the $this object (always valid) |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return ''; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|