-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFbpDumper.php
221 lines (197 loc) · 6.35 KB
/
FbpDumper.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
* This file is part of the phpflo\phpflo-fbp package.
*
* (c) Marc Aschmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpFlo\Fbp;
use PhpFlo\Common\FbpDefinitionsInterface;
use PhpFlo\Common\Exception\DumperException;
use Symfony\Component\Yaml\Yaml;
/**
* Class FbpDumper
*
* @package PhpFlo\Fbp
* @author Marc Aschmann <[email protected]>
*/
final class FbpDumper implements FbpDefinitionsInterface
{
/**
* @var array
*/
private static $processes;
/**
* @param array $definition
* @return string json
*/
public static function toJson(array $definition): string
{
return json_encode($definition, JSON_PRETTY_PRINT);
}
/**
* @param array $definition
* @param int $inline level until inlining starts
* @return string yaml
*/
public static function toYaml(array $definition, int $inline = 3): string
{
return Yaml::dump($definition, $inline);
}
/**
* @param array $definition
* @return string
*/
public static function toFbp(array $definition): string
{
return self::createFbp($definition);
}
/**
* @param array $definition
* @return string
* @throws DumperException
*/
private static function createFbp(array $definition): string
{
$fbp = [];
try {
if (self::hasElement(self::PROPERTIES_LABEL, $definition)) {
foreach($definition[self::PROPERTIES_LABEL] as $name => $element) {
if (!empty($element)) {
$fbp[] = '# ' . (string)$element;
}
}
}
// first check for process definitions
if (self::hasElement(self::PROCESSES_LABEL, $definition)) {
self::$processes = $definition[self::PROCESSES_LABEL];
}
// handle initializer
if (!empty($definition[self::INITIALIZERS_LABEL])) {
foreach ($definition[self::INITIALIZERS_LABEL] as $initializer) {
if (empty($initializer[self::DATA_LABEL])) {
self::throwDumperException('no_definition', self::DATA_LABEL);
}
if (empty($initializer[self::TARGET_LABEL])) {
self::throwDumperException('no_definition', self::TARGET_LABEL);
}
array_push(
$fbp,
self::connectPorts(
$initializer[self::DATA_LABEL],
self::examineProcess(self::TARGET_LABEL, $initializer[self::TARGET_LABEL])
)
);
}
}
foreach ($definition[self::CONNECTIONS_LABEL] as $connection) {
array_push($fbp, self::examineConnectionTouple($connection));
}
return implode(self::FILE_LINEFEED, $fbp);
} catch (\Exception $e) {
throw new DumperException(
"Unexpected dumper error \"{$e->getMessage()}\" in {$e->getFile()} on Line {$e->getLine()}"
);
}
}
/**
* Look for all needed fields and build a port -> port connection.
*
* @param array $connectionTouple
* @return string
*/
private static function examineConnectionTouple(array $connectionTouple): string
{
self::hasElement(self::SOURCE_LABEL, $connectionTouple);
self::hasElement(self::TARGET_LABEL, $connectionTouple);
return self::connectPorts(
self::examineProcess(self::SOURCE_LABEL, $connectionTouple[self::SOURCE_LABEL]),
self::examineProcess(self::TARGET_LABEL, $connectionTouple[self::TARGET_LABEL])
);
}
/**
* @param string $type
* @param array $processPart
* @throws DumperException
* @return string
*/
private static function examineProcess(string $type, array $processPart): string
{
self::hasElement(self::PROCESS_LABEL, $processPart);
self::hasElement(self::PORT_LABEL, $processPart);
$inport = '';
$outport = '';
$process = $processPart[self::PROCESS_LABEL];
$port = $processPart[self::PORT_LABEL];
if (self::hasElement($process, self::$processes, false)) {
$meta = "(" . self::$processes[$process][self::COMPONENT_LABEL] . ")";
} else {
self::throwDumperException('process', $process);
}
if (self::SOURCE_LABEL == $type) {
$outport = " {$port}";
} else {
$inport = "{$port} ";
}
return "{$inport}{$process}{$meta}{$outport}";
}
/**
* @param string $needle
* @param array $haystack
* @param bool $triggerException
* @return bool
*/
private static function hasElement(
string $needle,
array $haystack,
bool $triggerException = true
): bool {
if (empty($haystack[$needle])) {
if ($triggerException) {
self::throwDumperException('elmeent', $needle);
} else {
return false;
}
}
return true;
}
/**
* @param string $sourcePort
* @param string $targetPort
* @return string
*/
private static function connectPorts(string $sourcePort, string $targetPort): string
{
return implode(
" " . self::SOURCE_TARGET_SEPARATOR . " ",
[
$sourcePort,
$targetPort
]
);
}
/**
* @param string $type
* @param string $value
* @throws DumperException
*/
private static function throwDumperException(string $type, string $value)
{
switch ($type) {
case 'element':
throw new DumperException("Element has no {$value}");
break;
case 'process':
throw new DumperException("{$value} is not defined in " . self::PROCESSES_LABEL);
break;
case 'no_definition':
throw new DumperException("Defintion has " .
self::INITIALIZERS_LABEL . " but no {$value} node"
);
break;
}
}
}