Skip to content

Commit 3d53299

Browse files
committed
Add some methods to WorkflowContext
1 parent 423d27f commit 3d53299

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Workflow/WorkflowContext.php

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ interface WorkflowContext
1818
*/
1919
public function get(string $slotName);
2020

21+
public function has(string $slotName): bool;
22+
23+
/**
24+
* @param string $slotName
25+
* @param string|null $type Checks also slot type
26+
*/
27+
public function assertSlot(string $slotName, string $type = null): void;
28+
2129
/**
2230
* Returns input data for given description
2331
*
@@ -31,4 +39,9 @@ public function getByDescription(DescriptionWithInputSlot $description): array;
3139
* @param mixed $slotValue
3240
*/
3341
public function put(string $slotName, $slotValue): void;
42+
43+
/**
44+
* @return string[]
45+
*/
46+
public function getSlotNames(): array;
3447
}

src/Workflow/WorkflowContextMap.php

+50
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace OpenCodeModeling\CodeGenerator\Workflow;
1212

13+
use OpenCodeModeling\CodeGenerator\Exception\MissingSlotName;
14+
use OpenCodeModeling\CodeGenerator\Exception\WrongSlotType;
15+
1316
final class WorkflowContextMap implements WorkflowContext
1417
{
1518
/**
@@ -27,6 +30,53 @@ public function put(string $slotName, $slotValue): void
2730
$this->map[$slotName] = $slotValue;
2831
}
2932

33+
public function has(string $slotName): bool
34+
{
35+
return isset($this->map[$slotName]);
36+
}
37+
38+
public function assertSlot(string $slotName, string $type = null): void
39+
{
40+
if (! $this->has($slotName)) {
41+
throw new MissingSlotName($slotName);
42+
}
43+
if ($type !== null) {
44+
$slot = $this->get($slotName);
45+
46+
switch ($type) {
47+
case 'int':
48+
case 'integer':
49+
$isType = \is_int($slot);
50+
break;
51+
case 'string':
52+
$isType = \is_string($slot);
53+
break;
54+
case 'bool':
55+
case 'boolean':
56+
$isType = \is_bool($slot);
57+
break;
58+
case 'array':
59+
$isType = \is_array($slot);
60+
break;
61+
default:
62+
$isType = $slot instanceof $type;
63+
break;
64+
}
65+
66+
if ($isType === false) {
67+
throw WrongSlotType::withSlotName($slotName, $type, (\is_object($slot) ? \get_class($slot) : \gettype($slot)));
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @return string[]
74+
*/
75+
public function getSlotNames(): array
76+
{
77+
return \array_keys($this->map);
78+
}
79+
3080
public function getByDescription(DescriptionWithInputSlot $description): array
3181
{
3282
$input = [];

0 commit comments

Comments
 (0)