Skip to content

Commit

Permalink
支持使用数字作为状态
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Apr 6, 2020
1 parent 15e9ae4 commit 8cbd0b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/ModelMarkingStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@
class ModelMarkingStore implements MarkingStoreInterface
{
private $property;
private $map;

public function __construct(string $property = 'status')
public function __construct(string $property = 'status', ?array $map = null)
{
$this->property = $property;
$this->map = $map;
}

protected function getPlace($marking)
{
if (empty($this->map)) {
return $marking;
}
return $this->map[$marking];
}

/**
Expand All @@ -21,7 +31,7 @@ public function __construct(string $property = 'status')
*/
public function getMarking(object $subject)
{
$marking = $subject->getAttr($this->property);
$marking = $this->getPlace($subject->getAttr($this->property));

if (!$marking) {
return new Marking();
Expand All @@ -42,7 +52,7 @@ public function setMarking(object $subject, Marking $marking, array $context = [

$marking = key($marking);

$subject->setAttr($this->property, $marking);
$subject->setAttr($this->property, $this->getPlace($marking));

$subject->save($context);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public function boot(Reader $reader)
foreach ($annotations as $annotation) {
if ($annotation instanceof StateMachine) {
$builder = new DefinitionBuilder();
$builder->addPlaces($annotation->places);
$map = null;
if (isset($annotation->places[0])) {
$places = $annotation->places;
} else {
$places = array_keys($annotation->places);
$map = $annotation->places;
}
$builder->addPlaces($places);
foreach ($annotation->transitions as $transition) {
foreach ((array) $transition->from as $from) {
foreach ((array) $transition->to as $to) {
Expand All @@ -47,7 +54,7 @@ public function boot(Reader $reader)
}
$builder->setInitialPlaces($annotation->initial);
$definition = $builder->build();
$marking = new ModelMarkingStore($annotation->value);
$marking = new ModelMarkingStore($annotation->value, $map);
$stateMachine = new Workflow($definition, $marking, null, get_class($model) . "@" . $annotation->value);

$registry->addWorkflow($stateMachine, new InstanceOfSupportStrategy(get_class($model)));
Expand Down

0 comments on commit 8cbd0b4

Please sign in to comment.