Skip to content

Commit 8631283

Browse files
Merge pull request #758 from code16/allow-enum-as-state
Feature: Allow enum as state in EntityState
2 parents 0a0a16d + 9253f01 commit 8631283

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/Console/GeneratorCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ protected function entityPrompt(): void
288288
'Dashboard' => $this->generateDashboardEntity(),
289289
};
290290

291-
if (confirm(label: 'Do you want to automatically declare this Entity in the Sharp configuration?')) {
291+
if (confirm(
292+
label: 'Do you want to automatically declare this Entity in the Sharp configuration?',
293+
default: false,
294+
hint: 'If you enabled auto discovery in your Sharp Service Provider, this isn’t necessary.')
295+
) {
292296
$providerFound = false;
293297

294298
while (! $providerFound) {
@@ -313,7 +317,7 @@ protected function entityPrompt(): void
313317
$providerFound = true;
314318
}
315319

316-
$reflector = new \ReflectionClass($provider);
320+
$reflector = new ReflectionClass($provider);
317321
$this->declareEntityInSharpConfiguration($reflector->getFileName(), $entityPath, $entityKey);
318322

319323
$this->components->info(

src/EntityList/Commands/EntityState.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Code16\Sharp\EntityList\Commands;
44

5+
use BackedEnum;
56
use Code16\Sharp\Exceptions\EntityList\SharpInvalidEntityStateException;
67
use Code16\Sharp\Exceptions\SharpInvalidConfigException;
8+
use InvalidArgumentException;
79

810
/**
911
* Base class for applicative Entity States.
@@ -19,8 +21,12 @@ public function states(): array
1921
return $this->states;
2022
}
2123

22-
protected function addState(string $key, string $label, ?string $color = null): self
24+
protected function addState($key, string $label, ?string $color = null): self
2325
{
26+
if ($key instanceof BackedEnum && ! is_string($key = $key->value)) {
27+
throw new InvalidArgumentException('When using enum as a key, it must be string backed.');
28+
}
29+
2430
$this->states[$key] = [$label, $color];
2531

2632
return $this;

0 commit comments

Comments
 (0)