<?php
/**
 * PHPFIT_Parser exception class
 * 
 * $Id$
 *
 * get all the parse warnings and errors
 *
 * @author Daniel Jahnke <Jahnke.Daniel@web.de>
 * @license PHP License
 * @package FIT
 * @subpackage Fixture
 */

/**
 * FIT ParserException
 * 
 * @version 0.2.1
 * @package FIT
 * @subpackage Fixture
 */
class PHPFIT_ParserException extends Exception
{
   
/**
    * error_level
    * @var array
    */
    
private static $error_level = array(
       
2047 => 'PHP-ALL',
       
1024 => 'PHP-USER-NOTICE',
       
512 => 'PHP-USER-WARNING',
       
256 => 'PHP-USER-ERROR',
       
128 => 'PHP-COMPILE-WARNING',
       
64 => 'PHP-COMPILE-ERROR',
       
32 => 'PHP-CORE-WARNING',
       
16 => 'PHP-CORE-ERROR',
       
10 => 'FIXTURE-WARNING',
       
=> 'PHP-NOTICE',
       
=> 'PHP-PARSE',
       
=> 'PHP-WARNING',
       
=> 'PRP-ERROR',
    );



   
/**
    * var for singleton function
    * @var object
    */
    
private static $instance;

   
/**
    * error and warning array for manual messages
    * @var array
    */
    
private static $msg = array();

   
/**
    * error and warning array for automatic thrown messages
    */
    
protected $parsExceptionArr;

   
/**
    * singleton function
    * if already exist an instance of this class, the function returns it.
    */
    
public static function singleton()
    {
        if (!isset(
self::$instance)) {
            
$c __CLASS__;
            
self::$instance = new $c;
        }

        return 
self::$instance;
    }

   
/**
    * add exception
    * catch the exceptions and write them in an array
    * @param code       error code
    * @param message    desctiption of error, warning
    * @param file       file
    * @param lineno     linenumber
    */
    
public function addException$code$message$file$lineno )
    {
        
$this->parsExceptionArr[] = array(
                            
'file'      => $file,
                            
'line'      => $lineno,
                            
'message'   => $message,
                            
'code'      => $code,
                                         );
    }

   
/**
    * add msg
    * to add manual an error
    * @param message    desctitpion of error, warning
    * @param code       error code
    * @param file       file
    * @param line       linenumber
    */
    
public static function addMsg$message$code$file __FILE__$line __LINE__ )
    {
        
$tmp = array(
                
'file'      => $file,
                
'line'      => $line,
                
'code'      => $code,
                
'message'   => $message,
                    );

        
array_pushself::$msg$tmp );
    }

   
/**
    * toString
    * to get the cachted errors, warnings, and manual added messages
    * if style = html, you get the messages as an html table
    * @param style      if null get messages as an simple array, or html get messages as html array
    */
    
public function __toString$style null)
    {
        
$print = array();
        
$i 0;
        if( !empty( 
$this->parsExceptionArr ) ) {
            foreach( 
$this->parsExceptionArr as $value ) {
                
$index $value['code'];
                
$print[$index][$i++] = $value;
            }
        }

        if( !empty( 
self::$msg ) ) {
            foreach( 
self::$msg as $value ) {
                
$index $value['code'];
                
$print[$index][$i++] = $value;
            }
        }

        if( 
$style == 'html' ) {
            if( 
count$print ) == ) {
                return 
0;
            }

            
$outputStyle 'style="padding:5px; background-color:blue; width: 100%; font-size:11px;"';

            
$htmlprint .= '<div style="border: 1px solid; margin: 5px; padding: 5px;">';
            
$htmlprint .= '<h3> TABLE PARSE WARNINGS </h3>';
            
$htmlprint .= '<table ' $outputStyle '><th align="left">message</th><th align="left">file</th><th align="left">line</th>';

            foreach( 
$print as $key => $value ) {
                
$htmlprint .= '<tr><td colspan="3"></td></tr><th colspan="3" align="left">' .  self::$error_level[$key] . '</th>';
                
$i 0;
                foreach( 
$value as $v ) {
                    
$htmlprint .= '<tr>';
                    
$htmlprint .= '<td><em>[' sprintf('%03d',$i++) . ']</em> ' $v['message'] . '</td>';
                    
$htmlprint .= '<td>' $v['file'] . '</td>';
                    
$htmlprint .= '<td>' $v['line'] . '</td>';
                    
$htmlprint .= '</tr>';
                }
            }
            
$htmlprint .= '</table>';
            
$htmlprint .= '</div>';

            echo 
$htmlprint;
            return 
true;
        }

        
print_r$print );
        return 
true;
    }

}


/**
 * exception error handler
 * function to catch all messages and throw it to ParserException class
 */
function exceptions_error_handler$code$message$file$lineno )
{
    
$parsException PHPFIT_ParserException::singleton();
    
$parsException->addException$code$message$file$lineno );
}

set_error_handler('exceptions_error_handler', ( E_ALL ) );

?>