[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/ -> VTConditionalExpression.inc (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10      class VTConditionalExpression{
  11  		public function __construct($expression){
  12              $parser = new VTConditionalParser($expression);
  13              $this->expTree = $parser->parse();
  14          }
  15  
  16  		public function evaluate($data){
  17              $this->env=$data;
  18              return $this->evalGate($this->expTree);
  19          }
  20          
  21  		private function evalGate($tree){
  22              if(in_array($tree[0], array("and", "or"))){
  23                  switch($tree[0]){
  24                      case "and":
  25                          return $this->evalGate($tree[1]) and $this->evalGate($tree[2]);
  26                      case "or":
  27                          return $this->evalGate($tree[1]) or $this->evalGate($tree[2]);
  28                  }
  29              }else{
  30                  return $this->evalCondition($tree);
  31              }
  32          }
  33          
  34  		private function evalCondition($tree){
  35              switch($tree[0]){
  36                  case "=":
  37                      return (int)$this->getVal($tree[1]) == (int)$this->getVal($tree[2]);
  38              }
  39          }
  40          
  41  		private function getVal($node){
  42              list($valueType, $value) = $node;
  43              switch($valueType){
  44                  case "sym":
  45                      return $this->env[$value];
  46                  case "num":
  47                      return $value;
  48              }
  49          }
  50      }
  51      
  52  class VTParseFailed extends Exception { }
  53      
  54  /**
  55   * This is a simple parser for conditional expressions used to trigger workflow actions.
  56   * 
  57   */
  58  class VTConditionalParser{
  59      
  60          
  61  	public function __construct($expr){
  62          $this->tokens = $this->getTokens($expr);
  63          $this->pos = 0;
  64      }
  65      
  66  	private function getTokens($expression){
  67          preg_match_all('/and|or|\\d+|=|\\w+|\\(|\\)/',$expression, $matches, PREG_SET_ORDER);
  68          $tokens=array();
  69          foreach($matches as $arr){
  70              $tokenVal = $arr[0];
  71              if(in_array($tokenVal, array("and", "or", "=", "(", ")"))){
  72                  $tokenType = "op";
  73              }else if(is_numeric($tokenVal)){
  74                  $tokenType = "num";
  75              }else{
  76                  $tokenType = "sym";
  77              }
  78              $tokens[]=array($tokenType, $tokenVal);
  79          }
  80          return $tokens;
  81      }
  82      
  83      
  84  	public function parse(){
  85          $op =  array(
  86              "and"=>array("op", "and"),
  87              "or"=>array("op", "or"),
  88              "="=>array("op", "="),
  89              "("=>array("op", "("),
  90              ")"=>array("op", ")"));
  91  
  92          if($this->peek()==$op['(']){
  93              $this->nextToken();
  94              $left = $this->parse();
  95              if($this->nextToken()!= $op[')']){
  96                  throw new VTParseFailed();
  97              }
  98          }else{
  99              $left = $this->cond();
 100          }
 101          if(sizeof($this->tokens)>$this->pos and in_array($this->peek(), array($op["and"], $op["or"]))){
 102              $nt = $this->nextToken();
 103              return array($nt[1], $left, $this->parse());
 104          }else{
 105              return $left;
 106          }
 107      }
 108      
 109  	private function cond(){
 110          $left = $this->nextToken();
 111          $operator = $this->nextToken();
 112          $right = $this->nextToken();
 113          return array($operator[1], $left, $right);
 114      }
 115      
 116  	private function peek(){
 117          return $this->tokens[$this->pos];
 118      }
 119      
 120  	private function nextToken(){
 121          $this->pos+=1;
 122          return $this->tokens[$this->pos - 1];
 123      }
 124  }
 125      
 126  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1