Convert string into executable expression PHP?
I am working on Expression Builder, where user can add many rules.
I am trying to convert rules into the one expression, but PHP always consider as string and always return true
.
Is there any way to make that expression executable?
public function translate($record = null){
$rule = $this->rule;
$conditions = [];
if( count($rule->rules) ){
$last_key = key( array_slice( $rule->rules, -1, 1, TRUE ) );
foreach ($rule->rules as $key => $value) {
$conditions[] = '"'.$record->{$value->field} .'" ' . $this->operator($value->operator) .' "'. $value->value.'"';
}
}
$condition = implode(' '.$rule->condition.' ', $conditions);
return $condition;// result : "Full Packaged Product" == "Full Packaged Product" AND "No" != "Y"
}
// using here
foreach( $records as $ind => $record ){
foreach( $rules as $rule){
// $condition = $rule->translate(collect($record));
$condition = $rule->translate($record);
if($condition){
dump('Pass', $condition);
}else{
dump('Fail', $condition);
}
}
}
Also I tried to use eval()
PHP function, but no luck!
Thanks,
Kaleem
from Laravel Questions and Answers https://laravelquestions.com/php/convert-string-into-executable-expression-php/
via Lzo Media
No comments:
Post a Comment