- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在创建一个验证器,它可以通过允许开发人员在条件规则中使用条件语句来处理复杂的条件。
以一组规则为例:
...
"element_name":{
"required": "conditional",
"conditions" : {
"requirements" : "(4 < 5)",
"requirements" : "('something' == 'something_else')"
}
}
...
然后 PHP 将做的是遍历那些 requirements
并将它们评估为代码以返回一个 bool 值,该 bool 值将确定该元素是否是必需的。
使用eval()
函数的问题非常明显。所以我问,考虑到条件语句是唯一允许的,有没有比以下更安全的方法:
$result = eval(element_name->requirements[0]);
谢谢你们。
----更新-----
谢谢 Mike 和大家的想法,我希望我能把你们都标记为答案,因为老实说,我最终使用了每个人的一点点想法。更多迈克的,所以他明白了。
所以,这可能是将来要研究的东西,因为它是一种非常有吸引力的有条件地验证字段的方法。我的目标是创建一种直观的方式来解决这个问题。我喜欢在 json 配置文件中简单地加入条件语句的想法。当然,这会涉及一些严重的安全风险或 super 复杂的解析引擎,所以我不再要求开发人员学习我们的条件语言方法,但正如您将看到的,我保持它与原始方法非常相似。我认为拥有简单的 API 很重要,否则您将阻碍在您的平台上进行开发。检查一下:
"element_name":{
"required": "conditional",
"conditions" : {
"<" : ['4', '5'],
"==" : [':element_id', ':other_element'], // will grab the values of those elements and compare
"exp" : [['something', '==', 'something_else'], 'OR', [':other_element', '!=', '0']]
}
}
最佳答案
为了扩展 dethtron5000 的答案,我想到的一种方法是让开发人员将他的条件分解成更多的多维条件事物,并使用递归函数对其进行循环。在每个级别,您都会有一个“运算符”,它可以是“AND”或“OR”(至少我希望这被称为“运算符”,如果不能随意更改的话)。
在您的示例中,您有:(32 < 40 AND 10 > 5 OR 20 == 10)
(看起来你正在对条件进行 json_encoding,所以我从以下 PHP 数组开始,然后从那里向后工作。我假设你可以 json_decode
你的开发人员提供给你的东西来获得有效的 PHP 数组) .上面的示例将表示为以下 PHP 数组:
$arr = array(
'required' => 'conditional',
'conditions' => array(
'requirements' => array(
'operator' => 'OR', // this makes it so that if any conditions at this level are true, it returns true
0 => array(
'operator' => 'AND', // this makes it so that all the conditions at this sub-level need to be satisfied to return true
array(
'conditional1' => 32,
'conditional2' => 40,
'operation' => 'lessthan',
),
array(
'conditional1' => 10,
'conditional2' => 5,
'operation' => 'greaterthan',
),
),
1 => array(
// Since there is only one condition here, it is not necessary to specify "AND" or "OR"
array(
'conditional1' => 20,
'conditional2' => 10,
'operation' => 'equals',
),
),
),
),
);
然后您可以使用这样的递归函数遍历条件:
function check_req(Array $reqs) {
$operator = (isset($reqs['operator'])) ? $reqs['operator'] : 'AND';
unset($reqs['operator']);
foreach ($reqs as $req) {
if (isset($req['operation'])) {
switch ($req['operation']) {
case 'lessthan':
$valids[] = $req['conditional1'] < $req['conditional2'];
break;
case 'greaterthan':
$valids[] = $req['conditional1'] > $req['conditional2'];
break;
case 'equals':
$valids[] = $req['conditional1'] == $req['conditional2'];
break;
}
}
else {
$valids[] = check_req($req);
}
}
if ($operator == 'OR') {
foreach ($valids as $valid) {
if ($valid == true) {
return true;
}
}
return false;
}
else {
foreach ($valids as $valid) {
if ($valid == false) {
return false;
}
}
return true;
}
}
var_dump(check_req($arr['conditions']['requirements'])); // true in this case
当我对其进行 json_encode 时,我得到:
{
"required":"conditional",
"conditions":{
"requirements":{
"operator":"OR",
"0":{
"operator":"AND",
"0":{
"conditional1":32,
"conditional2":40,
"operation":"lessthan"
},
"1":{
"conditional1":10,
"conditional2":5,
"operation":"greaterthan"
}
},
"1":[{
"conditional1":20,
"conditional2":10,
"operation":"equals"
}]
}
}
}
我假设这是开发人员必须为您提供的。
关于php - 尝试不在 php 中使用 eval() 来评估带有条件语句的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17871112/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!