Apache Zeta Components Manual :: File Source for plain_text.php
Source for file plain_text.php
Documentation is available at plain_text.php
* File containing the ezcTreeVisitorPlainText class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version //autogentag//
* An implementation of the ezcTreeVisitor interface that generates
* a plain text representation of a tree structure.
* $visitor = new ezcTreeVisitorPlainText( ezcTreeVisitorPlainText::SYMBOL_UTF8 );
* $tree->accept( $visitor );
* echo (string) $visitor; // print the plot
* Shows (something like):
* │ ├─Müller's Bornean Gibbon
* │ ├─Western Hoolock Gibbon
* │ └─Eastern Hoolock Gibbon
* ├─Eastern Black Crested Gibbon
* ├─White-cheecked Crested Gibbon
* └─Yellow-cheecked Gibbon
* @version //autogentag//
* Represents the ASCII symbol set.
* Represents the UTF-8 symbol set.
* Holds all the edges of the graph.
* @var array(string=>array(string))
* Constructs a new ezcTreeVisitorPlainText visualizer using $symbolCharset
* This class only supports 'ascii' and 'utf-8' as character sets. Default is
* @param int $symbolCharset
public function __construct( $symbolCharset =
self::SYMBOL_UTF8 )
switch ( $symbolCharset )
$symbols =
array( '|', '+', '-', '+' );
$symbols =
array( '│', '├', '─', '└' );
$this->symbolPipe =
$symbols[0];
$this->symbolTee =
$symbols[1];
$this->symbolLine =
$symbols[2];
$this->symbolCorner =
$symbols[3];
* Visits the node and sets the the member variables according to the node
* @param ezcTreeVisitable $visitable
public function visit( ezcTreeVisitable $visitable )
if ( $this->root ===
null )
$this->root =
$visitable->id;
$parent =
$visitable->fetchParent();
$this->edges[$parent->id][] =
$visitable->id;
* Loops over the children of the node with ID $id.
* This methods loops over all the node's children and adds the correct
* layout for each node depending on the state that is collected in the
* $level and $levelLast variables.
* @param array(int=>bool) $levelLast
private function doChildren( $id, $level =
0, $levelLast =
array() )
if ( !isset
( $this->edges[$id] ) )
$children =
$this->edges[$id];
$numChildren =
count( $children );
foreach ( $children as $child )
for ( $i =
0; $i <
$level; $i++
)
if ( isset
( $levelLast[$i] ) &&
$levelLast[$i] )
$text .=
"{
$this->symbolPipe} ";
if ( $count != $numChildren )
$text .= "{
$this->symbolTee}{
$this->symbolLine}";
$levelLast[$level] = false;
$text .= "{
$this->symbolCorner}{
$this->symbolLine}";
$levelLast[$level] = true;
$text .= $this->doChildren( $child, $level + 1, $levelLast );
* Returns the text representatation of a tree.
public function __toString()
$tree = $this->root . "\n";
$tree .= $this->doChildren( $this->root );