Skip to content

Commit ba59456

Browse files
authored
支持多维数组的校验
1 parent d746591 commit ba59456

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

src/Validate.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,26 +1397,35 @@ public function getError()
13971397
return $this->error;
13981398
}
13991399

1400+
14001401
/**
14011402
* 获取数据值
14021403
* @access protected
14031404
* @param array $data 数据
1404-
* @param string $key 数据标识 支持二维
1405+
* @param string $key 数据标识 支持多维嵌套
14051406
* @return mixed
14061407
*/
14071408
protected function getDataValue($data, $key)
14081409
{
1410+
// 如果键是数字,直接返回键值
14091411
if (is_numeric($key)) {
1410-
$value = $key;
1411-
} elseif (strpos($key, '.')) {
1412-
// 支持二维数组验证
1413-
list($name1, $name2) = explode('.', $key);
1414-
$value = isset($data[$name1][$name2]) ? $data[$name1][$name2] : null;
1415-
} else {
1416-
$value = isset($data[$key]) ? $data[$key] : null;
1412+
return $key;
1413+
}
1414+
1415+
// 支持多级嵌套字段
1416+
if (strpos($key, '.') !== false) {
1417+
$keys = explode('.', $key);
1418+
foreach ($keys as $k) {
1419+
if (!is_array($data) || !array_key_exists($k, $data)) {
1420+
return null;
1421+
}
1422+
$data = $data[$k];
1423+
}
1424+
return $data;
14171425
}
14181426

1419-
return $value;
1427+
// 单级键,直接访问
1428+
return isset($data[$key]) ? $data[$key] : null;
14201429
}
14211430

14221431
/**

0 commit comments

Comments
 (0)