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

## Source for file plain_text.php

Documentation is available at plain_text.php

1. <?php
2. /**
3. * File containing the ezcTreeVisitorPlainText 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. * a plain text representation of a tree structure.
31. *
32. * <code>
33. * <?php
34. * $visitor = new ezcTreeVisitorPlainText( ezcTreeVisitorPlainText::SYMBOL_UTF8 );
35. * $tree->accept( $visitor );
36. * echo (string) $visitor; // print the plot
37. * ?>
38. * </code>
39. *
40. * Shows (something like):
41. * <code>
42. * Hylobatidae
43. * ├─Hylobates
44. * │ ├─Lar Gibbon
45. * │ ├─Agile Gibbon
46. * │ ├─Müller's Bornean Gibbon
47. * │ ├─Silvery Gibbon
48. * │ ├─Pileated Gibbon
49. * │ └─Kloss's Gibbon
50. * ├─Hoolock
51. * │ ├─Western Hoolock Gibbon
52. * │ └─Eastern Hoolock Gibbon
53. * ├─Symphalangus
54. * └─Nomascus
55. * ├─Black Crested Gibbon
56. * ├─Eastern Black Crested Gibbon
57. * ├─White-cheecked Crested Gibbon
58. * └─Yellow-cheecked Gibbon
59. * </code>
60. *
61. * @package Tree
62. * @version //autogentag//
63. */
64. class ezcTreeVisitorPlainText implements ezcTreeVisitor
65. {
66. /**
67. * Represents the ASCII symbol set.
68. */
69. const SYMBOL_ASCII = 1;
71. /**
72. * Represents the UTF-8 symbol set.
73. */
74. const SYMBOL_UTF8 = 2;
76. /**
77. * Holds all the edges of the graph.
78. *
79. * @var array(string=>array(string))
80. */
81. protected $edges = array();
83. /**
84. * Holds the root ID
85. *
86. * @var string
87. */
88. protected $root = null;
90. /**
91. * Constructs a new ezcTreeVisitorPlainText visualizer using $symbolCharset
92. * as character set.
93. *
94. * This class only supports 'ascii' and 'utf-8' as character sets. Default is
95. * 'utf-8'.
96. *
97. * @see SYMBOL_UTF8
98. * @see SYMBOL_ASCII
99. *
100. * @param int $symbolCharset
101. */
102. public function __construct ( $symbolCharset = self::SYMBOL_UTF8 )
103. {
104. switch ( $symbolCharset )
105. {
106. case self::SYMBOL_ASCII:
107. $symbols = array( '|', '+', '-', '+' );
108. break;
109. case self::SYMBOL_UTF8:
110. default:
111. $symbols = array( '│', '├', '─', '└' );
112. }
113. $this->symbolPipe = $symbols[0];
114. $this->symbolTee = $symbols[1];
115. $this->symbolLine = $symbols[2];
116. $this->symbolCorner = $symbols[3];
117. }
119. /**
120. * Visits the node and sets the the member variables according to the node
121. * type and contents.
122. *
123. * @param ezcTreeVisitable $visitable
124. * @return bool
125. */
126. public function visit ( ezcTreeVisitable $visitable )
127. {
128. if ( $visitable instanceof ezcTreeNode )
129. {
130. if ( $this-> root === null )
131. {
132. $this-> root = $visitable->id;
133. }
135. $parent = $visitable->fetchParent();
136. if ( $parent )
137. {
138. $this-> edges [$parent->id][] = $visitable->id;
139. }
140. }
142. return true;
143. }
145. /**
146. * Loops over the children of the node with ID $id.
147. *
148. * This methods loops over all the node's children and adds the correct
149. * layout for each node depending on the state that is collected in the
150. * $level and $levelLast variables.
151. *
152. * @param string $id
153. * @param int $level
154. * @param array(int=>bool) $levelLast
155. *
156. * @return string
157. */
158. private function doChildren( $id, $level = 0, $levelLast = array() )
159. {
160. $text = '';
162. if ( !isset( $this-> edges [$id] ) )
163. {
164. return $text;
165. }
166. $children = $this-> edges [$id];
167. $numChildren = count ( $children );
169. $count = 0;
170. foreach ( $children as $child )
171. {
172. $count++;
173. for ( $i = 0; $i < $level; $i++ )
174. {
175. if ( isset( $levelLast[$i] ) && $levelLast[$i] )
176. {
177. $text .= ' ';
178. }
179. else
180. {
181. $text .= "{$this->symbolPipe} ";
182. }
183. }
184. if ( $count != $numChildren )
185. {
186. $text .= "{$this->symbolTee}{$this->symbolLine}";

$levelLast[$level] = false;
- }
- else
- {
- $text .= "{$this->symbolCorner}{$this->symbolLine}";
- $levelLast[$level] = true;
- }
- $text .= $child . "\n";
- $text .= $this->doChildren( $child, $level + 1, $levelLast );
- }
- return $text;
- }
- /**
- * Returns the text representatation of a tree.
- *
- * @return string
- * @ignore
- */
- public function __toString()
- {
- $tree = $this-> root . "\n";
- $tree .= $this->doChildren( $this-> root );
- return $tree;
- }
- }
Documentation generated by phpDocumentor 1.4.3