示例#1
0
 /**
  * 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;
 }
    /**
     * Test if wrongly defined variables are detected
     */
    public function testCheckVariables()
    {
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(array());
        $this->assertEquals(array(), $arErrors);
        $cont = <<<EOT
{FULL}
{PARTIAL1
PARTIAL2}
<h1>{FULL2}{FULL3}</h2>
<p><strong>{PARTIAL3</strong>}</p>
<p>{<strong>PARTIAL3}</strong></p>
EOT;
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(HTML_Template_PHPLIB_Helper::getLines(null, $cont));
        $arErrors = self::stripMessages($arErrors);
        $this->assertEquals(array(array('short' => 'CLOSING_BRACE_MISSING', 'line' => 2, 'code' => '{PARTIAL1'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 3, 'code' => 'PARTIAL2}'), array('short' => 'CLOSING_BRACE_MISSING', 'line' => 5, 'code' => '{PARTIAL3<'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 6, 'code' => '>PARTIAL3}')), $arErrors);
    }