1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "elao/enum" package. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Elao |
7
|
|
|
* |
8
|
|
|
* @author Elao <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Elao\Enum\Bridge\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use Elao\Enum\EnumInterface; |
14
|
|
|
use Elao\Enum\Exception\InvalidArgumentException; |
15
|
|
|
use Elao\Enum\ReadableEnumInterface; |
16
|
|
|
use Twig\Extension\AbstractExtension; |
17
|
|
|
use Twig\TwigFunction; |
18
|
|
|
|
19
|
|
|
class EnumExtension extends AbstractExtension |
20
|
|
|
{ |
21
|
|
|
public function getFunctions() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
new TwigFunction('enum_get', [$this, 'get']), |
25
|
|
|
new TwigFunction('enum_values', [$this, 'values']), |
26
|
|
|
new TwigFunction('enum_accepts', [$this, 'accepts']), |
27
|
|
|
new TwigFunction('enum_instances', [$this, 'instances']), |
28
|
|
|
new TwigFunction('enum_readables', [$this, 'readables']), |
29
|
|
|
new TwigFunction('enum_readable_for', [$this, 'readableFor']), |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function get(string $className, $value): EnumInterface |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (!is_a($className, EnumInterface::class, true)) { |
36
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return \call_user_func([$className, 'get'], $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function values(string $className): array |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (!is_a($className, EnumInterface::class, true)) { |
45
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return \call_user_func([$className, 'values']); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function accepts(string $className, $value): bool |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if (!is_a($className, EnumInterface::class, true)) { |
54
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return \call_user_func([$className, 'accepts'], $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function instances(string $className): array |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (!is_a($className, EnumInterface::class, true)) { |
63
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return \call_user_func([$className, 'instances']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function readables(string $className): array |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if (!is_a($className, ReadableEnumInterface::class, true)) { |
72
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return \call_user_func([$className, 'readables']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function readableFor(string $className, $value): string |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (!is_a($className, ReadableEnumInterface::class, true)) { |
81
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return \call_user_func([$className, 'readableFor'], $value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.