HTML_Template_PHPLIB
[ class tree: HTML_Template_PHPLIB ] [ index: HTML_Template_PHPLIB ] [ all elements ]

Source for file Validator.php

Documentation is available at Validator.php

  1. <?php
  2.  
  3. /**
  4. * Class to validate templates (syntax checks)
  5. *
  6. @category HTML
  7. @package  HTML_Template_PHPLIB
  8. @author   Christian Weiske <cweiske@php.net>
  9. @license  http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  10. @link     http://pear.php.net/package/HTML_Template_PHPLIB
  11. */
  12. {
  13.     /**
  14.     * Validates a template file.
  15.     * You can pass either a file name, or the file content. One of the parameters
  16.     *  needs to be !== null.
  17.     *
  18.     * @param string $strFile    Template file name to check
  19.     * @param string $strContent Template content to check
  20.     *
  21.     * @return mixed Boolean true if no errors have been found, array of
  22.     *                 errors otherwise. An error is an array with keys
  23.     *                 - 'short' (short error code, string)
  24.     *                 - 'message' (readable message)
  25.     *                 - 'line'    (line number)
  26.     *                 - 'code'    (code that caused the error)
  27.     *                 false if no file and content is given
  28.     *
  29.     * @public
  30.     * @static
  31.     */
  32.     function validate($strFile = null$strContent = null)
  33.     {
  34.         if ($strFile !== null{
  35.             $strContent file_get_contents($strFile);
  36.         else if ($strContent === null{
  37.             return false;
  38.         }
  39.         
  40.         $arErrors HTML_Template_PHPLIB_Validator::checkBlockDefinitions($strContent);
  41.  
  42.         HTML_Template_PHPLIB_Validator::sortByLine($arErrors);
  43.  
  44.         return count($arErrors== 0 ? true : $arErrors;
  45.     }//function validate($strFile = null, $strContent = null)
  46.  
  47.  
  48.  
  49.     /**
  50.     * Check if all block definitions have a closing counterpart
  51.     * and if the block comments have all the required spaces
  52.     *
  53.     * @param string $strContent Template content
  54.     *
  55.     * @return array Array of errors/warnings. An error/warning is an array
  56.     *                 of several keys: message, line
  57.     *
  58.     * @protected
  59.     * @static
  60.     */
  61.     function checkBlockDefinitions($strContent)
  62.     {
  63.         $arLines explode(
  64.             "\n"str_replace(array("\r\n""\r")"\n"$strContent)
  65.         );
  66.         //Array of block definitions found.
  67.         // key is the block name, value is an array of line numbers
  68.         $arBlockOpen  = array();
  69.         $arBlockClose = array();
  70.         $arErrors     = array();
  71.         
  72.         $strRegex '/<!--(\s*)(BEGIN|END)(\s*)([a-zA-Z0-9_]*)(\s*)-->/';
  73.         foreach ($arLines as $nLine => $strLine{
  74.             if (preg_match($strRegex$strLine$arMatches)) {
  75.                 //code line numbers start with 1, not 0
  76.                 $nLine $nLine + 1;
  77.  
  78.                 $strType      $arMatches[2];
  79.                 $strBlockName $arMatches[4];
  80.                 $strArName    $strType == 'BEGIN' 'arBlockOpen' 'arBlockClose';
  81.                 if ($arMatches[1== ''{
  82.                     //space missing between <!-- and BEGIN|END
  83.                     $arErrors[= array(
  84.                         'short'   => 'MISSING_SPACE',
  85.                         'message' => 'Space missing between HTML comment opening marker and ' $strType,
  86.                         'line'    => $nLine,
  87.                         'code'    => $strLine
  88.                     );
  89.                 }
  90.                 if ($arMatches[3== ''{
  91.                     //space missing between BEGIN and block name
  92.                     $arErrors[= array(
  93.                         'short'   => 'MISSING_SPACE',
  94.                         'message' => 'Space missing between ' $strType ' and block name',
  95.                         'line'    => $nLine,
  96.                         'code'    => $strLine
  97.                     );                     
  98.                 }
  99.                 if ($arMatches[4== ''{
  100.                     //block name missing
  101.                     $arErrors[= array(
  102.                         'short'   => 'MISSING_BLOCK_NAME',
  103.                         'message' => 'Block name missing',
  104.                         'line'    => $nLine,
  105.                         'code'    => $strLine
  106.                     );
  107.                 else {
  108.                     ${$strArName}[$strBlockName][$nLine;
  109.                 }
  110.                 if ($arMatches[5== ''{
  111.                     //space missing between block name and -->
  112.                     $arErrors[= array(
  113.                         'short'   => 'MISSING_SPACE',
  114.                         'message' => 'Space missing between block name and HTML comment end marker',
  115.                         'line'    => $nLine,
  116.                         'code'    => $strLine
  117.                     );
  118.                 }
  119.             }
  120.         }
  121.         
  122.         
  123.         /**
  124.         * Check if all open blocks have a close counterpart
  125.         */
  126.         foreach ($arBlockOpen as $strBlockName => $arLines{
  127.             if (count($arLines> 1{
  128.                 $arErrors[= array(
  129.                     'short'   => 'DUPLICATE_BLOCK',
  130.                     'message' => 'Block "' $strBlockName '" is opened'
  131.                                . ' several times on lines ' implode(', '$arLines),
  132.                     'line'    => $arLines[0],
  133.                     'code'    => $strBlockName
  134.                 );
  135.             }
  136.             if (!isset($arBlockClose[$strBlockName])) {
  137.                 $arErrors[= array(
  138.                     'short'   => 'UNFINISHED_BLOCK',
  139.                     'message' => 'Block "' $strBlockName '" is not closed.',
  140.                     'line'    => $arLines[0],
  141.                     'code'    => $strBlockName
  142.                 );
  143.             }
  144.         }
  145.         foreach ($arBlockClose as $strBlockName => $arLines{
  146.             if (count($arLines> 1{
  147.                 $arErrors[= array(
  148.                     'short'   => 'DUPLICATE_BLOCK',
  149.                     'message' => 'Block "' $strBlockName '" is closed'
  150.                                . ' several times on lines ' implode(', '$arLines),
  151.                     'line'    => $arLines[0],
  152.                     'code'    => $strBlockName
  153.                 );
  154.             }
  155.             if (!isset($arBlockOpen[$strBlockName])) {
  156.                 $arErrors[= array(
  157.                     'short'   => 'UNFINISHED_BLOCK',
  158.                     'message' => 'Block "' $strBlockName '" is closed but not opened.',
  159.                     'line'    => $arLines[0],
  160.                     'code'    => $strBlockName
  161.                 );
  162.             }
  163.         }
  164.         
  165.         //TODO: Check proper nesting
  166.         
  167.         return $arErrors;
  168.     }//function checkBlockDefinitions($strContent)
  169.     
  170.     
  171.     
  172.     /**
  173.     * Sorts the given error array by line numbers
  174.     *
  175.     * @param array &$arErrors Error array
  176.     *
  177.     * @return void 
  178.     */
  179.     function sortByLine(&$arErrors)
  180.     {
  181.         if (!is_array($arErrors)) {
  182.             return;
  183.         }
  184.         usort($arErrorsarray(__CLASS__'intcmpLine'));
  185.     }//function sortByLine(&$arErrors)
  186.     
  187.     
  188.     
  189.     /**
  190.     * Compares the two error arrays by line number
  191.     *
  192.     * @param array $arA Error array one
  193.     * @param array $arB Error array two
  194.     *
  195.     * @return integer -1, 0 or 1 if $arA is smaller, equal or bigger than $arB
  196.     */
  197.     function intcmpLine($arA$arB)
  198.     {
  199.         return $arA['line'$arB['line'];
  200.     }//function intcmpLine($arA, $arB)
  201.  
  202. }//class HTML_Template_PHPLIB_Validator
  203.  
  204. ?>

Documentation generated on Mon, 11 Mar 2019 15:10:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.