It is just one of the files. I have also tried to write some tests using PHPUnit. Please give me some suggestions to improve my coding-writing skills.
The below is the test file for the above file:
<?php
class TestParseXML extends PHPUnit_Framework_TestCase
{
public function testSetup()
{
$objectParseXML = new ParseXML("test.txt");
return $objectParseXML;
}
/**
* @depends testSetup
*/
public function testParseObject($objectParseXML)
{
$objectParseXML->current_line = "<Article>";
return $objectParseXML->current_line;
}
/**
* @depends testSetup
* @depends testParseObject
*/
public function testtagNameOn($objectParseXML, $currentLine){
$this->assertEmpty($objectParseXML->tree);
$objectParseXML->scanCharacter('<', 0);
$this->assertTrue($objectParseXML->isTagName);
}
/**
* @depends testSetup
* @depends testParseObject
*/
public function testTagContentOn($objectParseXML){
$objectParseXML->isTagName = True;
$objectParseXML->scanCharacter('>', 3);
$this->assertTrue($objectParseXML->isTagContent);
$this->assertNotTrue($objectParseXML->isTagName);
}
}
?>
Code Files:
<?php
require('XML.php');
class OpenXML extends XML
{
var $fileHandler;
public function openXMLFile($filename, $mode='r'){
$this->fileHandler = @fopen($filename, $mode);
}
public function getHandler()
{
return $this->fileHandler;
}
public function getHandlerType(){
return get_resource_type($this->fileHandler);
}
}
?>
<?php
require('ReadXML.php');
class ParseXML extends ReadXML
{
public $handle;
public $current_line;
public $isTagName = False;
public $isTagContent = False;
public $startParse = False;
public $tagName;
public $tagContent;
public $tree = array();
//Call appropriate function depend on $isTagName and $isTagContent
public function scanCharacter($char, $index)
{
if(strcmp($char, '<')==0)
{
if($this->isTagContent){
$this->isTagContent = False;
}
if($index+1 < $this->getLen())
{
$this->isTagName = $this->tagNameOn($this->current_line[$index+1]);
}
}
elseif($this->isTagName and (strcmp($char, '>')==0))
{
$this->isTagName = $this->tagNameOff();
$this->isTagContent = $this->tagContentOn();
}
elseif((strcmp($char, '/')==0)){
$this->isTagName = False;
$this->isTagContent = False;
}
elseif($this->isTagName and !strcmp($char, ' '))
{
$this->isTagName = $this->tagNameOff();
}
elseif($this->isTagName)
{
$this->gatherTagName($char);
}
elseif($this->startParse and $this->isTagContent)
{
$this->gatherTagContent($char);
}
}
public function tagNameOn($nextChar)
{
if(!strcmp($nextChar, '/')==0)
{
$this->tagName = "";
return True;
}
// print_r($this->tagName);
// print_r("\n");
// print_r($this->tagContent);
$this->isTagContent = False;
return False;
}
public function tagNameOff()
{
if(!($this->startParse) and strcmp($this->tagName, 'Document') == 0){
$this->startParse = True;
}
return False;
}
public function getLen(){
return strlen($this->current_line);
}
public function tagContentOn()
{
return True;
}
public function gatherTagName($char){
$this->tagName.= $char;
}
public function gatherTagContent($char){
$this->tagContent.= $char;
}
public function getTag()
{
$this->tagContent = trim($this->tagContent);
if($this->tagName and $this->tagContent){
if(!$this->isTagContent){
array_push($this->tree, array($this->tagName, $this->tagContent));
$this->tagContent = "";
$this->tagName = "";
}
}
}
public function getTagContent(){
$listNames = array();
forEach($this->tree as $value){
array_push($listNames, $value[1]);
}
return $listNames;
}
public function getTagNames(){
$listNames = array();
forEach($this->tree as $value){
array_push($listNames, $value[0]);
}
return $listNames;
}
public function readLine()
{
$this->handle = $this->getHandle();
$this->current_line = fgets($this->handle);
while($this->current_line)
{
for ($i = 0; $i < $this->getLen(); $i++)
{
$char = $this->current_line[$i];
$this->scanCharacter($char, $i);
}
$this->getTag();
}
return $this->tree;
}
public function goOverLine(){
}
}
$a = new ParseXML('../IMQ+AZIBPrototyp.xml');
$a->openXMLFile($a->getFileName());
$a->acquireHandler();
$a->readLine();
print_r($a->getTagContent());
?>
ReadXML.php
<?php
require('OpenXML.php');
class ReadXML extends OpenXML
{
var $content;
var $line;
public function acquireHandler()
{
$this->content = $this->getHandler();
}
public function getHandle()
{
return $this->content;
}
public function isReadLine()
{
return feof($this->content);
}
public function getLine()
{
if(!($this->isReadLine()))
{
$this->line = fgets($this->content);
return $this->line;
}
}
}
?>