<?php
// Loading Abstract Data Type pTree
require_once "class.pTree.php";
// Creating tree
$tree = new pTree;
echo "<h1>Tree (Separator: \"/\")</h1>";
// Testing add with unlimited depth and value type
// add(nodeName [, nodeValue])
// PS.: Spaces are allowed, but converted to "_" char
$tree->add("child1");
$tree->add("child2", "value");
$tree->add("child3", array(1 => "value1", "item2" => 200));
$tree->add("child1/sub-child1", "valueTest");
$tree->add("child1/sub-child2", 200);
$tree->add("child1/sub-child3");
$tree->add("child4", "value");
$tree->add("child5");
$tree->add("child5/sub-child5.1");
$tree->add("child5/sub-child5.1/sub-child5.1.1");
$tree->add("child5/sub-child5.1/sub-child5.1.1/sub-child5.1.1.1");
$tree->add("child5/sub-child5.1/sub-child5.1.1/sub-child5.1.1.1/sub-child5.1.1.1.1", "Deep... but with value. =)");
// Test commenting a node (can be any node)
// comment(nodeName, nodeCommment)
// PS.: new line separator: \n
$tree->comment("child1/sub-child1", "Testing comments");
// Test retrieving a node
// get(nodeName)
echo "<b>Value of child2:</b> ".$tree->get("child2")."<br />";
echo "<b>Value of child1/sub-child2:</b> ".$tree->get("child1/sub-child2")."<br />";
// Converting tree into a Human readable text
echo "<h3>Displaying tree before removes</h3>";
echo "<pre>\n";
echo $tree->toString(1);
echo "</pre>";
// Test removing a node (can be any node). Sub-childs will be delete too
// remove(nodeName)
$tree->remove("child2");
$tree->remove("child5");
$tree->comment("child4", "Comments anywhere! =P");
// Retrieving content
$tree_content = $tree->toString(1);
// Displaying Tree after Node remove
echo "<h3>Displaying tree after removes</h3>";
echo "<pre>\n";
echo $tree_content;
echo "</pre>";
// Testing error handler (brackets test)
// Uncoment the next 2 lines to test
//$nTree = new pTree;
//$nTree->fromString(substr($tree_content, 0, strlen($tree_content) - 4));
// Reverse way... building new tree from a given text
$tree2 = new pTree(";");
$tree2->fromString($tree_content);
echo "<h1>Tree 2 (Separator: \";\")</h1>";
// Look at separator usage
// Updating content
$tree2->add("child1;sub-child3", "Now with value!");
// Commenting
$tree2->comment("child1;sub-child3", "Now I can put some comment here\nWith new line included!!!");
// Displaying tree2 node value, showing separator usage
echo "<b>Value of child1;sub-child3:</b> ".$tree2->get("child1;sub-child3")."<br />";
// Updating content
$tree2->add("child1;sub-child3", "Now with value!");
// Displaying tree
echo "<pre>\n";
echo $tree2->toString(1);
echo "</pre>";
// Testing other methods
echo "<h1>Testing Other Tools (using Tree 2)</h1>";
// isRoot(nodeName) => check if the node is root or not
echo "<b>child4</b> ".(($tree2->isRoot("child4")) ? "is" : "isn't")." root<br />";
// hasChilds(nodeName) => checks if nodeName has childNodes or not
echo "<b>child3</b> ".(($tree2->hasChilds("child3")) ? "has" : "hasn't")." childs<br />";
// hasValue(nodeName) => check if nodeName has a value or not
echo "<b>child1;sub-child1</b> ".(($tree2->hasValue("child1;sub-child1")) ? "has" : "hasn't")." value<br />";
// Displaying tree
echo "<h3>Displaying Object Tree 2 - Showing real pTree Structure</h3>";
echo "<pre>\n";
print_r($tree2);
echo "</pre>";
?>
|