99use Fhaculty \Graph \Exception \InvalidArgumentException ;
1010use Fhaculty \Graph \Edge \Base as Edge ;
1111use \stdClass ;
12+ use Fhaculty \Graph \Attribute \AttributeBagNamespaced ;
1213
1314class GraphViz
1415{
@@ -26,9 +27,6 @@ class GraphViz
2627 */
2728 private $ format = 'png ' ;
2829
29- private $ layoutVertex = array ();
30- private $ layoutEdge = array ();
31-
3230 /**
3331 * Either the name of full path to GraphViz layout.
3432 *
@@ -151,21 +149,6 @@ public function display()
151149 const LAYOUT_EDGE = 2 ;
152150 const LAYOUT_VERTEX = 3 ;
153151
154- private function mergeLayout (&$ old , $ new )
155- {
156- if ($ new === NULL ) {
157- $ old = array ();
158- } else {
159- foreach ($ new as $ key => $ value ) {
160- if ($ value === NULL ) {
161- unset($ old [$ key ]);
162- } else {
163- $ old [$ key ] = $ value ;
164- }
165- }
166- }
167- }
168-
169152 public function setLayout ($ where , $ layout , $ value = NULL )
170153 {
171154 if (!is_array ($ where )) {
@@ -174,13 +157,17 @@ public function setLayout($where, $layout, $value = NULL)
174157 if (func_num_args () > 2 ) {
175158 $ layout = array ($ layout => $ value );
176159 }
160+
161+ $ map = array (
162+ self ::LAYOUT_GRAPH => 'graphviz.graph. ' ,
163+ self ::LAYOUT_EDGE => 'graphviz.edge. ' ,
164+ self ::LAYOUT_VERTEX => 'graphviz.node ' ,
165+ );
166+
177167 foreach ($ where as $ where ) {
178- if ($ where === self ::LAYOUT_GRAPH ) {
179- $ this ->graph ->setLayout ($ layout , $ value );
180- } elseif ($ where === self ::LAYOUT_EDGE ) {
181- $ this ->mergeLayout ($ this ->layoutEdge , $ layout );
182- } elseif ($ where === self ::LAYOUT_VERTEX ) {
183- $ this ->mergeLayout ($ this ->layoutVertex , $ layout );
168+ if (isset ($ map [$ where ])) {
169+ $ bag = new AttributeBagNamespaced ($ this ->graph , $ map [$ where ]);
170+ $ bag ->setAttributes ($ layout );
184171 } else {
185172 throw new InvalidArgumentException ('Invalid layout identifier ' );
186173 }
@@ -285,15 +272,18 @@ public function createScript()
285272 $ script = ($ directed ? 'di ' :'' ) . 'graph G { ' . self ::EOL ;
286273
287274 // add global attributes
288- $ layout = $ this ->graph ->getLayout ();
289- if ($ layout ) {
290- $ script .= $ this ->formatIndent . 'graph ' . $ this ->escapeAttributes ($ layout ) . self ::EOL ;
291- }
292- if ($ this ->layoutVertex ) {
293- $ script .= $ this ->formatIndent . 'node ' . $ this ->escapeAttributes ($ this ->layoutVertex ) . self ::EOL ;
294- }
295- if ($ this ->layoutEdge ) {
296- $ script .= $ this ->formatIndent . 'edge ' . $ this ->escapeAttributes ($ this ->layoutEdge ) . self ::EOL ;
275+ $ globals = array (
276+ 'graph ' => 'graphviz.graph. ' ,
277+ 'node ' => 'graphviz.node. ' ,
278+ 'edge ' => 'graphviz.edge. ' ,
279+ );
280+
281+ foreach ($ globals as $ key => $ prefix ) {
282+ $ bag = new AttributeBagNamespaced ($ this ->graph , $ prefix );
283+
284+ if ($ layout = $ bag ->getAttributes ()) {
285+ $ script .= $ this ->formatIndent . $ key . ' ' . $ this ->escapeAttributes ($ layout ) . self ::EOL ;
286+ }
297287 }
298288
299289 $ alg = new Groups ($ this ->graph );
@@ -428,7 +418,8 @@ public static function raw($string)
428418
429419 protected function getLayoutVertex (Vertex $ vertex )
430420 {
431- $ layout = $ vertex ->getLayout ();
421+ $ bag = new AttributeBagNamespaced ($ vertex , 'graphviz. ' );
422+ $ layout = $ bag ->getAttributes ();
432423
433424 $ balance = $ vertex ->getBalance ();
434425 if ($ balance !== NULL ){
@@ -446,7 +437,8 @@ protected function getLayoutVertex(Vertex $vertex)
446437
447438 protected function getLayoutEdge (Edge $ edge )
448439 {
449- $ layout = $ edge ->getLayout ();
440+ $ bag = new AttributeBagNamespaced ($ edge , 'graphviz. ' );
441+ $ layout = $ bag ->getAttributes ();
450442
451443 // use flow/capacity/weight as edge label
452444 $ label = NULL ;
0 commit comments