Source for file Validator.php
Documentation is available at Validator.php
* Class to validate templates (syntax checks)
* @package HTML_Template_PHPLIB
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear.php.net/package/HTML_Template_PHPLIB
* Validates a template file.
* You can pass either a file name, or the file content. One of the parameters
* @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)
* - 'code' (code that caused the error)
* false if no file and content is given
function validate($strFile = null , $strContent = null )
} else if ($strContent === null ) {
return count($arErrors) == 0 ? true : $arErrors;
}//function validate($strFile = null, $strContent = null)
* Check if all block definitions have a closing counterpart
* and if the block comments have all the required spaces
* @param string $strContent Template content
* @return array Array of errors/warnings. An error/warning is an array
* of several keys: message, line
"\n", str_replace(array ("\r\n", "\r"), "\n", $strContent)
//Array of block definitions found.
// key is the block name, value is an array of line numbers
$strRegex = '/<!--(\s*)(BEGIN|END)(\s*)([a-zA-Z0-9_]*)(\s*)-->/';
foreach ($arLines as $nLine => $strLine) {
if (preg_match($strRegex, $strLine, $arMatches)) {
//code line numbers start with 1, not 0
$strType = $arMatches[2 ];
$strBlockName = $arMatches[4 ];
$strArName = $strType == 'BEGIN' ? 'arBlockOpen' : 'arBlockClose';
if ($arMatches[1 ] == '') {
//space missing between <!-- and BEGIN|END
'short' => 'MISSING_SPACE',
'message' => 'Space missing between HTML comment opening marker and ' . $strType,
if ($arMatches[3 ] == '') {
//space missing between BEGIN and block name
'short' => 'MISSING_SPACE',
'message' => 'Space missing between ' . $strType . ' and block name',
if ($arMatches[4 ] == '') {
'short' => 'MISSING_BLOCK_NAME',
'message' => 'Block name missing',
$ {$strArName}[$strBlockName][] = $nLine;
if ($arMatches[5 ] == '') {
//space missing between block name and -->
'short' => 'MISSING_SPACE',
'message' => 'Space missing between block name and HTML comment end marker',
* Check if all open blocks have a close counterpart
foreach ($arBlockOpen as $strBlockName => $arLines) {
if (count($arLines) > 1 ) {
'short' => 'DUPLICATE_BLOCK',
'message' => 'Block "' . $strBlockName . '" is opened'
. ' several times on lines ' . implode(', ', $arLines),
if (!isset ($arBlockClose[$strBlockName])) {
'short' => 'UNFINISHED_BLOCK',
'message' => 'Block "' . $strBlockName . '" is not closed.',
foreach ($arBlockClose as $strBlockName => $arLines) {
if (count($arLines) > 1 ) {
'short' => 'DUPLICATE_BLOCK',
'message' => 'Block "' . $strBlockName . '" is closed'
. ' several times on lines ' . implode(', ', $arLines),
if (!isset ($arBlockOpen[$strBlockName])) {
'short' => 'UNFINISHED_BLOCK',
'message' => 'Block "' . $strBlockName . '" is closed but not opened.',
//TODO: Check proper nesting
}//function checkBlockDefinitions($strContent)
* Sorts the given error array by line numbers
* @param array &$arErrors Error array
usort($arErrors, array (__CLASS__ , 'intcmpLine'));
}//function sortByLine(&$arErrors)
* Compares the two error arrays by line number
* @param array $arA Error array one
* @param array $arB Error array two
* @return integer -1, 0 or 1 if $arA is smaller, equal or bigger than $arB
return $arA['line'] - $arB['line'];
}//function intcmpLine($arA, $arB)
}//class HTML_Template_PHPLIB_Validator
Documentation generated on Mon, 11 Mar 2019 15:10:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|