| 1 |
<?php |
| 2 |
|
| 3 |
class SFConditionParser { |
| 4 |
|
| 5 |
private $insertedEntryBase; |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* @param InsertEntryBase $insertedEntryBase |
| 10 |
* @return mixed |
| 11 |
*/ |
| 12 |
public function __construct($insertedEntryBase) |
| 13 |
{ |
| 14 |
$this->insertedEntryBase = $insertedEntryBase; |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
public function IsTrue($conditionElements) |
| 19 |
{ |
| 20 |
$statementIndex=0; |
| 21 |
array_unshift($conditionElements,array("IsOpeningPar"=>'y',"IsClosingPar"=>'n',"Op"=>"root")); |
| 22 |
array_push($conditionElements,array("IsOpeningPar"=>'n',"IsClosingPar"=>'y',"Op"=>"root")); |
| 23 |
return $this->EvaluateStatementInsideParentheses($statementIndex,$conditionElements); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
private function EvaluateStatementInsideParentheses(&$statementIndex, $conditionElements) |
| 28 |
{ |
| 29 |
$latestCondition=null; |
| 30 |
$latestEvaluation=null; |
| 31 |
for($i=$statementIndex;$i<count($conditionElements);$i++) |
| 32 |
{ |
| 33 |
$childParenthesesWasEvaluated=false; |
| 34 |
$currentCondition=$conditionElements[$i]; |
| 35 |
if($i!=$statementIndex&&$currentCondition["IsOpeningPar"]=='y') |
| 36 |
{ |
| 37 |
$currentEvaluation = $this->EvaluateStatementInsideParentheses($i, $conditionElements); |
| 38 |
$childParenthesesWasEvaluated=true; |
| 39 |
} |
| 40 |
else{ |
| 41 |
if($currentCondition["Op"]=="root") |
| 42 |
continue; |
| 43 |
$currentEvaluation=$this->EvaluateCondition($currentCondition); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if($latestCondition!=null) |
| 48 |
{ |
| 49 |
if($latestCondition["Join"]=='and') |
| 50 |
$currentEvaluation=$latestEvaluation&&$currentEvaluation; |
| 51 |
else |
| 52 |
$currentEvaluation=$latestEvaluation||$currentEvaluation; |
| 53 |
} |
| 54 |
|
| 55 |
$latestCondition=$conditionElements[$i]; |
| 56 |
$latestEvaluation=$currentEvaluation; |
| 57 |
|
| 58 |
if($latestCondition["IsClosingPar"]=='y'&&$childParenthesesWasEvaluated==false) |
| 59 |
{ |
| 60 |
$statementIndex=$i; |
| 61 |
return $currentEvaluation; |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
return $latestEvaluation; |
| 67 |
} |
| 68 |
|
| 69 |
private function EvaluateCondition($currentCondition) |
| 70 |
{ |
| 71 |
if($currentCondition["SerializationType"]=="text") |
| 72 |
return $this->EvaluateText($currentCondition); |
| 73 |
if($currentCondition["SerializationType"]=="date") |
| 74 |
return $this->EvaluateDate($currentCondition); |
| 75 |
if($currentCondition["SerializationType"]=="list") |
| 76 |
return $this->EvaluateList($currentCondition); |
| 77 |
|
| 78 |
throw new Exception('Invalid serialization type '.$currentCondition["SerializationType"]); |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private function EvaluateText($currentCondition) |
| 83 |
{ |
| 84 |
$conditionValue=trim($currentCondition["Value"]); |
| 85 |
$operationType=$currentCondition["Op"]; |
| 86 |
$fieldValue=trim($this->insertedEntryBase->GetFieldValue($currentCondition["Field"],SFSerializationType::TEXT)); |
| 87 |
switch($operationType) |
| 88 |
{ |
| 89 |
case "eq": |
| 90 |
return strcasecmp($conditionValue,$fieldValue)==0; |
| 91 |
case "neq": |
| 92 |
return strcasecmp($conditionValue,$fieldValue)!=0; |
| 93 |
case "contains": |
| 94 |
return stripos ($fieldValue,$fieldValue)!==false; |
| 95 |
case "ncontains": |
| 96 |
return stripos ($fieldValue,$fieldValue)===false; |
| 97 |
case "gt": |
| 98 |
$numericCondition=intval($conditionValue); |
| 99 |
$numericFieldValue=intval($fieldValue); |
| 100 |
return $numericFieldValue>$numericCondition; |
| 101 |
case "get": |
| 102 |
$numericCondition=intval($conditionValue); |
| 103 |
$numericFieldValue=intval($fieldValue); |
| 104 |
return $numericFieldValue>=$numericCondition; |
| 105 |
case "lt": |
| 106 |
$numericCondition=intval($conditionValue); |
| 107 |
$numericFieldValue=intval($fieldValue); |
| 108 |
return $numericFieldValue<$numericCondition; |
| 109 |
case "let": |
| 110 |
$numericCondition=intval($conditionValue); |
| 111 |
$numericFieldValue=intval($fieldValue); |
| 112 |
return $numericFieldValue<=$numericCondition; |
| 113 |
default: |
| 114 |
throw new Exception("Invalid condition operation type"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private function EvaluateDate($currentCondition) |
| 119 |
{ |
| 120 |
$operationType=$currentCondition["Op"]; |
| 121 |
$fieldValue=$this->insertedEntryBase->GetFieldValue($currentCondition["Field"],SFSerializationType::DATE); |
| 122 |
|
| 123 |
$conditionValue=trim($currentCondition["Value"]); |
| 124 |
$splitDate=explode("-",$conditionValue); |
| 125 |
|
| 126 |
$conditionValue= mktime(null,null,null,intval($splitDate[1]),intval($splitDate[2]),intval($splitDate[0])); |
| 127 |
|
| 128 |
switch($operationType) |
| 129 |
{ |
| 130 |
case "eq": |
| 131 |
return $fieldValue==$conditionValue; |
| 132 |
case "neq": |
| 133 |
return $fieldValue!=$conditionValue; |
| 134 |
case "contains": |
| 135 |
return $fieldValue==$conditionValue; |
| 136 |
case "ncontains": |
| 137 |
return $fieldValue!=$conditionValue; |
| 138 |
case "gt": |
| 139 |
return $fieldValue>$conditionValue; |
| 140 |
case "get": |
| 141 |
return $fieldValue>=$conditionValue; |
| 142 |
case "lt": |
| 143 |
return $fieldValue<$conditionValue; |
| 144 |
case "let": |
| 145 |
return $fieldValue<=$conditionValue; |
| 146 |
default: |
| 147 |
throw new Exception("Invalid condition operation type"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
private function EvaluateList($currentCondition) |
| 152 |
{ |
| 153 |
$operationType=$currentCondition["Op"]; |
| 154 |
$fieldValues=$this->insertedEntryBase->GetFieldValue($currentCondition["Field"],SFSerializationType::ARRAYLIST); |
| 155 |
|
| 156 |
$conditionValues=$currentCondition["Value"]; |
| 157 |
|
| 158 |
|
| 159 |
switch($operationType) |
| 160 |
{ |
| 161 |
case "contains": |
| 162 |
foreach($fieldValues as $fieldValue) |
| 163 |
foreach($conditionValues as $conditionValue) |
| 164 |
if($fieldValue["label"]==$conditionValue) |
| 165 |
return true; |
| 166 |
return false; |
| 167 |
case "ncontains": |
| 168 |
foreach($fieldValues as $fieldValue) |
| 169 |
foreach($conditionValues as $conditionValue) |
| 170 |
if($fieldValue["label"]==$conditionValue) |
| 171 |
return false; |
| 172 |
return true; |
| 173 |
default: |
| 174 |
throw new Exception("Invalid condition operation type"); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
} |