Skip to content

Commit 4200cac

Browse files
author
Bernhard Schmitt
committed
Adjust kickstarter documentation for enums
1 parent dba6af9 commit 4200cac

2 files changed

Lines changed: 30 additions & 95 deletions

File tree

Documentation/05_Kickstarter.md

Lines changed: 28 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ For more information on this pattern, have a look at this excellent article: htt
2828
./flow component:kickstartenum \
2929
Vendor.Site:Headline \
3030
HeadlineLook string \
31-
--values=REGULAR,HERO
31+
--values=regular,hero
3232
```
3333

3434
> **Hint:** Components are namespaced, defaulting to "Component". The component name "Vendor.Site:Headline" thus will be evaluated as "Vendor.Site:Component.Headline".
@@ -40,10 +40,10 @@ For more information on this pattern, have a look at this excellent article: htt
4040

4141
This is the central pseudo-enum class. It consists of:
4242

43-
* A set of constants that represent the enum values
44-
* Static factory methods that are named like the enum values
43+
* A set of constants that represent the enum cases
44+
* Static factory methods that are named like the enum cases
4545
* `getIs*` methods to identify a value both in PHP and Neos.Eel
46-
* A static `getValues` method to retrieve a list of all possible enum values
46+
* A static `cases` method to retrieve a list of all possible enum cases
4747
* A `getValue` method that will return the value of the enum instance as your chosen type
4848
* A `__toString` method for string casting
4949

@@ -62,8 +62,13 @@ use Neos\Flow\Annotations as Flow;
6262
*/
6363
final class HeadlineLook
6464
{
65-
const LOOK_REGULAR = 'REGULAR';
66-
const LOOK_HERO = 'HERO';
65+
const LOOK_REGULAR = 'regular';
66+
const LOOK_HERO = 'hero';
67+
68+
/**
69+
* @var array<string,self>|self[]
70+
*/
71+
private static array $instances = [];
6772

6873
private string $value;
6974

@@ -72,23 +77,27 @@ final class HeadlineLook
7277
$this->value = $value;
7378
}
7479

75-
public static function fromString(string $string): self
80+
public static function from(string $string): self
7681
{
77-
if (!in_array($string, self::getValues())) {
78-
throw HeadlineLookIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
82+
if (!isset(self::$instances[$string])) {
83+
if ($string !== self::LOOK_REGULAR
84+
&& $string !== self::LOOK_HERO) {
85+
throw HeadlineLookIsInvalid::becauseItMustBeOneOfTheDefinedConstants($string);
86+
}
87+
self::$instances[$string] = new self($string);
7988
}
8089

81-
return new self($string);
90+
return self::$instances[$string];
8291
}
8392

8493
public static function regular(): self
8594
{
86-
return new self(self::LOOK_REGULAR);
95+
return self::from(self::LOOK_REGULAR);
8796
}
8897

8998
public static function hero(): self
9099
{
91-
return new self(self::LOOK_HERO);
100+
return self::from(self::LOOK_HERO);
92101
}
93102

94103
public function getIsRegular(): bool
@@ -102,13 +111,13 @@ final class HeadlineLook
102111
}
103112

104113
/**
105-
* @return array|string[]
114+
* @return array<int,self>|self[]
106115
*/
107-
public static function getValues(): array
116+
public static function cases(): array
108117
{
109118
return [
110-
self::LOOK_REGULAR,
111-
self::LOOK_HERO
119+
self::from(self::LOOK_REGULAR),
120+
self::from(self::LOOK_HERO)
112121
];
113122
}
114123

@@ -126,7 +135,7 @@ final class HeadlineLook
126135

127136
#### HeadlineLookIsInvalid.php
128137

129-
The exception in this file will be thrown, when the pseudo-enum is initialized with an invalid value (which could happen, when `::fromString` is called).
138+
The exception in this file will be thrown, when the pseudo-enum is initialized with an invalid value (which could happen, when `::from` is called).
130139

131140
```php
132141
<?php
@@ -150,80 +159,6 @@ final class HeadlineLookIsInvalid extends \DomainException
150159
}
151160
```
152161

153-
#### HeadlineLookProvider.php
154-
155-
This file contains a data provider to use the enum values for SelectBoxes in the Neos backend UI.
156-
157-
```php
158-
<?php
159-
namespace Vendor\Site\Application;
160-
161-
/*
162-
* This file is part of the Vendor.Site package.
163-
*/
164-
165-
use Neos\ContentRepository\Domain\Model\NodeInterface;
166-
use Neos\Flow\Annotations as Flow;
167-
use Neos\Flow\I18n\Translator;
168-
use Neos\Neos\Service\DataSource\AbstractDataSource;
169-
use Neos\Eel\ProtectedContextAwareInterface;
170-
use Vendor\Site\Presentation\Component\Headline\HeadlineLook;
171-
172-
class HeadlineLookProvider extends AbstractDataSource implements ProtectedContextAwareInterface
173-
{
174-
/**
175-
* @Flow\Inject
176-
* @var Translator
177-
*/
178-
protected $translator;
179-
180-
/**
181-
* @var string
182-
*/
183-
protected static $identifier = 'vendor-site-headline-looks';
184-
185-
public function getData(NodeInterface $node = null, array $arguments = []): array
186-
{
187-
$headlineLooks = [];
188-
foreach (HeadlineLook::getValues() as $value) {
189-
$headlineLooks[$value]['label'] = $this->translator->translateById('headlineLook.' . $value, [], null, null, 'Headline', 'Vendor.Site') ?: $value;
190-
}
191-
192-
return $headlineLooks;
193-
}
194-
195-
/**
196-
* @return array|string[]
197-
*/
198-
public function getValues(): array
199-
{
200-
return HeadlineLook::getValues();
201-
}
202-
203-
public function allowsCallOfMethod($methodName): bool
204-
{
205-
return true;
206-
}
207-
}
208-
```
209-
210-
This data source can be used in your `NodeTypes.*.yaml` configuration like this:
211-
212-
```yaml
213-
properties:
214-
headlineLook:
215-
type: string
216-
ui:
217-
label: 'Headline look'
218-
reloadIfChanged: true
219-
inspector:
220-
group: 'headline'
221-
editor: Neos.Neos/Inspector/Editors/SelectBoxEditor
222-
editorOptions:
223-
placeholder: Choose a headline look...
224-
dataSourceIdentifier: vendor-site-headline-looks
225-
```
226-
227162
## `component:kickstart` command
228163

229164
This command creates all patterns needed for a component. It takes the name of the component and a list of property descriptors which consist of a property name and a type name separated by a colon.
@@ -265,8 +200,8 @@ prototype(Vendor.Site:Component.Headline) < prototype(PackageFactory.AtomicFusio
265200
title = 'Headline'
266201
267202
props {
268-
type = ''
269-
look = ''
203+
type = 'h1'
204+
look = 'regular'
270205
content = 'Text'
271206
}
272207
}

Tests/Unit/Fixtures/Site/Presentation/Component/Headline/HeadlineLook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ final class HeadlineLook implements PseudoEnumInterface
1616
{
1717
const LOOK_LARGE = 'large';
1818

19-
private string $value;
20-
2119
/**
2220
* @var array<string,self>|self[]
2321
*/
2422
private static array $instances = [];
2523

24+
private string $value;
25+
2626
private function __construct(string $value)
2727
{
2828
$this->value = $value;

0 commit comments

Comments
 (0)