VisJs::getOutput()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
ccs 25
cts 25
cp 1
rs 8.5806
cc 4
eloc 22
nc 6
nop 1
crap 4
1
<?php
2
3
namespace tomzx\Graphp\VisJs;
4
5
use Fhaculty\Graph\Edge\Directed;
6
use Fhaculty\Graph\Exporter\ExporterInterface;
7
use Fhaculty\Graph\Graph;
8
9
class VisJs implements ExporterInterface
10
{
11
	/**
12
	 * @param \Fhaculty\Graph\Graph $graph
13
	 * @return string
14
	 */
15 6
	public function getOutput(Graph $graph)
16
	{
17 6
		$vertices = [];
18
		/** @var \Fhaculty\Graph\Vertex $vertex */
19 6
		foreach ($graph->getVertices() as $vertex) {
20
			$data = [
21 5
				'id'    => $vertex->getId(),
22 5
			];
23
24 5
			$vertexAttribute = $vertex->getAttributeBag()->getAttributes();
25 5
			$data += $vertexAttribute;
26
27 5
			$vertices[] .= json_encode($data);
28 6
		}
29 6
		$output = 'var nodes = [' . PHP_EOL . implode(',' . PHP_EOL, $vertices) . PHP_EOL . '];' . PHP_EOL;
30
31 6
		$edges = [];
32
		/** @var \Fhaculty\Graph\Edge\Base $edge */
33 6
		foreach ($graph->getEdges() as $edge) {
34 3
			$vertices = $edge->getVertices()->getVector();
35
			$data = [
36 3
				'from' => $vertices[0]->getId(),
37 3
				'to'   => $vertices[1]->getId(),
38 3
			];
39
40 3
			if ($edge instanceof Directed) {
41 2
				$data['arrows'] = 'to';
42 2
			}
43
44 3
			$edgeAttributes = $edge->getAttributeBag()->getAttributes();
45 3
			$data += $edgeAttributes;
46
47 3
			$edges[] = json_encode($data);
48 6
		}
49 6
		$output .= 'var edges = [' . PHP_EOL . implode(',' . PHP_EOL, $edges) . PHP_EOL . '];';
50
51 6
		return $output;
52
	}
53
}
54