Skip to content

Commit bc726f7

Browse files
bug #34294 [Workflow] Fix error when we use ValueObject for the marking property (FabienSalles)
This PR was merged into the 4.3 branch. Discussion ---------- [Workflow] Fix error when we use ValueObject for the marking property | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #28203 #22031 | License | MIT Fix Illegal offset type in `MethodMarkingStore` class when we use Value Object for the marking property. Now, we can avoid to use only a string an we can have a Subject class with a Value Object like this : ```php final class State { public const DRAFT = 'draft'; public const REVIEWED = 'reviewed'; public const REJECTED = 'rejected'; public const PUBLISHED = 'published'; /** @var string */ private $state; public function __construct(string $state) { // some validation $this->state = $state; } public function __toString() { return $this->state; } public static function Draft() { return new self(self::DRAFT); } ... } final class Subject { private $marking; public function __construct(State $marking = null) { $this->marking = $marking; } public function getMarking() { return $this->marking; } public function setMarking($marking) { $this->marking = $marking instanceof State ? $marking : new State($marking); } ``` Commits ------- 6570d5c Fix error when we use VO for the marking property
2 parents 618aec6 + 6570d5c commit bc726f7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Symfony/Component/Workflow/MarkingStore/MethodMarkingStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getMarking($subject)
6161
}
6262

6363
if ($this->singleState) {
64-
$marking = [$marking => 1];
64+
$marking = [(string) $marking => 1];
6565
}
6666

6767
return new Marking($marking);

src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,35 @@ public function testGetSetMarkingWithSingleState()
5252

5353
$this->assertEquals($marking, $marking2);
5454
}
55+
56+
public function testGetMarkingWithValueObject()
57+
{
58+
$subject = new Subject($this->createValueObject('first_place'));
59+
60+
$markingStore = new MethodMarkingStore(true);
61+
62+
$marking = $markingStore->getMarking($subject);
63+
64+
$this->assertInstanceOf(Marking::class, $marking);
65+
$this->assertCount(1, $marking->getPlaces());
66+
$this->assertSame('first_place', (string) $subject->getMarking());
67+
}
68+
69+
private function createValueObject(string $markingValue)
70+
{
71+
return new class($markingValue) {
72+
/** @var string */
73+
private $markingValue;
74+
75+
public function __construct(string $markingValue)
76+
{
77+
$this->markingValue = $markingValue;
78+
}
79+
80+
public function __toString()
81+
{
82+
return $this->markingValue;
83+
}
84+
};
85+
}
5586
}

0 commit comments

Comments
 (0)