<?php
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* This is the model class for table "formula_item".
*
* @property int $id
* @property int $formula_id
* @property string $condition
* @property string $outcome
*/
class FormulaItem extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'formula_item';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['formula_id'], 'integer'],
[['condition', 'outcome'], 'string', 'max' => 2000],
['condition', 'validateCondition'],
['outcome', 'validateOutcome'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'formula_id' => 'Formula ID',
'condition' => '条件表达式',
'outcome' => '结果表达式',
];
}
//验证条件表达式合法性
public function validateCondition($attribute,$params)
{
if(!$this->hasErrors())
{
$is_ok = self::conditionIsValid($this->$attribute);
if(!$is_ok){
$this->addError($attribute, "条件表达式有错误: {$this->$attribute}");
}
}
}
//验证结果表达式合法性
public function validateOutcome($attribute, $params)
{
if(!$this->hasErrors())
{
$is_ok = self::outcomeIsValid($this->$attribute);
if(!$is_ok){
$this->addError($attribute, "结果表达式有错误: {$this->$attribute}");
}
}
}
/**
* 判断条件表达式是否有效
*
* @param string $str
*
* @return boolean
*/
public static function conditionIsValid($str)
{
$is_ok = false;
try
{
if($str == ''){
return true;
}
ini_set('track_errors',true);
$php_errormsg = null;
//预设的公式变量
$default = array_keys(FormulaVar::getDefaultList());
//自定义的公式变量
$list = FormulaVar::find()->asArray()->all();
$keys = ArrayHelper::getColumn($list,'key');
//预设的公式变量+自定义的公式变量
$keys = array_merge($default,$keys);
$args = [];
foreach($keys as $k => $v){
$args[$v] = 1;
//字符串的vars变成args
$find = "vars['" . $v . "']";
$replace = '$args[' ."'" . $v ."']";
$str = str_replace($find,$replace,$str);
}
$result = @eval("return $str;");
//$result = @eval("return is_bool($str);");
//if($php_errormsg == null && $result){
if($php_errormsg == null){
$is_ok = true;
}
}
catch(\Error $e)
{
$is_ok = false;
}
return $is_ok;
}
/**
* 判断结果表达式是否有效
*
* @param string $str
*
* @return boolean
*/
public static function outcomeIsValid($str)
{
$is_ok = false;
try
{
ini_set('track_errors',true);
$php_errormsg = null;
//预设的公式变量
$default = array_keys(FormulaVar::getDefaultList());
//自定义的公式变量
$list = FormulaVar::find()->asArray()->all();
$keys = ArrayHelper::getColumn($list,'key');
//预设的公式变量+自定义的公式变量
$keys = array_merge($default,$keys);
$args = [];
foreach($keys as $k => $v){
$args[$v] = 1;
//字符串的vars变成args
$find = "vars['" . $v . "']";
$replace = '$args[' ."'" . $v ."']";
$str = str_replace($find,$replace,$str);
}
//$result = @eval("return $str;");
$result = @eval("return is_numeric($str);");
if($php_errormsg == null && $result){
$is_ok = true;
}
}
catch(\Error $e)
{
$is_ok = false;
}
return $is_ok;
}
}
<?php
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* This is the model class for table "formula_var_item".
*
* @property int $id
* @property int $formula_var_id
* @property string $condition
* @property string $formula
*/
class FormulaVarItem extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'formula_var_item';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['formula_var_id'], 'integer'],
[['condition', 'outcome'], 'string', 'max' => 2000],
['condition', 'validateCondition'],
['outcome', 'validateOutcome'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'formula_var_id' => 'formula_var_id',
'condition' => '条件表达式',
'outcome' => '结果表达式',
];
}
//验证条件表达式合法性
public function validateCondition($attribute,$params)
{
if(!$this->hasErrors())
{
$is_ok = self::conditionIsValid($this->$attribute);
if(!$is_ok){
$this->addError($attribute, "条件表达式有错误: {$this->$attribute}");
}
}
}
//验证结果表达式合法性
public function validateOutcome($attribute, $params)
{
if(!$this->hasErrors())
{
$is_ok = self::outcomeIsValid($this->$attribute);
if(!$is_ok){
$this->addError($attribute, "结果表达式有错误: {$this->$attribute}");
}
}
}
/**
* 判断条件表达式是否有效
*
* @param string $str
*
* @return boolean
*/
public static function conditionIsValid($str)
{
$is_ok = false;
try
{
if($str == ''){
return true;
}
ini_set('track_errors',true);
$php_errormsg = null;
//预设的公式变量
$default = array_keys(FormulaVar::getDefaultList());
//自定义的公式变量
$list = FormulaVar::find()->asArray()->all();
$keys = ArrayHelper::getColumn($list,'key');
//预设的公式变量+自定义的公式变量
$keys = array_merge($default,$keys);
$args = [];
foreach($keys as $k => $v){
$args[$v] = 1;
//字符串的vars变成args
$find = "vars['" . $v . "']";
$replace = '$args[' ."'" . $v ."']";
$str = str_replace($find,$replace,$str);
}
//$result = @eval("return $str;");
$result = @eval("return is_bool($str);");
//if($php_errormsg == null && $result){
if($php_errormsg == null){
$is_ok = true;
}
}
catch(\Error $e)
{
$is_ok = false;
}
return $is_ok;
}
/**
* 判断结果表达式是否有效
*
* @param string $str
*
* @return boolean
*/
public static function outcomeIsValid($str)
{
$is_ok = false;
try
{
ini_set('track_errors',true);
$php_errormsg = null;
//预设的公式变量
$default = array_keys(FormulaVar::getDefaultList());
//自定义的公式变量
$list = FormulaVar::find()->asArray()->all();
$keys = ArrayHelper::getColumn($list,'key');
//预设的公式变量+自定义的公式变量
$keys = array_merge($default,$keys);
$args = [];
foreach($keys as $k => $v){
$args[$v] = 1;
//字符串的vars变成args
$find = "vars['" . $v . "']";
$replace = '$args[' ."'" . $v ."']";
$str = str_replace($find,$replace,$str);
}
//$result = @eval("return $str;");
$result = @eval("return is_numeric($str);");
if($php_errormsg == null && $result){
$is_ok = true;
}
}
catch(\Error $e)
{
$is_ok = false;
}
return $is_ok;
}
}
|