|
| 1 | +<?php declare(strict_types=1); |
| 2 | +namespace PackageFactory\AtomicFusion\PresentationObjects\Application; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the PackageFactory.AtomicFusion.PresentationObjects package. |
| 6 | + */ |
| 7 | + |
| 8 | +use Neos\ContentRepository\Domain\Model\NodeInterface; |
| 9 | +use Neos\ContentRepository\Domain\Model\NodeType; |
| 10 | +use Neos\ContentRepository\NodeTypePostprocessor\NodeTypePostprocessorInterface; |
| 11 | +use Neos\Flow\Annotations as Flow; |
| 12 | +use Neos\Flow\I18n\Translator; |
| 13 | +use Neos\Neos\Service\DataSource\AbstractDataSource; |
| 14 | +use Neos\Eel\ProtectedContextAwareInterface; |
| 15 | +use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface; |
| 16 | + |
| 17 | +class PseudoEnumProvider extends AbstractDataSource implements ProtectedContextAwareInterface, NodeTypePostprocessorInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @Flow\Inject |
| 21 | + * @var Translator |
| 22 | + */ |
| 23 | + protected $translator; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected static $identifier = 'packagefactory-atomicfusion-presentationobjects-enumcases'; |
| 29 | + |
| 30 | + public function getData(NodeInterface $node = null, array $arguments = []): array |
| 31 | + { |
| 32 | + if (!isset($arguments['enumName'])) { |
| 33 | + throw new \InvalidArgumentException('Argument "enumName" must be provided.', 1625297174); |
| 34 | + } |
| 35 | + $this->validateEnumName($arguments['enumName']); |
| 36 | + $options = []; |
| 37 | + foreach ($this->getCases($arguments['enumName']) as $value) { |
| 38 | + $options[$value]['label'] = $this->getLabel($arguments['enumName'], $value); |
| 39 | + } |
| 40 | + |
| 41 | + return $options; |
| 42 | + } |
| 43 | + |
| 44 | + public function process(NodeType $nodeType, array &$configuration, array $options) |
| 45 | + { |
| 46 | + if (!isset($options['enumName'])) { |
| 47 | + throw new \InvalidArgumentException('Option "enumName" must be provided.', 1625298032); |
| 48 | + } |
| 49 | + $this->validateEnumName($options['enumName']); |
| 50 | + $cases = $this->getCases($options['enumName']); |
| 51 | + foreach ($options['propertyNames'] as $propertyName) { |
| 52 | + foreach ($cases as $case) { |
| 53 | + $configuration['properties'][$propertyName]['ui']['inspector']['editorOptions']['values'][$case] = [ |
| 54 | + 'label' => $this->getLabel($options['enumName'], (string)$case) |
| 55 | + ]; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private function getLabel(string $enumName, string $value): string |
| 61 | + { |
| 62 | + list($packageKey, $componentName) = explode('/Presentation/', $enumName); |
| 63 | + $pivot = \mb_strrpos($componentName, '/'); |
| 64 | + $componentNamespace = \mb_substr($packageKey, 0 , $pivot); |
| 65 | + $enumShort = lcfirst(\mb_substr($packageKey, $pivot+1)); |
| 66 | + |
| 67 | + return $this->translator->translateById( |
| 68 | + $enumShort . '.' . $value, |
| 69 | + [], |
| 70 | + null, |
| 71 | + null, |
| 72 | + \str_replace('/', '.', $componentNamespace), |
| 73 | + \str_replace('/', '.', $packageKey) |
| 74 | + ) ?: $value; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param class-string<mixed> $enumName |
| 79 | + * @return array|string[]|int[] |
| 80 | + */ |
| 81 | + public function getCases(string $enumName): array |
| 82 | + { |
| 83 | + return array_map(function (PseudoEnumInterface $case) { |
| 84 | + return $case->getValue(); |
| 85 | + }, $enumName::cases()); |
| 86 | + } |
| 87 | + |
| 88 | + private function validateEnumName(string $enumName): void |
| 89 | + { |
| 90 | + if (!class_exists($enumName)) { |
| 91 | + throw new \InvalidArgumentException('Given enum "' . $enumName . '" does not exist.', 1625297031); |
| 92 | + } |
| 93 | + if (!in_array(PseudoEnumInterface::class, class_implements($enumName))) { |
| 94 | + throw new \InvalidArgumentException('Given enum "' . $enumName . '" does not implement the required ' . PseudoEnumInterface::class, 1625297122); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public function allowsCallOfMethod($methodName): bool |
| 99 | + { |
| 100 | + return true; |
| 101 | + } |
| 102 | +} |
0 commit comments