Skip to content

Commit 150a17e

Browse files
author
Bernhard Schmitt
committed
properly use new pseudo enum interface
1 parent 0f03464 commit 150a17e

7 files changed

Lines changed: 79 additions & 50 deletions

Classes/Domain/Enum/Enum.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function getClassContent(): string
5353
*/
5454
5555
use Neos\Flow\Annotations as Flow;
56-
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumInterface;
56+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
5757
5858
/**
5959
* @Flow\Proxy(false)
6060
*/
61-
final class ' . $this->name->getName() . ' implements EnumInterface
61+
final class ' . $this->name->getName() . ' implements PseudoEnumInterface
6262
{
6363
' . $this->renderConstants() . '
6464
@@ -71,7 +71,9 @@ private function __construct(' . $this->type . ' $value)
7171
7272
public static function from' . ucfirst((string)$this->type) . '(' . $this->type . ' ' . $variable . '): self
7373
{
74-
if (!in_array(' . $variable . ', self::getValues())) {
74+
if (!in_array(' . $variable . ', array_map(function(self $case) {
75+
return $case->getValue();
76+
}, self::cases()))) {
7577
throw ' . $this->name->getExceptionName() . '::becauseItMustBeOneOfTheDefinedConstants(' . $variable . ');
7678
}
7779
@@ -83,12 +85,12 @@ public static function from' . ucfirst((string)$this->type) . '(' . $this->type
8385
' . $this->renderComparators() . '
8486
8587
/**
86-
* @return array|' . $this->type . '[]
88+
* @return array|self[]
8789
*/
88-
public static function getValues(): array
90+
public static function cases(): array
8991
{
9092
return [
91-
' . $this->renderValues() .'
93+
' . $this->renderCases() .'
9294
];
9395
}
9496
@@ -190,17 +192,17 @@ private function renderComparators(): string
190192
/**
191193
* @return string
192194
*/
193-
public function renderValues(): string
195+
public function renderCases(): string
194196
{
195-
$values = [];
197+
$cases = [];
196198

197199
if (is_array($this->values)) {
198200
foreach ($this->values as $name => $value) {
199-
$values[] = 'self::' . $this->getConstantName($name) . ',';
201+
$cases[] = 'new self(self::' . $this->getConstantName($name) . '),';
200202
}
201203
}
202204

203-
return trim(trim(implode("\n ", $values)), ',');
205+
return trim(trim(implode("\n ", $cases)), ',');
204206
}
205207

206208
/**

Documentation/01_PresentationObjectsAndComponents.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ final class HeadlineType implements PseudoEnumInterface
176176

177177
public static function fromString(string $string): self
178178
{
179-
if ($string !== self::TYPE_H1 && $string !== self::TYPE_H2 && $string !== self::TYPE_H3) {
179+
if (!in_array($string, array_map(function(self $case) {
180+
return $case->getValue();
181+
}, self::cases()))) {
180182
throw HeadlineTypeIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
181183
}
182184

@@ -198,6 +200,21 @@ final class HeadlineType implements PseudoEnumInterface
198200
return new self(self::TYPE_H3);
199201
}
200202

203+
public function getIsH1(): bool
204+
{
205+
return $this->value === self::TYPE_H1;
206+
}
207+
208+
public function getIsH2(): bool
209+
{
210+
return $this->value === self::TYPE_H2;
211+
}
212+
213+
public function getIsH3(): bool
214+
{
215+
return $this->value === self::TYPE_H3;
216+
}
217+
201218
/**
202219
* @return array|self[]
203220
*/

Tests/Unit/Domain/Enum/EnumTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public function testGetClassContent(): void
5252
*/
5353
5454
use Neos\Flow\Annotations as Flow;
55-
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumInterface;
55+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\PseudoEnumInterface;
5656
5757
/**
5858
* @Flow\Proxy(false)
5959
*/
60-
final class MyComponentType implements EnumInterface
60+
final class MyComponentType implements PseudoEnumInterface
6161
{
6262
const TYPE_PRIMARY = \'primary\';
6363
const TYPE_SECONDARY = \'secondary\';
@@ -71,7 +71,9 @@ private function __construct(string $value)
7171
7272
public static function fromString(string $string): self
7373
{
74-
if (!in_array($string, self::getValues())) {
74+
if (!in_array($string, array_map(function(self $case) {
75+
return $case->getValue();
76+
}, self::cases()))) {
7577
throw MyComponentTypeIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
7678
}
7779
@@ -99,13 +101,13 @@ public function getIsSecondary(): bool
99101
}
100102
101103
/**
102-
* @return array|string[]
104+
* @return array|self[]
103105
*/
104-
public static function getValues(): array
106+
public static function cases(): array
105107
{
106108
return [
107-
self::TYPE_PRIMARY,
108-
self::TYPE_SECONDARY
109+
new self(self::TYPE_PRIMARY),
110+
new self(self::TYPE_SECONDARY)
109111
];
110112
}
111113

Tests/Unit/Domain/Enum/__snapshots__/EnumGeneratorTest__generatesEnums with data set coLocatedHeadlineType__1.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Vendor\Site\Presentation\Component\Headline;
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
* @Flow\Proxy(false)
1313
*/
14-
final class HeadlineType implements EnumInterface
14+
final class HeadlineType implements PseudoEnumInterface
1515
{
1616
const TYPE_H1 = 'h1';
1717
const TYPE_H2 = 'h2';
@@ -26,7 +26,9 @@ final class HeadlineType implements EnumInterface
2626

2727
public static function fromString(string $string): self
2828
{
29-
if (!in_array($string, self::getValues())) {
29+
if (!in_array($string, array_map(function(self $case) {
30+
return $case->getValue();
31+
}, self::cases()))) {
3032
throw HeadlineTypeIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
3133
}
3234

@@ -64,14 +66,14 @@ final class HeadlineType implements EnumInterface
6466
}
6567

6668
/**
67-
* @return array|string[]
69+
* @return array|self[]
6870
*/
69-
public static function getValues(): array
71+
public static function cases(): array
7072
{
7173
return [
72-
self::TYPE_H1,
73-
self::TYPE_H2,
74-
self::TYPE_DIV
74+
new self(self::TYPE_H1),
75+
new self(self::TYPE_H2),
76+
new self(self::TYPE_DIV)
7577
];
7678
}
7779

Tests/Unit/Domain/Enum/__snapshots__/EnumGeneratorTest__generatesEnums with data set duration__1.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Vendor\Default\Presentation\Custom\Type\Crossing;
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
* @Flow\Proxy(false)
1313
*/
14-
final class Duration implements EnumInterface
14+
final class Duration implements PseudoEnumInterface
1515
{
1616
const VALUE_SHORT = 1.2;
1717
const VALUE_MEDIUM = 2.4;
@@ -26,7 +26,9 @@ final class Duration implements EnumInterface
2626

2727
public static function fromFloat(float $float): self
2828
{
29-
if (!in_array($float, self::getValues())) {
29+
if (!in_array($float, array_map(function(self $case) {
30+
return $case->getValue();
31+
}, self::cases()))) {
3032
throw DurationIsInvalid::becauseItMustBeOneOfTheDefinedConstants($float);
3133
}
3234

@@ -64,14 +66,14 @@ final class Duration implements EnumInterface
6466
}
6567

6668
/**
67-
* @return array|float[]
69+
* @return array|self[]
6870
*/
69-
public static function getValues(): array
71+
public static function cases(): array
7072
{
7173
return [
72-
self::VALUE_SHORT,
73-
self::VALUE_MEDIUM,
74-
self::VALUE_LONG
74+
new self(self::VALUE_SHORT),
75+
new self(self::VALUE_MEDIUM),
76+
new self(self::VALUE_LONG)
7577
];
7678
}
7779

Tests/Unit/Domain/Enum/__snapshots__/EnumGeneratorTest__generatesEnums with data set headlinetype__1.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Vendor\Site\Presentation\Component\Headline;
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
* @Flow\Proxy(false)
1313
*/
14-
final class HeadlineType implements EnumInterface
14+
final class HeadlineType implements PseudoEnumInterface
1515
{
1616
const TYPE_H1 = 'h1';
1717
const TYPE_H2 = 'h2';
@@ -26,7 +26,9 @@ final class HeadlineType implements EnumInterface
2626

2727
public static function fromString(string $string): self
2828
{
29-
if (!in_array($string, self::getValues())) {
29+
if (!in_array($string, array_map(function(self $case) {
30+
return $case->getValue();
31+
}, self::cases()))) {
3032
throw HeadlineTypeIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
3133
}
3234

@@ -64,14 +66,14 @@ final class HeadlineType implements EnumInterface
6466
}
6567

6668
/**
67-
* @return array|string[]
69+
* @return array|self[]
6870
*/
69-
public static function getValues(): array
71+
public static function cases(): array
7072
{
7173
return [
72-
self::TYPE_H1,
73-
self::TYPE_H2,
74-
self::TYPE_DIV
74+
new self(self::TYPE_H1),
75+
new self(self::TYPE_H2),
76+
new self(self::TYPE_DIV)
7577
];
7678
}
7779

Tests/Unit/Domain/Enum/__snapshots__/EnumGeneratorTest__generatesEnums with data set trafficlight__1.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Vendor\Default\Presentation\Component\Crossing;
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
* @Flow\Proxy(false)
1313
*/
14-
final class TrafficLight implements EnumInterface
14+
final class TrafficLight implements PseudoEnumInterface
1515
{
1616
const LIGHT_RED = 1;
1717
const LIGHT_YELLOW = 2;
@@ -26,7 +26,9 @@ final class TrafficLight implements EnumInterface
2626

2727
public static function fromInt(int $int): self
2828
{
29-
if (!in_array($int, self::getValues())) {
29+
if (!in_array($int, array_map(function(self $case) {
30+
return $case->getValue();
31+
}, self::cases()))) {
3032
throw TrafficLightIsInvalid::becauseItMustBeOneOfTheDefinedConstants($int);
3133
}
3234

@@ -64,14 +66,14 @@ final class TrafficLight implements EnumInterface
6466
}
6567

6668
/**
67-
* @return array|int[]
69+
* @return array|self[]
6870
*/
69-
public static function getValues(): array
71+
public static function cases(): array
7072
{
7173
return [
72-
self::LIGHT_RED,
73-
self::LIGHT_YELLOW,
74-
self::LIGHT_GREEN
74+
new self(self::LIGHT_RED),
75+
new self(self::LIGHT_YELLOW),
76+
new self(self::LIGHT_GREEN)
7577
];
7678
}
7779

0 commit comments

Comments
 (0)