/**
  * Validates a template file.
  * You can pass either a file name, or the file content. One of the parameters
  *  needs to be !== null.
  *
  * @param string $strFile    Template file name to check
  * @param string $strContent Template content to check
  *
  * @return mixed Boolean true if no errors have been found, array of
  *                errors otherwise. An error is an array with keys
  *                - 'short' (short error code, string)
  *                - 'message' (readable message)
  *                - 'line'    (line number)
  *                - 'code'    (code that caused the error)
  *                false if no file and content is given
  */
 public static function validate($strFile = null, $strContent = null)
 {
     $arLines = HTML_Template_PHPLIB_Helper::getLines($strFile, $strContent);
     if ($arLines === false) {
         return false;
     }
     $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($arLines);
     $arErrors = array_merge($arErrors, HTML_Template_PHPLIB_Validator::checkVariables($arLines));
     HTML_Template_PHPLIB_Validator::sortByLine($arErrors);
     return count($arErrors) == 0 ? true : $arErrors;
 }
Exemple #2
0
 /**
  * Validates the files given on the cmdline
  *
  * @param array $args Command line arguments (files)
  *
  * @return void
  */
 function doValidate($args)
 {
     if (count($args) == 0) {
         $this->dieHard("No template files to validate\n", 1);
     }
     include_once 'HTML/Template/PHPLIB/Validator.php';
     $nError = 0;
     foreach ($args as $file) {
         if (file_exists($file)) {
             $arErrors = HTML_Template_PHPLIB_Validator::validate($file);
             if ($arErrors === true) {
                 echo 'No errors found in ' . $file . "\n";
                 $nError = 0;
             } else {
                 if ($arErrors === false) {
                     echo 'Some unexpected error in ' . $file . "\n";
                     $nError = 3;
                 } else {
                     echo count($arErrors) . ' errors in ' . $file . "\n";
                     foreach ($arErrors as $arError) {
                         echo ' Line #' . $arError['line'] . ': ' . $arError['message'] . "\n";
                     }
                     $nError = 10;
                 }
             }
         } else {
             echo 'File does not exist: ' . $file . "\n";
             $nError = 4;
         }
     }
     $this->dieHard('', $nError);
 }
 public function testIntcmpLine()
 {
     $ar1 = array('line' => 1);
     $ar2 = array('line' => 2);
     $this->assertEquals(-1, HTML_Template_PHPLIB_Validator::intcmpLine($ar1, $ar2));
     $ar1 = array('line' => 2);
     $ar2 = array('line' => 1);
     $this->assertEquals(1, HTML_Template_PHPLIB_Validator::intcmpLine($ar1, $ar2));
     $ar1 = array('line' => 2);
     $ar2 = array('line' => 2);
     $this->assertEquals(0, HTML_Template_PHPLIB_Validator::intcmpLine($ar1, $ar2));
 }