|
|
|
@ -193,4 +193,55 @@ class ParkingLicensePlateService |
|
|
|
} |
|
|
|
return $license_id; |
|
|
|
} |
|
|
|
|
|
|
|
// 验证车牌格式 |
|
|
|
public function validateLicensePlate($license_plate): string |
|
|
|
{ |
|
|
|
$plate = str_replace(' ', '', $license_plate); |
|
|
|
|
|
|
|
// 2. 根据车牌类型选择正则 |
|
|
|
if ($this->isMainlandChinaPlate($plate)) { |
|
|
|
// 中国大陆车牌校验(禁止 I、O) |
|
|
|
$pattern |
|
|
|
= '/^[\x{4e00}-\x{9fa5}][A-Z](?:[A-HJ-NP-Z0-9]{5}|(?:[DF][A-HJ-NP-Z0-9][0-9]{4}|[0-9]{5}[DF]))$/u'; |
|
|
|
if (!preg_match($pattern, $plate)) { |
|
|
|
return __validation('parking_space.zh_plate'); |
|
|
|
} |
|
|
|
} elseif ($this->isHongKongPlate($plate)) { |
|
|
|
// 中国香港车牌校验(禁止 I、O、Q) |
|
|
|
$pattern = '/^[A-HJ-NP-Z0-9]{1,8}$/'; |
|
|
|
if (!preg_match($pattern, $plate)) { |
|
|
|
return __validation('parking_space.hk_plate'); |
|
|
|
} |
|
|
|
} |
|
|
|
return ''; |
|
|
|
} |
|
|
|
|
|
|
|
private function isMainlandChinaPlate(string $plate): bool |
|
|
|
{ |
|
|
|
// 长度校验:普通车牌7位,新能源车牌8位[reference:12][reference:13] |
|
|
|
$length = mb_strlen($plate); |
|
|
|
if (!in_array($length, [7, 8])) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 基础格式校验:首字为汉字,第二位为大写字母 |
|
|
|
if (!preg_match('/^[\x{4e00}-\x{9fa5}][A-Z]/u', $plate)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private function isHongKongPlate(string $plate): bool |
|
|
|
{ |
|
|
|
// 长度校验:1-8位 |
|
|
|
$length = strlen($plate); |
|
|
|
if ($length < 1 || $length > 8) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 仅包含字母和数字 |
|
|
|
return ctype_alnum($plate); |
|
|
|
} |
|
|
|
} |
|
|
|
|