Passed
Pull Request — master (#32)
by Florian
03:46
created

BaseEnum::camelCaseToTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the feedback 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 Symfony\Component\Translation\TranslatorInterface;
16
17
abstract class BaseEnum
18
{
19
    /**
20
     * returns an array fit to be used by the ChoiceType.
21
     *
22
     * @return array
23
     */
24
    public static function getBuilderArguments()
25
    {
26
        $elem = new static();
27
28
        return $elem->getChoicesForBuilderInternal();
29
    }
30
31
    /**
32
     * returns a translation string for the passed enum value.
33
     *
34
     * @param $enumValue
35
     *
36
     * @return string
37
     */
38
    public static function getTranslation($enumValue, TranslatorInterface $translator)
39
    {
40
        $elem = new static();
41
42
        return $elem->getTranslationInternal($enumValue, $translator);
43
    }
44
45
    /**
46
     * returns a translation string for the passed enum value.
47
     *
48
     * @param $enumValue
49
     *
50
     * @return string
51
     */
52
    public static function getText($enumValue)
53
    {
54
        $elem = new static();
55
56
        return $elem->getTextInternal($enumValue);
57
    }
58
59
    /**
60
     * makes from camelCase => camel_case.
61
     *
62
     * @param $camelCase
63
     *
64
     * @return string
65
     */
66
    private static function camelCaseToTranslation($camelCase)
67
    {
68
        return mb_strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $camelCase));
69
    }
70
71
    /**
72
     * generates an array to be used in form fields.
73
     *
74
     * @return array
75
     */
76
    private function getChoicesForBuilderInternal()
77
    {
78
        try {
79
            $res = [];
80
            $reflection = new ReflectionClass(static::class);
81
            $choices = $reflection->getConstants();
82
83
            foreach ($choices as $name => $value) {
84
                $res[mb_strtolower($name)] = $value;
85
            }
86
            $transDomain = 'enum_' . $this->camelCaseToTranslation($reflection->getShortName());
87
88
            return ['translation_domain' => $transDomain, 'label' => 'enum.name', 'choices' => $res, 'choice_translation_domain' => $transDomain];
89
        } catch (\ReflectionException $e) {
90
            //this never fails because the class clearly exists
91
        }
92
93
        return [];
94
    }
95
96
    /**
97
     * returns a translation string for the passed enum value.
98
     *
99
     * @param $enumValue
100
     *
101
     * @return bool|string
102
     */
103
    private function getTranslationInternal($enumValue, TranslatorInterface $translator)
104
    {
105
        try {
106
            $reflection = new ReflectionClass(static::class);
107
108
            return $translator->trans($this->getTextInternal($enumValue, $reflection), [], 'enum_' . $this->camelCaseToTranslation($reflection->getShortName()));
109
        } catch (\ReflectionException $e) {
110
            //this never fails because the class clearly exists
111
        }
112
113
        return '';
114
    }
115
116
    /**
117
     * returns a translation string for the passed enum value.
118
     *
119
     * @param $enumValue
120
     * @param ReflectionClass $reflection
121
     *
122
     * @return bool|string
123
     */
124
    private function getTextInternal($enumValue, $reflection = null)
125
    {
126
        try {
127
            if (null === $reflection) {
128
                $reflection = new ReflectionClass(static::class);
129
            }
130
131
            $choices = $reflection->getConstants();
132
133
            foreach ($choices as $name => $value) {
134
                if ($value === $enumValue) {
135
                    return mb_strtolower($name);
136
                }
137
            }
138
        } catch (\ReflectionException $e) {
139
            //this never fails because the class clearly exists
140
        }
141
142
        return '';
143
    }
144
}
145