[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/src/compiler/expression/ -> WactExpression.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  require_once 'limb/wact/src/compiler/expression/WactExpressionFilterParser.class.php';
  11  require_once 'limb/wact/src/compiler/expression/WactExpressionValueParser.class.php';
  12  
  13  /**
  14   * Represents a single Expression found in the template like "var|uppercase|trim:0,10"
  15   * Responsibly for parsing the expression and building a filter chain
  16   * for the expression (if expression contained filter syntax)
  17   * @package wact
  18   * @version $Id: WactExpression.class.php 5945 2007-06-06 08:31:43Z pachanga $
  19   */
  20  class WactExpression implements WactExpressionInterface
  21  {
  22    protected $parsed;
  23  
  24    protected $context;
  25  
  26    protected $original_expression;
  27  
  28    protected $expression;
  29  
  30    protected $filter_dictionary;
  31  
  32    function __construct($expression, $context_node, $filter_dictionary, $default_filter = 'raw')
  33    {
  34      $this->original_expression = $expression;
  35  
  36      $this->expression = $expression;
  37  
  38      $this->context = $context_node;
  39  
  40      $this->filter_dictionary = $filter_dictionary;
  41  
  42      $this->default_filter = $default_filter;
  43  
  44      $this->_parseExpression();
  45    }
  46  
  47    protected function _parseExpression()
  48    {
  49      $apply_default_filter = $this->_shouldApplyDefaultFilter();
  50  
  51      $this->_createParsedExpression();
  52  
  53      if ($apply_default_filter)
  54        $this->_applyDefaultFilter();
  55    }
  56  
  57    protected function _createParsedExpression()
  58    {
  59      $pos = strpos($this->expression, "|");
  60  
  61      if ($pos === FALSE)
  62        $this->parsed = $this->createValue($this->expression);
  63      else
  64      {
  65        $base_expression = trim(substr($this->expression, 0, $pos));
  66        $filters_expression = trim(substr($this->expression, $pos + 1));
  67        $this->parsed = $this->createFilterChain($filters_expression, $this->createValue($base_expression));
  68      }
  69    }
  70  
  71    protected function _shouldApplyDefaultFilter()
  72    {
  73      if ($this->default_filter == 'raw')
  74        return false;
  75  
  76      if (preg_match('/^(.*)\s*\|\s*raw$/is', $this->expression, $match))
  77      {
  78        $this->expression = $match[1];
  79        return false;
  80      }
  81      return true;
  82    }
  83  
  84    protected function _applyDefaultFilter()
  85    {
  86      $default_filter = $this->_createFilter($this->default_filter, $this->parsed);
  87  
  88      // Don't apply the default filter if the last filter in the

  89      // chain is already that filter.

  90      if (strcasecmp(get_class($this->parsed), get_class($default_filter)) == 0)
  91        return;
  92  
  93      $this->parsed = $default_filter;
  94    }
  95  
  96    /**

  97    * Parses an expression and returns an object representing the expression

  98    */
  99    function createValue($expression)
 100    {
 101      $Parser = new WactExpressionValueParser($this->context);
 102      return $Parser->parse($expression);
 103    }
 104  
 105    /**

 106    * Parses an expression, building a chain of filters for it

 107    */
 108    function createFilterChain($expression, $base)
 109    {
 110      $filter_parser = new WactExpressionFilterParser($this->context);
 111      $filters_specs = $filter_parser->parse($expression);
 112  
 113      foreach($filters_specs as $filter_spec)
 114      {
 115        $filter_name = $filter_spec['name'];
 116        $filter_info = $this->filter_dictionary->getFilterInfo($filter_name);
 117  
 118        if (!is_object($filter_info))
 119          $this->context->raiseCompilerError('Unknown filter', array('filter' => $filter_name));
 120  
 121        $base = $this->_createFilter($filter_name, $base, $filter_spec['params']);
 122      }
 123  
 124      return $base;
 125    }
 126  
 127    protected function _createFilter($name, $base, $args = array())
 128    {
 129      $filter_info = $this->filter_dictionary->getFilterInfo($name);
 130  
 131      if (!is_object($filter_info))
 132        $this->context->raiseCompilerError('Unknown filter', array('filter' => $name));
 133  
 134      $filter_info->load();
 135  
 136      $filter_class = $filter_info->FilterClass;
 137      $filter = new $filter_class($this->context->getLocationInTemplate());
 138  
 139      if (is_array($args) && count($args))
 140      {
 141        $numArgs = count($args);
 142  
 143        if ($numArgs < $filter_info->MinParameterCount)
 144          $this->context->raiseCompilerError('Invalid or missing filter parameter', array('filter' => $name));
 145  
 146        if ($numArgs > $filter_info->MaxParameterCount)
 147          $this->context->raiseCompilerError('Too many parameters for filter', array('filter' => $name));
 148  
 149        foreach ($args as $value_expr)
 150        {
 151          $parameter_expression = $this->createValue($value_expr);
 152          $filter->registerParameter($parameter_expression);
 153        }
 154      }
 155  
 156      $filter->registerBase($base);
 157  
 158      return $filter;
 159    }
 160  
 161    function isConstant()
 162    {
 163      return $this->parsed->isConstant();
 164    }
 165  
 166    function getValue()
 167    {
 168      return $this->parsed->getValue();
 169    }
 170  
 171    function generatePreStatement($code_writer)
 172    {
 173      $this->parsed->generatePreStatement($code_writer);
 174    }
 175  
 176    function generateExpression($code_writer)
 177    {
 178      $this->parsed->generateExpression($code_writer);
 179    }
 180  
 181    function generatePostStatement($code_writer)
 182    {
 183      $this->parsed->generatePostStatement($code_writer);
 184    }
 185  
 186    function prepare()
 187    {
 188      return $this->parsed->prepare();
 189    }
 190  
 191    function getFilterDictionary()
 192    {
 193      return $this->filter_dictionary;
 194    }
 195  }
 196  ?>


Generated: Tue Dec 2 03:54:09 2008 Cross-referenced by PHPXref 0.7