|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Soap\WorkflowStorage; |
| 4 | + |
| 5 | +use Illuminate\Support\Arr; |
| 6 | + |
| 7 | +class WorkflowData |
| 8 | +{ |
| 9 | + protected string $name; |
| 10 | + |
| 11 | + protected string $type; |
| 12 | + |
| 13 | + protected array $supports; |
| 14 | + |
| 15 | + protected array $marking_store = []; |
| 16 | + |
| 17 | + protected array $places = []; |
| 18 | + |
| 19 | + protected array $transitions = []; |
| 20 | + |
| 21 | + protected array $events_to_dispatch = []; |
| 22 | + |
| 23 | + public function __construct(string $name, ?string $type = 'workflow', ?array $supports = []) |
| 24 | + { |
| 25 | + $this->name = $name; |
| 26 | + $this->type = $type; |
| 27 | + $this->supports = $supports; |
| 28 | + } |
| 29 | + |
| 30 | + public function addPlace(string $name, bool $initial = false, array $metadata = []): void |
| 31 | + { |
| 32 | + $this->places = Arr::add($this->places, $name, $metadata); |
| 33 | + } |
| 34 | + |
| 35 | + public function addTransition(string $name, string|array $from, string|array $to, array $metadata = []): void |
| 36 | + { |
| 37 | + if (is_string($from)) { |
| 38 | + $from = [$from]; |
| 39 | + } |
| 40 | + |
| 41 | + foreach ($from as $place) { |
| 42 | + $this->ensurePlaceExists($place); |
| 43 | + } |
| 44 | + |
| 45 | + if (is_string($to)) { |
| 46 | + $to = [$to]; |
| 47 | + } |
| 48 | + |
| 49 | + foreach ($to as $place) { |
| 50 | + $this->ensurePlaceExists($place); |
| 51 | + } |
| 52 | + |
| 53 | + $this->transitions = Arr::add($this->transitions, $name, [ |
| 54 | + 'from' => $from, |
| 55 | + 'to' => $to, |
| 56 | + 'metadata' => $metadata, |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + protected function ensurePlaceExists(string $place): void |
| 61 | + { |
| 62 | + if (! array_key_exists($place, $this->places)) { |
| 63 | + } |
| 64 | + $this->addPlace($place); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + public function getPlaces(): array |
| 69 | + { |
| 70 | + return $this->places; |
| 71 | + } |
| 72 | + |
| 73 | + public function getTransitions(): array |
| 74 | + { |
| 75 | + return $this->transitions; |
| 76 | + } |
| 77 | + |
| 78 | + public function toArray(): array |
| 79 | + { |
| 80 | + $workflow = [ |
| 81 | + $this->name => [ |
| 82 | + 'type' => $this->type, |
| 83 | + 'supports' => $this->supports, |
| 84 | + 'marking_store' => $this->marking_store, |
| 85 | + 'places' => $this->places, |
| 86 | + 'transitions' => $this->transitions, |
| 87 | + 'events_to_dispatch' => $this->events_to_dispatch, |
| 88 | + ], |
| 89 | + ]; |
| 90 | + |
| 91 | + return $workflow; |
| 92 | + } |
| 93 | +} |
0 commit comments