Skip to content

Commit f408e89

Browse files
author
Bernhard Schmitt
committed
Introduce enum label
1 parent 71bebbe commit f408e89

2 files changed

Lines changed: 87 additions & 20 deletions

File tree

Classes/Application/PseudoEnumProvider.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
use Neos\Flow\I18n\Translator;
1313
use Neos\Neos\Service\DataSource\AbstractDataSource;
1414
use Neos\Eel\ProtectedContextAwareInterface;
15+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumLabel;
1516
use 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

Classes/Domain/Enum/EnumLabel.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
namespace PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum;
3+
4+
/*
5+
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package.
6+
*/
7+
8+
use Neos\Flow\Annotations as Flow;
9+
10+
/**
11+
* @Flow\Proxy(false)
12+
*/
13+
final class EnumLabel
14+
{
15+
private string $labelIdPrefix;
16+
17+
private string $sourceName;
18+
19+
private string $packageKey;
20+
21+
private function __construct(
22+
string $labelIdPrefix,
23+
string $sourceName,
24+
string $packageKey
25+
) {
26+
$this->labelIdPrefix = $labelIdPrefix;
27+
$this->sourceName = $sourceName;
28+
$this->packageKey = $packageKey;
29+
}
30+
31+
public static function fromEnumName(string $enumName): self
32+
{
33+
list($packageKey, $componentName) = explode('/Presentation/', $enumName);
34+
$pivot = \mb_strrpos($componentName, '/');
35+
$componentNamespace = \mb_substr($packageKey, 0 , $pivot);
36+
$enumShort = lcfirst(\mb_substr($packageKey, $pivot+1));
37+
38+
return new self(
39+
$enumShort . '.',
40+
\str_replace('/', '.', $componentNamespace),
41+
\str_replace('/', '.', $packageKey)
42+
);
43+
}
44+
45+
public function getLabelIdPrefix(): string
46+
{
47+
return $this->labelIdPrefix;
48+
}
49+
50+
public function getSourceName(): string
51+
{
52+
return $this->sourceName;
53+
}
54+
55+
public function getPackageKey(): string
56+
{
57+
return $this->packageKey;
58+
}
59+
}

0 commit comments

Comments
 (0)