/**
  * Validates a single argument.
  * Validates a single argument. Uses a specified ValidatorRule object for validation.
  * @param ref mixed $argument The argument to be validated.
  * @param ref object ValidatorRule $rule The rule to use for validation.
  * @param optional boolean $isFatal If TRUE, upon validation failure, a fatal error
  * will be thrown. Default: FALSE.
  * @access public
  * @return boolean If validation is successful, returns TRUE. If validation
  * fails and $isFatal is FALSE, returns FALSE. If $isFatal is TRUE, then
  * if validation fails, the script would halt and nothing would ever be
  * returned.
  * @static
  **/
 static function validate($argument, ValidatorRuleInterface $rule, $isFatal = true)
 {
     if (defined('DISABLE_VALIDATION') && DISABLE_VALIDATION) {
         return true;
     }
     // now try to validate the argument
     // if argument is invalid
     if (!$rule->check($argument)) {
         // then throw an error
         $description = "";
         $description .= "Argument validation failed in ";
         // get information abour the function that called the ArgumentValidator
         $debugBacktrace = debug_backtrace();
         $class = $debugBacktrace[1]["class"];
         $function = $debugBacktrace[1]["function"];
         if (isset($debugBacktrace[1]["args"])) {
             $arguments = $debugBacktrace[1]["args"];
         } else {
             $arguments = array();
         }
         $description .= "<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;" . $class . "::" . $function;
         // print the arguments using the ArgumentRenderer
         $description .= "(";
         $description .= ArgumentRenderer::renderManyArguments($arguments, false, false);
         $description .= ");";
         $description .= "<br/><br/>Argument '";
         $description .= ArgumentRenderer::renderOneArgument($argument, false, false);
         $description .= "' could not be validated using a/an ";
         $description .= get_class($rule);
         $description .= ".";
         // now create the error
         throw new InvalidArgumentException($description);
     }
     // validation is successful
     return true;
 }
예제 #2
0
/**
* Prints a single error.
* @param object Error The Error object to be printed.
* @param boolean $isDetailed If TRUE, will print the error with details.
* @access private
*/
function printError($message = "")
{
    print "\n<br />\n<b>ERRORS:</b><br /><br />\n";
    print "<ul>";
    $description = nl2br($message);
    print "<li>\n";
    print "<b>Description</b>: " . $description . "<br /><br />\n";
    /* get the call sequence information */
    $traceArray = debug_backtrace();
    if (is_array($traceArray)) {
        foreach ($traceArray as $trace) {
            /* each $traceArray element represents a step in the call hiearchy. Print them from bottom up. */
            $file = basename($trace['file']);
            $line = $trace['line'];
            $function = $trace['function'];
            $class = $trace['class'];
            $type = $trace['type'];
            $args = ArgumentRenderer::renderManyArguments($trace['args'], false, false);
            print "in <b>{$file}:{$line}</b> {$class}{$type}{$function}({$args})<br />\n";
        }
    }
    print "<br />";
    print "</ul>";
}
 /**
  * Renders many arguments.
  * Renders many arguments by printing their types and values and comma delimiting them.
  * In 'detailed' mode, includes some additional information (i.e., for arrays, 
  * prints all elements of the array; for objects, prints the object structure).
  * @param array $arguments The arguments to render.
  * @param boolean $isDetailed If TRUE, will print additional details.
  * @param boolean $shouldPrint If TRUE, will print on screen; If FALSE, will not
  * print, but just return the result as a string.
  * @return string The output of the method. This will be output to the browser
  * if $shouldPrint is set to TRUE. Returns FALSE, if something goes wrong.
  * @access public
  **/
 function renderManyArguments($arguments, $isDetailed = false, $shouldPrint = false)
 {
     // see if $arguments is an array
     if (!is_array($arguments)) {
         return false;
     }
     // make sure $arguments is not empty
     if (count($arguments) == 0) {
         return false;
     }
     // render each element of $arguments
     $resultArray = array();
     foreach (array_keys($arguments) as $i => $key) {
         $resultArray[] = ArgumentRenderer::renderOneArgument($arguments[$key], $isDetailed, false);
     }
     $glue = $isDetailed ? ",\n" : ", ";
     $result = implode($glue, $resultArray);
     // print if necessary
     if ($shouldPrint) {
         echo $result;
     }
     return $result;
 }
 /**
  *    Renders multiple arguments, in detailed mode.
  */
 function test_Multiple_Arguments()
 {
     $arguments = array();
     $arguments[] = true;
     $arguments[] = 15;
     $arguments[] = 15.25;
     $arguments[] = "Muhaha";
     $arguments[] = array("one", "two");
     $arguments[] = new ArgumentRenderer();
     $arguments[] = mysql_connect("devo.middlebury.edu", "test", "test");
     // detailed mode
     $result = ArgumentRenderer::renderManyArguments($arguments, true, true);
     // simple mode
     $result = ArgumentRenderer::renderManyArguments($arguments, false, true);
     $this->assertTrue($result);
 }
 /**
  * Prints a debug_backtrace() array in a pretty plain text way...
  * @param optional array $trace The array. If null, a current backtrace is used.
  * @param optional boolean $return If true will return the HTML instead of printing it.
  * @access public
  * @return void
  */
 public static function printPlainTextDebugBacktrace($trace = null, $return = false)
 {
     if (is_array($trace)) {
         $traceArray = $trace;
     } else {
         $traceArray = debug_backtrace();
     }
     if ($return) {
         ob_start();
     }
     $filenameSize = 5;
     if (is_array($traceArray)) {
         foreach ($traceArray as $trace) {
             if (isset($trace['file'])) {
                 $filenameSize = max($filenameSize, strlen(basename($trace['file'])));
             }
         }
     }
     $filenameSize = $filenameSize + 2;
     print "\n* # ";
     print "\tFile";
     for ($j = 4; $j < $filenameSize; $j++) {
         print " ";
     }
     print "Line";
     print "\tCall ";
     print "\n*-----------------------------------------------------------------------------";
     if (is_array($traceArray)) {
         foreach ($traceArray as $i => $trace) {
             /* each $traceArray element represents a step in the call hiearchy. Print them from bottom up. */
             $file = isset($trace['file']) ? basename($trace['file']) : '';
             $line = isset($trace['line']) ? $trace['line'] : '';
             $function = isset($trace['function']) ? $trace['function'] : '';
             $class = isset($trace['class']) ? $trace['class'] : '';
             $type = isset($trace['type']) ? $trace['type'] : '';
             $args = ArgumentRenderer::renderManyArguments($trace['args'], false, false);
             print "\n* {$i}";
             print "\t" . $file;
             for ($j = strlen($file); $j < $filenameSize; $j++) {
                 print " ";
             }
             print "" . $line;
             print "\t";
             if ($class || $type || $function || $args) {
                 print $class . $type . $function . "(" . $args . ");";
             }
         }
     }
     if ($return) {
         return ob_get_clean();
     }
 }