1212use Neos \Flow \I18n \Translator ;
1313use Neos \Neos \Service \DataSource \AbstractDataSource ;
1414use Neos \Eel \ProtectedContextAwareInterface ;
15+ use PackageFactory \AtomicFusion \PresentationObjects \Domain \Enum \EnumLabel ;
1516use PackageFactory \AtomicFusion \PresentationObjects \Domain \Enum \PseudoEnumInterface ;
1617
17- class PseudoEnumProvider extends AbstractDataSource implements ProtectedContextAwareInterface, NodeTypePostprocessorInterface
18+ final class PseudoEnumProvider extends AbstractDataSource implements ProtectedContextAwareInterface, NodeTypePostprocessorInterface
1819{
1920 /**
2021 * @Flow\Inject
@@ -32,10 +33,11 @@ public function getData(NodeInterface $node = null, array $arguments = []): arra
3233 if (!isset ($ arguments ['enumName ' ])) {
3334 throw new \InvalidArgumentException ('Argument "enumName" must be provided. ' , 1625297174 );
3435 }
35- $ this ->validateEnumName ($ arguments ['enumName ' ]);
36+ $ values = $ this ->getValues ($ arguments ['enumName ' ]);
37+ $ enumLabel = EnumLabel::fromEnumName ($ arguments ['enumName ' ]);
3638 $ options = [];
37- foreach ($ this -> getCases ( $ arguments [ ' enumName ' ]) as $ value ) {
38- $ options [$ value ]['label ' ] = $ this ->getLabel ($ arguments [ ' enumName ' ], $ value );
39+ foreach ($ values as $ value ) {
40+ $ options [$ value ]['label ' ] = $ this ->getLabel ($ enumLabel , ( string ) $ value );
3941 }
4042
4143 return $ options ;
@@ -46,43 +48,49 @@ public function process(NodeType $nodeType, array &$configuration, array $option
4648 if (!isset ($ options ['enumName ' ])) {
4749 throw new \InvalidArgumentException ('Option "enumName" must be provided. ' , 1625298032 );
4850 }
49- $ this ->validateEnumName ($ options ['enumName ' ]);
50- $ cases = $ this -> getCases ($ options ['enumName ' ]);
51+ $ values = $ this ->getValues ($ options ['enumName ' ]);
52+ $ enumLabel = EnumLabel:: fromEnumName ($ options ['enumName ' ]);
5153 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 )
54+ foreach ($ values as $ value ) {
55+ $ configuration ['properties ' ][$ propertyName ]['ui ' ]['inspector ' ]['editorOptions ' ]['values ' ][$ value ] = [
56+ 'label ' => $ this ->getLabel ($ enumLabel , (string )$ value )
5557 ];
5658 }
5759 }
5860 }
5961
60- private function getLabel (string $ enumName , string $ value ): string
62+ private function getLabel (EnumLabel $ enumLabel , string $ value ): string
6163 {
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-
6764 return $ this ->translator ->translateById (
68- $ enumShort . ' . ' . $ value ,
65+ $ enumLabel -> getLabelIdPrefix () . $ value ,
6966 [],
7067 null ,
7168 null ,
72- \str_replace ( ' / ' , ' . ' , $ componentNamespace ),
73- \str_replace ( ' / ' , ' . ' , $ packageKey )
69+ $ enumLabel -> getSourceName ( ),
70+ $ enumLabel -> getPackageKey ( )
7471 ) ?: $ value ;
7572 }
7673
7774 /**
7875 * @param class-string<mixed> $enumName
7976 * @return array|string[]|int[]
8077 */
81- public function getCases (string $ enumName ): array
78+ public function getValues (string $ enumName ): array
8279 {
8380 return array_map (function (PseudoEnumInterface $ case ) {
8481 return $ case ->getValue ();
85- }, $ enumName ::cases ());
82+ }, $ this ->getCases ($ enumName ));
83+ }
84+
85+ /**
86+ * @param class-string<mixed> $enumName
87+ * @return array|PseudoEnumInterface[]
88+ */
89+ public function getCases (string $ enumName ): array
90+ {
91+ $ this ->validateEnumName ($ enumName );
92+
93+ return $ enumName ::cases ();
8694 }
8795
8896 private function validateEnumName (string $ enumName ): void
0 commit comments