-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
62 lines (55 loc) · 2.03 KB
/
Parser.php
File metadata and controls
62 lines (55 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Ezweb\Workflow;
class Parser
{
/**
* @param string $json
* @return Workflow
*/
public static function createFromJson(string $json): Workflow
{
$decodedJson = json_decode($json);
if ($decodedJson === false) {
throw new \RuntimeException('Invalid JSON: ' . json_last_error_msg());
}
$workflow = new Workflow($decodedJson->name);
foreach ($decodedJson->value as $value) {
$parsedValue = self::parse($value);
if ($parsedValue instanceof \Ezweb\Workflow\Elements\Types\ParentTypes\Rule) {
$workflow->addRule($parsedValue);
}
}
return $workflow;
}
/**
* @param \stdClass $decodedData
* @return Elements\Types\Type
*/
private static function parse(\stdClass $decodedData, Loader $configLoader = null): Elements\Types\Type
{
if (!isset($decodedData->type)) {
throw new \RuntimeException(
'Object type property must be defined:'
. PHP_EOL
. json_encode($decodedData, JSON_PRETTY_PRINT)
);
}
if ($configLoader === null) {
$configLoader = new Loader();
}
$classType = $configLoader->getTypeProviderConfig()->getClass($decodedData->type);
/** @var \Ezweb\Workflow\Elements\Types\Type $classType */
$typeElement = $classType::createFromParser($decodedData, $configLoader);
if (\is_array($decodedData->value)) {
/** @var \Ezweb\Workflow\Elements\Types\ParentTypes\ParentType $typeElement */
/** @var mixed $value */
foreach ($decodedData->value as $value) {
$typeElement->addValue(self::parse($value));
}
} elseif ($decodedData->value instanceof \stdClass) {
/** @var \Ezweb\Workflow\Elements\Types\ParentTypes\ParentType $typeElement */
$typeElement->addValue(self::parse($decodedData->value));
}
return $typeElement;
}
}