fuc-new/src/Services/TomTool/Nice.php
2026-04-30 15:44:52 +08:00

31 lines
760 B
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services\TomTool;
class Nice
{
/**
* 确保返回必然的布尔值
*
* @param mixed $xValue 传入的原始数据 (x前缀代表未知类型)
* @param bool $bDefault 默认值
* @return bool
*/
public static function ensureBool(mixed $xValue, bool $bDefault = false): bool
{
// 如果为 null 直接返回默认值,避免 filter_var 的空字符串陷阱
if (is_null($xValue)) {
return $bDefault;
}
// 严谨校验:确保字符串 "false", "no", "0" 都能正确识别
return filter_var($xValue, FILTER_VALIDATE_BOOLEAN, [
'flags' => FILTER_NULL_ON_FAILURE
]) ?? $bDefault;
}
}