Source for file if.php

Documentation is available at if.php

  1. <?php
  2.  
  3. /**
  4.  * Conditional block, the syntax is very similar to the php one, allowing () || && and
  5.  * other php operators. Additional operators and their equivalent php syntax are as follow :
  6.  *
  7.  * eq -> ==
  8.  * neq or ne -> !=
  9.  * gte or ge -> >=
  10.  * lte or le -> <=
  11.  * gt -> >
  12.  * lt -> <
  13.  * mod -> %
  14.  * not -> !
  15.  * X is [not] div by Y -> (X % Y) == 0
  16.  * X is [not] even [by Y] -> (X % 2) == 0 or ((X/Y) % 2) == 0
  17.  * X is [not] odd [by Y] -> (X % 2) != 0 or ((X/Y) % 2) != 0
  18.  *
  19.  * This software is provided 'as-is', without any express or implied warranty.
  20.  * In no event will the authors be held liable for any damages arising from the use of this software.
  21.  *
  22.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  23.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  24.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  25.  * @link       http://dwoo.org/
  26.  * @version    1.0.0
  27.  * @date       2008-10-23
  28.  * @package    Dwoo
  29.  */
  30. {
  31.     public function init(array $rest)
  32.     {
  33.     }
  34.  
  35.     public static function replaceKeywords(array $params, Dwoo_Compiler $compiler)
  36.     {
  37.         $p = array();
  38.  
  39.         reset($params);
  40.         while (list($k,$v) = each($params)) {
  41.             $v = (string) $v;
  42.             if(substr($v, 0, 1) === '"' || substr($v, 0, 1) === '\'') {
  43.                 $vmod = strtolower(substr($v, 1, -1));
  44.             } else {
  45.                 $vmod = strtolower($v);
  46.             }
  47.             switch($vmod) {
  48.  
  49.             case 'and':
  50.                 $p[] = '&&';
  51.                 break;
  52.             case 'or':
  53.                 $p[] = '||';
  54.                 break;
  55.             case '==':
  56.             case 'eq':
  57.                 $p[] = '==';
  58.                 break;
  59.             case '<>':
  60.             case '!=':
  61.             case 'ne':
  62.             case 'neq':
  63.                 $p[] = '!=';
  64.                 break;
  65.             case '>=':
  66.             case 'gte':
  67.             case 'ge':
  68.                 $p[] = '>=';
  69.                 break;
  70.             case '<=':
  71.             case 'lte':
  72.             case 'le':
  73.                 $p[] = '<=';
  74.                 break;
  75.             case '>':
  76.             case 'gt':
  77.                 $p[] = '>';
  78.                 break;
  79.             case '<':
  80.             case 'lt':
  81.                 $p[] = '<';
  82.                 break;
  83.             case '===':
  84.                 $p[] = '===';
  85.                 break;
  86.             case '!==':
  87.                 $p[] = '!==';
  88.                 break;
  89.             case 'is':
  90.                 if (isset($params[$k+1]) && strtolower(trim($params[$k+1], '"\'')) === 'not') {
  91.                     $negate = true;
  92.                     next($params);
  93.                 } else {
  94.                     $negate = false;
  95.                 }
  96.                 $ptr = 1+(int)$negate;
  97.                 if (!isset($params[$k+$ptr])) {
  98.                     $params[$k+$ptr] = '';
  99.                 } else {
  100.                     $params[$k+$ptr] = trim($params[$k+$ptr], '"\'');
  101.                 }
  102.                 switch($params[$k+$ptr]) {
  103.  
  104.                 case 'div':
  105.                     if (isset($params[$k+$ptr+1]) && strtolower(trim($params[$k+$ptr+1], '"\'')) === 'by') {
  106.                         $p[] = ' % '.$params[$k+$ptr+2].' '.($negate?'!':'=').'== 0';
  107.                         next($params);
  108.                         next($params);
  109.                         next($params);
  110.                     } else {
  111.                         throw new Dwoo_Compilation_Exception($compiler, 'If : Syntax error : syntax should be "if $a is [not] div by $b", found '.$params[$k-1].' is '.($negate?'not ':'').'div '.$params[$k+$ptr+1].' '.$params[$k+$ptr+2]);
  112.                     }
  113.                     break;
  114.                 case 'even':
  115.                     $a = array_pop($p);
  116.                     if (isset($params[$k+$ptr+1]) && strtolower(trim($params[$k+$ptr+1], '"\'')) === 'by') {
  117.                         $b = $params[$k+$ptr+2];
  118.                         $p[] = '('.$a .' / '.$b.') % 2 '.($negate?'!':'=').'== 0';
  119.                         next($params);
  120.                         next($params);
  121.                     } else {
  122.                         $p[] = $a.' % 2 '.($negate?'!':'=').'== 0';
  123.                     }
  124.                     next($params);
  125.                     break;
  126.                 case 'odd':
  127.                     $a = array_pop($p);
  128.                     if (isset($params[$k+$ptr+1]) && strtolower(trim($params[$k+$ptr+1], '"\'')) === 'by') {
  129.                         $b = $params[$k+$ptr+2];
  130.                         $p[] = '('.$a .' / '.$b.') % 2 '.($negate?'=':'!').'== 0';
  131.                         next($params);
  132.                         next($params);
  133.                     } else {
  134.                         $p[] = $a.' % 2 '.($negate?'=':'!').'== 0';
  135.                     }
  136.                     next($params);
  137.                     break;
  138.                 default:
  139.                     throw new Dwoo_Compilation_Exception($compiler, 'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found '.$params[$k-1].' is '.$params[$k+$ptr+1]);
  140.  
  141.                 }
  142.                 break;
  143.             case '%':
  144.             case 'mod':
  145.                 $p[] = '%';
  146.                 break;
  147.             case '!':
  148.             case 'not':
  149.                 $p[] = '!';
  150.                 break;
  151.             default:
  152.                 $p[] = $v;
  153.  
  154.             }
  155.         }
  156.  
  157.         return $p;
  158.     }
  159.  
  160.     public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
  161.     {
  162.         return '';
  163.     }
  164.  
  165.     public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
  166.     {
  167.         $params = $compiler->getCompiledParams($params);
  168.  
  169.         $pre = Dwoo_Compiler::PHP_OPEN.'if ('.implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
  170.  
  171.         $post = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
  172.  
  173.         if (isset($params['hasElse'])) {
  174.             $post .= $params['hasElse'];
  175.         }
  176.  
  177.         return $pre . $content . $post;
  178.     }
  179. }

Documentation generated on Sun, 07 Feb 2010 17:53:49 +0000 by phpDocumentor 1.4.0