2018-11-08 16:18:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Workflow\MarkingStore;
|
|
|
|
|
|
|
|
use Symfony\Component\Workflow\Exception\LogicException;
|
|
|
|
use Symfony\Component\Workflow\Marking;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MethodMarkingStore stores the marking with a subject's method.
|
|
|
|
*
|
|
|
|
* This store deals with a "single state" or "multiple state" Marking.
|
|
|
|
*
|
2019-03-13 16:02:04 +00:00
|
|
|
* "single state" Marking means a subject can be in one and only one state at
|
2019-05-18 22:50:26 +01:00
|
|
|
* the same time. Use it with state machine.
|
2019-03-13 16:02:04 +00:00
|
|
|
*
|
|
|
|
* "multiple state" Marking means a subject can be in many states at the same
|
|
|
|
* time. Use it with workflow.
|
|
|
|
*
|
2018-11-08 16:18:12 +00:00
|
|
|
* @author Grégoire Pineau <lyrixx@lyrixx.info>
|
|
|
|
*/
|
2019-03-22 13:04:03 +00:00
|
|
|
final class MethodMarkingStore implements MarkingStoreInterface
|
2018-11-08 16:18:12 +00:00
|
|
|
{
|
|
|
|
private $singleState;
|
|
|
|
private $property;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $property Used to determine methods to call
|
|
|
|
* The `getMarking` method will use `$subject->getProperty()`
|
|
|
|
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = array())`
|
|
|
|
*/
|
|
|
|
public function __construct(bool $singleState = false, string $property = 'marking')
|
|
|
|
{
|
|
|
|
$this->singleState = $singleState;
|
|
|
|
$this->property = $property;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getMarking($subject)
|
|
|
|
{
|
|
|
|
$method = 'get'.ucfirst($this->property);
|
|
|
|
|
|
|
|
if (!method_exists($subject, $method)) {
|
|
|
|
throw new LogicException(sprintf('The method "%s::%s()" does not exists.', \get_class($subject), $method));
|
|
|
|
}
|
|
|
|
|
|
|
|
$marking = $subject->{$method}();
|
|
|
|
|
|
|
|
if (!$marking) {
|
|
|
|
return new Marking();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->singleState) {
|
|
|
|
$marking = [$marking => 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Marking($marking);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setMarking($subject, Marking $marking, array $context = [])
|
|
|
|
{
|
|
|
|
$marking = $marking->getPlaces();
|
|
|
|
|
|
|
|
if ($this->singleState) {
|
|
|
|
$marking = key($marking);
|
|
|
|
}
|
|
|
|
|
|
|
|
$method = 'set'.ucfirst($this->property);
|
|
|
|
|
|
|
|
if (!method_exists($subject, $method)) {
|
|
|
|
throw new LogicException(sprintf('The method "%s::%s()" does not exists.', \get_class($subject), $method));
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject->{$method}($marking, $context);
|
|
|
|
}
|
|
|
|
}
|