|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Workflow\MarkingStore; |
| 13 | + |
| 14 | +use Symfony\Component\Workflow\Exception\LogicException; |
| 15 | +use Symfony\Component\Workflow\Marking; |
| 16 | + |
| 17 | +/** |
| 18 | + * PropertiesMarkingStore stores the marking with a subject's properties. |
| 19 | + * |
| 20 | + * This store deals with a "single state" or "multiple state" Marking. |
| 21 | + * |
| 22 | + * "single state" Marking means a subject can be in one and only one state at |
| 23 | + * the same time. Use it with state machine. It uses a string to store the marking |
| 24 | + * |
| 25 | + * "multiple state" Marking means a subject can be in many states at the same |
| 26 | + * time. Use it with workflow. It uses an array to store the marking |
| 27 | + * |
| 28 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 29 | + */ |
| 30 | +final class PropertiesMarkingStore implements MarkingStoreInterface |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + private bool $singleState = false, |
| 34 | + private string $property = 'marking', |
| 35 | + private ?string $contextProperty = 'markingContext', |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function getMarking(object $subject): Marking |
| 40 | + { |
| 41 | + if (!property_exists($subject, $this->property)) { |
| 42 | + throw new LogicException(sprintf('The property "%s::$%s" does not exist.', get_debug_type($subject), $this->property)); |
| 43 | + } |
| 44 | + |
| 45 | + $marking = null; |
| 46 | + try { |
| 47 | + $marking = $subject->{$this->property}; |
| 48 | + } catch (\Error $e) { |
| 49 | + $unInitializedPropertyMessage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property); |
| 50 | + if ($e->getMessage() !== $unInitializedPropertyMessage) { |
| 51 | + throw $e; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (null === $marking) { |
| 56 | + return new Marking(); |
| 57 | + } |
| 58 | + |
| 59 | + if ($this->singleState) { |
| 60 | + $marking = [(string) $marking => 1]; |
| 61 | + } elseif (!\is_array($marking)) { |
| 62 | + throw new LogicException(sprintf('The property "%s::$%s" did not return an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject), $this->property)); |
| 63 | + } |
| 64 | + |
| 65 | + return new Ma
10000
rking($marking); |
| 66 | + } |
| 67 | + |
| 68 | + public function setMarking(object $subject, Marking $marking, array $context = []): void |
| 69 | + { |
| 70 | + $marking = $marking->getPlaces(); |
| 71 | + |
| 72 | + if ($this->singleState) { |
| 73 | + $marking = key($marking); |
| 74 | + } |
| 75 | + |
| 76 | + if (!property_exists($subject, $this->property)) { |
| 77 | + throw new LogicException(sprintf('The property "%s::$%s" does not exist.', get_debug_type($subject), $this->property)); |
| 78 | + } |
| 79 | + $subject->{$this->property} = $marking; |
| 80 | + |
| 81 | + if (null !== $this->contextProperty) { |
| 82 | + if (!property_exists($subject, $this->contextProperty)) { |
| 83 | + throw new LogicException(sprintf('The property "%s::$%s" does not exist.', get_debug_type($subject), $this->contextProperty)); |
| 84 | + } |
| 85 | + $subject->{$this->contextProperty} = $context; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments