Skip to content

Commit 3c0ee05

Browse files
authored
Merge pull request #17 from PackageFactory/enum-provider
Enum provider
2 parents 12ff81f + ad8fcd8 commit 3c0ee05

41 files changed

Lines changed: 1014 additions & 750 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\Component\PropType\IsEnum;
16+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumLabel;
17+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
18+
19+
final class PseudoEnumProvider extends AbstractDataSource implements ProtectedContextAwareInterface, NodeTypePostprocessorInterface
20+
{
21+
/**
22+
* @Flow\Inject
23+
* @var Translator
24+
*/
25+
protected $translator;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected static $identifier = 'packagefactory-atomicfusion-presentationobjects-enumcases';
31+
32+
/**
33+
* @param NodeInterface|null $node
34+
* @param array<string|int,string> $arguments
35+
* @return array<string|int,array<string,string>>
36+
*/
37+
public function getData(NodeInterface $node = null, array $arguments = []): array
38+
{
39+
if (!array_key_exists('enumName', $arguments) || !is_string($arguments['enumName'])) {
40+
throw new \InvalidArgumentException('Argument "enumName" must be provided.', 1625297174);
41+
}
42+
/** @var class-string<mixed> $enumName */
43+
$enumName = $arguments['enumName'];
44+
45+
$values = $this->getValues($enumName);
46+
$enumLabel = EnumLabel::fromEnumName($enumName);
47+
$options = [];
48+
foreach ($values as $value) {
49+
$options[$value]['label'] = $enumLabel->translate((string)$value, $this->translator);
50+
}
51+
52+
return $options;
53+
}
54+
55+
/**
56+
* @param NodeType $nodeType
57+
* @param array<mixed> $configuration
58+
* @param array<mixed> $options
59+
*/
60+
public function process(NodeType $nodeType, array &$configuration, array $options)
61+
{
62+
if (!array_key_exists('enumName', $options) || !is_string($options['enumName'])) {
63+
throw new \InvalidArgumentException('Option "enumName" must be provided.', 1625298032);
64+
}
65+
if (!array_key_exists('propertyNames', $options) || !is_array($options['propertyNames'])) {
66+
throw new \InvalidArgumentException('Option "propertyNames" must be provided.', 1626540931);
67+
}
68+
/** @var class-string<mixed> $enumName */
69+
$enumName = $options['enumName'];
70+
$values = $this->getValues($enumName);
71+
$enumLabel = EnumLabel::fromEnumName($enumName);
72+
foreach ($options['propertyNames'] as $propertyName) {
73+
foreach ($values as $value) {
74+
$configuration['properties'][$propertyName]['ui']['inspector']['editorOptions']['values'][$value] = [
75+
'label' => $enumLabel->translate((string)$value, $this->translator)
76+
];
77+
}
78+
}
79+
}
80+
81+
/**
82+
* @param class-string<mixed> $enumName
83+
* @return array|string[]|int[]
84+
*/
85+
public function getValues(string $enumName): array
86+
{
87+
return array_map(function (PseudoEnumInterface $case) {
88+
return $case->getValue();
89+
}, $this->getCases($enumName));
90+
}
91+
92+
/**
93+
* @param class-string<mixed> $enumName
94+
* @return array|PseudoEnumInterface[]
95+
*/
96+
public function getCases(string $enumName): array
97+
{
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+
}
101+
102+
return $enumName::cases();
103+
}
104+
105+
public function allowsCallOfMethod($methodName): bool
106+
{
107+
return true;
108+
}
109+
}

Classes/Domain/Component/PropType/EnumPropType.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Neos\Flow\Annotations as Flow;
9+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
910

1011
/**
1112
* @Flow\Proxy(false)
@@ -60,15 +61,17 @@ public function getStyleGuideValue(int $nestingLevel = 0): string
6061
return '= \'\'';
6162
}
6263

63-
$values = $this->className::getValues();
64-
$value = reset($values);
65-
switch ((string) $type) {
66-
case 'string':
67-
return '= \'' . $value . '\'';
68-
case 'int':
69-
case 'float':
70-
return '= ' . $value;
71-
default:
64+
$cases = $this->className::cases();
65+
if (!empty($cases)) {
66+
/** @var PseudoEnumInterface $defaultCase */
67+
$defaultCase = reset($cases);
68+
switch ((string) $type) {
69+
case 'string':
70+
return '= \'' . $defaultCase->getValue() . '\'';
71+
case 'int':
72+
return '= ' . $defaultCase->getValue();
73+
default:
74+
}
7275
}
7376

7477
return '= \'\'';

Classes/Domain/Component/PropType/IsEnum.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
use Neos\Flow\Annotations as Flow;
9-
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumInterface;
9+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
1010

1111
/**
1212
* The specification for enum classes
@@ -17,6 +17,6 @@ final class IsEnum
1717
public static function isSatisfiedByClassName(string $className): bool
1818
{
1919
return class_exists($className)
20-
&& is_subclass_of($className, EnumInterface::class);
20+
&& is_subclass_of($className, PseudoEnumInterface::class);
2121
}
2222
}

0 commit comments

Comments
 (0)