/classes/plugins/builtin/blocks/if.php

https://github.com/kazy111/g_feeder · PHP · 180 lines · 139 code · 14 blank · 27 comment · 19 complexity · c583d264376b260c1f8228936537bb66 MD5 · raw file

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