Skip to content

Commit 63e3cf1

Browse files
committed
Add GraphML Exporter
1 parent 6cbba33 commit 63e3cf1

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/Exporter.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Graphp\GraphML;
4+
5+
use Fhaculty\Graph\Exporter\ExporterInterface;
6+
use Fhaculty\Graph\Graph;
7+
use SimpleXMLElement;
8+
use Fhaculty\Graph\Edge\Directed;
9+
10+
class Exporter implements ExporterInterface
11+
{
12+
const SKEL = <<<EOL
13+
<?xml version="1.0" encoding="UTF-8"?>
14+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
17+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
18+
</graphml>
19+
EOL;
20+
21+
public function getOutput(Graph $graph)
22+
{
23+
$root = new SimpleXMLElement(self::SKEL);
24+
25+
$graphElem = $root->addChild('graph');
26+
$graphElem['edgeDefault'] = 'undirected';
27+
28+
foreach ($graph->getVertices()->getMap() as $id => $vertex) {
29+
/* @var $vertex Vertex */
30+
$vertexElem = $graphElem->addChild('node');
31+
$vertexElem['id'] = $id;
32+
}
33+
34+
foreach ($graph->getEdges() as $edge) {
35+
/* @var $edge Edge */
36+
$edgeElem = $graphElem->addChild('edge');
37+
$edgeElem['source'] = $edge->getVertices()->getVertexFirst()->getId();
38+
$edgeElem['target'] = $edge->getVertices()->getVertexLast()->getId();
39+
40+
if ($edge instanceof Directed) {
41+
$edgeElem['directed'] = 'true';
42+
}
43+
}
44+
45+
return $root->asXML();
46+
}
47+
}

tests/ExporterTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Graph;
4+
use Graphp\GraphML\Exporter;
5+
6+
class ExporterTest extends TestCase
7+
{
8+
private $exporter;
9+
10+
public function setUp()
11+
{
12+
$this->exporter = new Exporter();
13+
}
14+
15+
public function testEmpty()
16+
{
17+
$graph = new Graph();
18+
19+
$output = $this->exporter->getOutput($graph);
20+
$xml = new SimpleXMLElement($output);
21+
22+
$this->assertEquals(1, count($xml));
23+
$this->assertEquals(1, count($xml->graph));
24+
$this->assertEquals(0, count($xml->graph->children()));
25+
}
26+
27+
public function testSimple()
28+
{
29+
// 1 -- 2
30+
$graph = new Graph();
31+
$v1 = $graph->createVertex(1);
32+
$v2 = $graph->createVertex(2);
33+
$v1->createEdge($v2);
34+
35+
$output = $this->exporter->getOutput($graph);
36+
$xml = new SimpleXMLElement($output);
37+
38+
$this->assertEquals(1, count($xml->graph->edge));
39+
40+
$edgeElem = $xml->graph->edge;
41+
$this->assertEquals('1', (string)$edgeElem['source']);
42+
$this->assertEquals('2', (string)$edgeElem['target']);
43+
$this->assertFalse(isset($edgeElem['directed']));
44+
}
45+
46+
public function testSimpleDirected()
47+
{
48+
// 1 -> 2
49+
$graph = new Graph();
50+
$v1 = $graph->createVertex(1);
51+
$v2 = $graph->createVertex(2);
52+
$v1->createEdgeTo($v2);
53+
54+
$output = $this->exporter->getOutput($graph);
55+
$xml = new SimpleXMLElement($output);
56+
57+
$this->assertEquals(1, count($xml->graph->edge));
58+
59+
$edgeElem = $xml->graph->edge;
60+
$this->assertEquals('1', (string)$edgeElem['source']);
61+
$this->assertEquals('2', (string)$edgeElem['target']);
62+
$this->assertEquals('true', (string)$edgeElem['directed']);
63+
}
64+
}

0 commit comments

Comments
 (0)