These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 string[]|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 | $class = $details['class']; |
||
| 40 | if (\is_string($class) && \is_subclass_of($class, AbstractEnumType::class)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 41 | $this->registeredEnumTypes[$type] = $class; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | protected function hasRegisteredEnumTypes(): bool |
||
| 50 | { |
||
| 51 | return !empty($this->registeredEnumTypes) && \is_array($this->registeredEnumTypes); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | protected function onlyOneOccurrenceFound(): bool |
||
| 58 | { |
||
| 59 | return 1 === \count($this->occurrences); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | protected function moreThanOneOccurrenceFound(): bool |
||
| 66 | { |
||
| 67 | return 1 < \count($this->occurrences); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return NoRegisteredEnumTypesException |
||
| 72 | */ |
||
| 73 | protected function createNoRegisteredEnumTypesException(): NoRegisteredEnumTypesException |
||
| 74 | { |
||
| 75 | return new NoRegisteredEnumTypesException('There are no registered ENUM types.'); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $enumType |
||
| 80 | * |
||
| 81 | * @throws EnumTypeIsNotRegisteredException |
||
| 82 | */ |
||
| 83 | protected function throwExceptionIfEnumTypeIsNotRegistered(string $enumType): void |
||
| 84 | { |
||
| 85 | if (!isset($this->registeredEnumTypes[$enumType])) { |
||
| 86 | throw new EnumTypeIsNotRegisteredException(\sprintf('ENUM type "%s" is not registered.', $enumType)); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 |