Skip to content

Commit 87ccf3b

Browse files
author
Bernhard Schmitt
committed
Use IsEnum to validate enum names
1 parent 8eddbed commit 87ccf3b

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

Classes/Application/PseudoEnumProvider.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Neos\Flow\I18n\Translator;
1313
use Neos\Neos\Service\DataSource\AbstractDataSource;
1414
use Neos\Eel\ProtectedContextAwareInterface;
15+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\PropType\IsEnum;
1516
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumLabel;
1617
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
1718

@@ -40,6 +41,7 @@ public function getData(NodeInterface $node = null, array $arguments = []): arra
4041
}
4142
/** @var class-string<mixed> $enumName */
4243
$enumName = $arguments['enumName'];
44+
4345
$values = $this->getValues($enumName);
4446
$enumLabel = EnumLabel::fromEnumName($enumName);
4547
$options = [];
@@ -60,11 +62,13 @@ public function process(NodeType $nodeType, array &$configuration, array $option
6062
if (!array_key_exists('enumName', $options) || !is_string($options['enumName'])) {
6163
throw new \InvalidArgumentException('Option "enumName" must be provided.', 1625298032);
6264
}
63-
$values = $this->getValues($options['enumName']);
64-
$enumLabel = EnumLabel::fromEnumName($options['enumName']);
6565
if (!array_key_exists('propertyNames', $options) || !is_array($options['propertyNames'])) {
6666
throw new \InvalidArgumentException('Option "propertyNames" must be provided.', 1626540931);
6767
}
68+
/** @var class-string<mixed> $enumName */
69+
$enumName = $options['enumName'];
70+
$values = $this->getValues($enumName);
71+
$enumLabel = EnumLabel::fromEnumName($enumName);
6872
foreach ($options['propertyNames'] as $propertyName) {
6973
foreach ($values as $value) {
7074
$configuration['properties'][$propertyName]['ui']['inspector']['editorOptions']['values'][$value] = [
@@ -91,21 +95,13 @@ public function getValues(string $enumName): array
9195
*/
9296
public function getCases(string $enumName): array
9397
{
94-
$this->validateEnumName($enumName);
98+
if (!IsEnum::isSatisfiedByClassName($enumName)) {
99+
throw new \InvalidArgumentException('Given enum "' . $enumName . '" does not exist or does not implement the required ' . PseudoEnumInterface::class, 1625297031);
100+
}
95101

96102
return $enumName::cases();
97103
}
98104

99-
private function validateEnumName(string $enumName): void
100-
{
101-
if (!class_exists($enumName)) {
102-
throw new \InvalidArgumentException('Given enum "' . $enumName . '" does not exist.', 1625297031);
103-
}
104-
if (!is_subclass_of($enumName, PseudoEnumInterface::class)) {
105-
throw new \InvalidArgumentException('Given enum "' . $enumName . '" does not implement the required ' . PseudoEnumInterface::class, 1625297122);
106-
}
107-
}
108-
109105
public function allowsCallOfMethod($methodName): bool
110106
{
111107
return true;

0 commit comments

Comments
 (0)