diff --git a/src/Symfony/Component/Workflow/CHANGELOG.md b/src/Symfony/Component/Workflow/CHANGELOG.md index 2926da4e6428d..93bc0eaed9663 100644 --- a/src/Symfony/Component/Workflow/CHANGELOG.md +++ b/src/Symfony/Component/Workflow/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + + * Add support for Backed Enum in `MethodMarkingStore` + 7.1 --- diff --git a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php index a2844b7b8ede7..5806c68298b84 100644 --- a/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php +++ b/src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php @@ -64,6 +64,10 @@ public function getMarking(object $subject): Marking } if ($this->singleState) { + if ($marking instanceof \BackedEnum) { + $marking = $marking->value; + } + $marking = [(string) $marking => 1]; } elseif (!\is_array($marking)) { throw new LogicException(\sprintf('The marking stored in "%s::$%s" is not an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject), $this->property)); diff --git a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php index af0be682329be..54311e78621b2 100644 --- a/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php +++ b/src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore; use Symfony\Component\Workflow\Tests\Subject; +use Symfony\Component\Workflow\Tests\TestEnum; class MethodMarkingStoreTest extends TestCase { @@ -84,6 +85,24 @@ public function testGetMarkingWithValueObject() $this->assertSame('first_place', (string) $subject->getMarking()); } + public function testGetMarkingWithBackedEnum() + { + $subject = new Subject(TestEnum::Foo); + + $markingStore = new MethodMarkingStore(true); + + $marking = $markingStore->getMarking($subject); + + $this->assertCount(1, $marking->getPlaces()); + $this->assertSame(['foo' => 1], $marking->getPlaces()); + + $marking->mark('bar'); + $marking->unmark('foo'); + $markingStore->setMarking($subject, $marking); + + $this->assertSame(TestEnum::Bar, $subject->getMarking()); + } + public function testGetMarkingWithUninitializedProperty() { $subject = new SubjectWithType(); diff --git a/src/Symfony/Component/Workflow/Tests/Subject.php b/src/Symfony/Component/Workflow/Tests/Subject.php index d68d430035f5d..82ea499a45164 100644 --- a/src/Symfony/Component/Workflow/Tests/Subject.php +++ b/src/Symfony/Component/Workflow/Tests/Subject.php @@ -13,7 +13,7 @@ final class Subject { - private string|array|null $marking; + private string|array|\BackedEnum|null $marking; private array $context = []; public function __construct($marking = null) @@ -21,13 +21,16 @@ public function __construct($marking = null) $this->marking = $marking; } - public function getMarking(): string|array|null + public function getMarking(): string|array|\BackedEnum|null { return $this->marking; } public function setMarking($marking, array $context = []): void { + if (\is_string($marking) && $newMarking = TestEnum::tryFrom($marking)) { + $marking = $newMarking; + } $this->marking = $marking; $this->context = $context; } diff --git a/src/Symfony/Component/Workflow/Tests/TestEnum.php b/src/Symfony/Component/Workflow/Tests/TestEnum.php new file mode 100644 index 0000000000000..e476f8b63b7ae --- /dev/null +++ b/src/Symfony/Component/Workflow/Tests/TestEnum.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Tests; + +enum TestEnum: string +{ + case Foo = 'foo'; + case Bar = 'bar'; +}