## Apache Zeta Components Manual :: File Source for graphviz.php

## Source for file graphviz.php

Documentation is available at graphviz.php

1. <?php
2. /**
3. * File containing the ezcTreeVisitorGraphViz class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package Tree
26. */
28. /**
29. * An implementation of the ezcTreeVisitor interface that generates
30. * GraphViz/dot markup for a tree structure.
31. *
32. * <code>
33. * <?php
34. * $visitor = new ezcTreeVisitorGraphViz;
35. * $tree->accept( $visitor );
36. * echo (string) $visitor; // print the plot
37. * ?>
38. * </code>
39. *
40. * @package Tree
41. * @version //autogentag//
42. */
43. class ezcTreeVisitorGraphViz implements ezcTreeVisitor
44. {
45. /**
46. * Holds the displayed strings for each of the nodes.
47. *
48. * @var array(string=>string)
49. */
50. protected $nodes = array();
52. /**
53. * Holds all the edges of the graph.
54. *
55. * @var array(id=>array(ezcTreeNode))
56. */
57. protected $edges = array();
59. /**
60. * Creates a graphviz compliant ID out of the ID identifying $node.
61. *
62. * @param ezcTreeNode $node
63. * @return string
64. */
65. private function createId( ezcTreeNode $node )
66. {
67. return preg_replace ( '/[^A-Za-z0-9_]/', '', $node->id ) . '_'. base_convert ( sprintf ( '%u', crc32 ( $node->id ) ), 16, 36 );
68. }
70. /**
71. * Visits the node and sets the the member variables according to the node
72. * type and contents.
73. *
74. * @param ezcTreeVisitable $visitable
75. * @return bool
76. */
77. public function visit ( ezcTreeVisitable $visitable )
78. {
79. if ( $visitable instanceof ezcTreeNode )
80. {
81. $id = $this->createId( $visitable );
82. $this-> nodes [$id] = $visitable->id;
84. $parent = $visitable->fetchParent();
85. if ( $parent )
86. {
87. $parentId = $this->createId( $parent );
88. $this-> edges [$parentId][] = $id;
89. }
90. }
92. return true;
93. }
95. /**
96. * Returns the contents as a graphviz .dot file structure.
97. *
98. * @return string
99. * @ignore
100. */
101. public function __toString()
102. {
103. $dot = "digraph Tree {\n";
105. foreach ( $this-> nodes as $key => $value )
106. {
107. $dot .= sprintf (
108. "node%s [label=\"%s\"]\n",
109. $key,
110. $value
111. );
112. }
114. $dot .= "\n";
116. foreach ( $this-> edges as $fromNode => $toNodes )
117. {
118. foreach ( $toNodes as $toNode )
119. {
120. $dot .= sprintf (
121. "node%s -> node%s\n",
123. $fromNode,
124. $toNode
125. );
126. }
127. }
129. return $dot . "}\n";
130. }
131. }
132. ?>

Documentation generated by phpDocumentor 1.4.3