snipershady/typeidentifier

免费服务,用于识别变量的原始类型

v1.0.4 2024-03-11 22:00 UTC

This package is auto-updated.

Last update: 2024-09-11 23:03:37 UTC


README

有效的原始类型识别器。一个免费库,用于识别和清理从关联数组、超级全局数组或 Http 请求中的数据

先决条件

这个库可以在 PHP 5.6 的项目中安装,但对 PHP 8.1 完全兼容。PHP 8.1 的文档是面向 PHP 8.1 的。虽然这个库在 PHP 5.6 中更为重要,但也可以在新项目中使用来处理和清理 HTTP 请求值或异构关联数组

一些示例

加载依赖项

use TypeIdentifier\Service\EffectivePrimitiveTypeIdentifierService;
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue(1);   // Result will be 1 with type int
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue("1");   // Result will be 1 with type int
$array["value"] = "1.1";
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue($array["value"]);  // Result will be 1.1 with type float
$value = "1.1a";
$array["value"] = $value;
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue($array["value"]); // Result will be "1.1a" with type string
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue(1 === 1); // result will be true type bool
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValue("snipershady       ", true); // Trim enabled. Result will be "snipershady" without any whitespace and type string

强制字符串清理

$ept = new EffectivePrimitiveTypeIdentifierService();
$trim = true; // Can be false, is not mandatory if you want to force string sanitizing
$forceString = true;
$result = $ept->getTypedValue("1", $trim, $forceString);   // Result will be "1" with type string and will not handled as integer

使用偏移验证进行数组清理

$value = "snipershady";
$array["value"] = $value;
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValueFromArray("value", $array);  // Result will be "snipershady" sanitized and type string

在无效偏移处进行数组清理时返回 null

$value = "snipershady";
$array["value"] = $value;
$ept = new EffectivePrimitiveTypeIdentifierService();
$result = $ept->getTypedValueFromArray("invalid_offset", $array);  // Result null. "invalid_offset" is not a valid offset for the array.