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

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