Skip to content

Commit 950882e

Browse files
authored
Merge pull request #2 from soap/develop
Develop
2 parents dfd476a + f1b038f commit 950882e

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/WorkflowData.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

tests/Unit/WorkflowDataTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Soap\WorkflowStorage\WorkflowData;
4+
5+
it('can create a workflow data', function () {
6+
$wfData = new WorkflowData(name: 'article', supports: ["App\Models\Article"]);
7+
$wfData->addPlace('draft');
8+
$wfData->addPlace('on review');
9+
$wfData->addTransition('submit', 'draft', 'on review');
10+
$wfData->addTransition('approve', 'on review', 'approved');
11+
$wfData->addTransition('reject', 'on review', 'rejected');
12+
13+
$output = $wfData->toArray();
14+
15+
expect(data_get($output, 'article.transitions.submit.from'))->toBe(['draft']);
16+
expect(data_get($output, 'article.transitions.submit.to'))->toBe(['on review']);
17+
expect(data_get($output, 'article.transitions.approve.from'))->toBe(['on review']);
18+
expect(data_get($output, 'article.transitions.approve.to'))->toBe(['approved']);
19+
expect(data_get($output, 'article.transitions.reject.from'))->toBe(['on review']);
20+
expect(data_get($output, 'article.transitions.reject.to'))->toBe(['rejected']);
21+
22+
expect(count(data_get($output, 'article.places')))->toBe(4);
23+
});

0 commit comments

Comments
 (0)