|
| 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\PropertyAccess\PropertyAccess; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * SingleStateMarkingStore stores the marking with a method of the subject. |
| 19 | + * |
| 20 | + * This store deals with a "single state" Marking. It means a subject can be in |
| 21 | + * one and only one state at the same time. |
| 22 | + * |
| 23 | + * The constructor accept a `property` name. |
| 24 | + * The `getMarking` method will use `$subject->getProperty()`. |
| 25 | + * The `setMarking` method will use `$subject->setProperty($places, $context)`. |
| 26 | + * |
| 27 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 28 | + */ |
| 29 | +class MethodSingleStateMarkingStore implements MarkingStoreInterface |
| 30 | +{ |
| 31 | + private $property; |
| 32 | + |
| 33 | + public function __construct(string $property = 'setMarking') |
| 34 | + { |
| 35 | + $this->property = $property; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + public function getMarking($subject) |
| 42 | + { |
| 43 | + $placeName = $subject->{'get'.ucfirst($this->property)}(); |
| 44 | + |
| 45 | + if (!$placeName) { |
| 46 | + return new Marking(); |
| 47 | + } |
| 48 | + |
| 49 | + return new Marking(array($placeName => 1)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function setMarking($subject, Marking $marking, array $context = array()) |
| 56 | + { |
| 57 | + $subject->{'set'.ucfirst($this->property)}(key($marking->getPlaces()), $context); |
| 58 | + } |
| 59 | +} |
0 commit comments