1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshDoctrineEnumBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[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
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\DoctrineEnumBundle\Twig\Extension; |
14
|
|
|
|
15
|
|
|
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType; |
16
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException; |
17
|
|
|
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException; |
18
|
|
|
use Twig\Extension\AbstractExtension; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AbstractEnumTwigExtension. |
22
|
|
|
* |
23
|
|
|
* @author Artem Henvald <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractEnumTwigExtension extends AbstractExtension |
26
|
|
|
{ |
27
|
|
|
/** @var AbstractEnumType[] */ |
28
|
|
|
protected $registeredEnumTypes = []; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
protected $occurrences = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $registeredTypes |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $registeredTypes) |
37
|
|
|
{ |
38
|
|
|
foreach ($registeredTypes as $type => $details) { |
39
|
|
|
if (\is_subclass_of($details['class'], AbstractEnumType::class)) { |
|
|
|
|
40
|
|
|
$this->registeredEnumTypes[$type] = $details['class']; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
protected function hasRegisteredEnumTypes(): bool |
49
|
|
|
{ |
50
|
|
|
return !empty($this->registeredEnumTypes) && \is_array($this->registeredEnumTypes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
protected function onlyOneOccurrenceFound(): bool |
57
|
|
|
{ |
58
|
|
|
return 1 === \count($this->occurrences); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function moreThanOneOccurrenceFound(): bool |
65
|
|
|
{ |
66
|
|
|
return 1 < \count($this->occurrences); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return NoRegisteredEnumTypesException |
71
|
|
|
*/ |
72
|
|
|
protected function createNoRegisteredEnumTypesException(): NoRegisteredEnumTypesException |
73
|
|
|
{ |
74
|
|
|
return new NoRegisteredEnumTypesException('There are no registered ENUM types.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $enumType |
79
|
|
|
* |
80
|
|
|
* @throws EnumTypeIsNotRegisteredException |
81
|
|
|
*/ |
82
|
|
|
protected function throwExceptionIfEnumTypeIsNotRegistered(string $enumType): void |
83
|
|
|
{ |
84
|
|
|
if (!isset($this->registeredEnumTypes[$enumType])) { |
85
|
|
|
throw new EnumTypeIsNotRegisteredException(\sprintf('ENUM type "%s" is not registered.', $enumType)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|