
Added by thebman220 on November-23-2008, 5:25 pm
<?php
class Boolean_Tester {
private $expression;
private $vars;
private $num_vars;
private $temp_array;
function __construct($e) {
$e=str_replace(array(" AND "," OR "),array("&&","||"),$e);
$this->expression=$e;
preg_match_all('/\$?\w[\w\d]*/',$e,$matches);
$this->vars=array();
foreach ($matches[0] as $match) {
if (!preg_match("/^(true|false)$/i",$match)) {
$this->vars[$match]=true;
}
}
$pos=0;
foreach ($this->vars as $var => $nothing) {
$this->vars[$pos++]=$var;
unset($this->vars[$var]);
}
$this->num_vars=count($this->vars);
}
function test() {
$argv=func_get_args();
$count=count($argv);
if ($count>0&&is_array($argv[0])) {
$argv=$argv[0];
$count=count($argv);
}
if ($count!=$this->num_vars) {
trigger_error("Boolean test arguments must match",E_USER_WARNING);
return;
}
foreach ($argv as &$arg) {
$arg=$arg=="T"?"true":($arg=="F"?"false":(int)$arg);
}
$e=str_replace($this->vars,$argv,$this->expression);
return eval("return $e;");
}
function multi_test() {
$argv=func_get_args();
$count=count($argv);
if ($count==0) {
$this->temp_array=array();
} elseif (is_array($argv[0])) {
if (isset($argv[1])) {
$argv[0][]=$argv[1];
$argv=$argv[0];
}
$count=count($argv);
}
if ($count<$this->num_vars) {
$this->multi_test($argv,"T");
$this->multi_test($argv,"F");
} else {
$this->temp_array[implode("|",$argv)]=$this->test($argv)?"T":"F";
}
if ($count==0) {
$strs=array();
$sep="+";
$header="|";
$sps=array();
foreach ($this->vars as $index => $var) {
$len=strlen($var);
$header.=" $var |";
$sep.="-".str_repeat("-",$len)."-+";
$sps[$index]=str_repeat(" ",$len-1);
}
$header.=" Result |";
$sep.="--------+";
foreach ($this->temp_array as $index => $temp) {
$str="|";
foreach (explode("|",$index) as $index => $bool) {
$str.=" $bool{$sps[$index]} |";
}
$str.=" $temp |";
$strs[]=$str;
}
$str="Testing '{$this->expression}':\n$sep\n$header\n$sep\n".implode("\n",$strs)."\n$sep";
return array("display"=>$str,"values"=>$this->temp_array);;
}
}
}
?>
<?php
header("Content-type: text/plain");
$test=new Boolean_Tester('$a&&$b||$c');
echo "Tested against TRUE, FALSE, TRUE: ".$test->test(true,false,true)."\n";
$res=$test->multi_test();
echo $res["display"];
?>
Tested against TRUE, FALSE, TRUE: 1 Testing '$a&&$b||$c': +----+----+----+--------+ | $a | $b | $c | Result | +----+----+----+--------+ | T | T | T | T | | T | T | F | T | | T | F | T | T | | T | F | F | F | | F | T | T | T | | F | T | F | F | | F | F | T | T | | F | F | F | F | +----+----+----+--------+
Added by thebman220 on May-2-2009, 4:40 am
Added by thebman220 on April-29-2009, 11:47 pm
Added by thebman220 on April-5-2009, 1:48 pm
Added by thebman220 on January-29-2009, 3:15 am
Added by thebman220 on January-29-2009, 3:04 am