dgame/php-cast

维护者

详细信息

github.com/Dgame/php-cast

源代码

问题

安装量: 3,751

依赖项: 2

建议者: 0

安全性: 0

星标: 2

关注者: 1

分支: 0

公开问题: 0

类型:软件包

v0.9.0 2022-08-07 20:28 UTC

This package is auto-updated.

Last update: 2024-09-08 00:45:46 UTC


README

你是否曾经需要验证用户数据?很可能你使用过类似 webmozarts/assert

$id = $data['id'] ?? null;
Assert::integer($id);

问题是,尽管我们已经检查了 $id 必须是 int 类型,但实际上它仍然被视为 mixed 类型(参见 此示例)。要改变这一点,你需要编写/使用自己的 phpstan 规则,让 phpstan 相信它现在将始终是 int 类型。因此,如果你使用自己的验证方法,你也必须编写/使用自己的 phpstan 规则。

此软件包试图简化这一点。要验证某物是 int 类型,你可以 假设 它必须是 int 类型。如果不是,你将得到 null

use function Dgame\Cast\Assume\int;

$id = int($data['id'] ?? null);

有了这个,对于 phpstan、psalm、phpstorm 等工具,$id 的类型是 int|null

如果你想 断言 它是 int 类型,你也可以使用以下方式

use function Dgame\Cast\Assert\int;

$id = int($data['id'] ?? null);

现在 $id 的类型是 int 或它将以 AssertionError 失败。你也可以可选地设置 AssertionError 的消息

use function Dgame\Cast\Assert\int;

$id = int($data['id'] ?? null, message: 'The id of the given user must be of type int');

你可以为 intfloatboolstringnumberscalararray 值执行此操作。

int

intify

使用 intify,你可以获得 int 值或任何可用的 int 类型转换的 scalar

use function Dgame\Cast\Assume\intify;

$id = intify($data['id'] ?? null); // $id is of type int|null

unsigned

unsigned 如果给定的值是一个大于等于 0 的 数字,则返回一个非空值。

use function Dgame\Cast\Assume\unsigned;

$id = unsigned($data['id'] ?? null); // $id is of type int|null and >= 0 if it is an int

positive

positive 如果给定的值是一个大于 0 的 数字,则返回一个非空值。

use function Dgame\Cast\Assume\positive;

$id = positive($data['id'] ?? null); // $id is of type int|null and > 0 if it is an int

negative

negative 如果给定的值是一个小于 0 的 数字,则返回一个非空值。

use function Dgame\Cast\Assume\negative;

$id = negative($data['id'] ?? null); // $id is of type int|null and < 0 if it is an int

float

floatify

使用 floatify,你可以获得 float 值或任何可用的 float 类型转换的 scalar

use function Dgame\Cast\Assume\float;

$money = float($data['money'] ?? null); // $money is of type float|null

bool

使用 bool,你可以获得对于 truefalse10onoffyesno 或 null 的 bool 值。

use function Dgame\Cast\Assume\bool;

$checked = bool($data['checked'] ?? null); // $checked is of type bool|null

boolify

使用 boolify,你可以获得 bool 值或任何可用的 bool 类型转换的 scalar

use function Dgame\Cast\Assume\boolify;

$checked = boolify($data['checked'] ?? null); // $checked is of type bool|null

string

stringify

使用 stringify,你可以获得 string 值或任何可用的 string 类型转换的 scalar

use function Dgame\Cast\Assume\stringify;

$value = stringify($data['value'] ?? null); // $value is of type string|null

number

使用 number,你可以获得 intfloat 值或 null,如果它既不是 int 也不是 float

use function Dgame\Cast\Assume\number;

$range = number($data['range'] ?? null); // $range is of type int|float|null

scalar

使用 scalar,你可以获得 intfloatboolstring 值或 null,如果它既不是 intfloatbool 也不是 string

use function Dgame\Cast\Assume\scalar;

$value = scalar($data['value'] ?? null); // $value is of type int|float|bool|string|null

array

使用 collection,你可以测试你的值是否是一个 array

use function Dgame\Cast\Assume\collection;

$values = collection($data['values'] ?? null); // $values is of type array<int|string, mixed>|null

使用 collectionOf,你可以测试你的值是否是类型为 Tarray

use function Dgame\Cast\Assume\collectionOf;

$values = collectionOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type array<int|string, int>|null

如果数组中的所有值不是int类型,您将得到null。如果您只想过滤掉非int类型的值,可以使用filter方法。

use function Dgame\Cast\Collection\filter;

$values = filter('Dgame\Cast\Assume\int', $data['values'] ?? []); // $values is of type array<int|string, int>

但请注意,filter方法期望输入的是一个array<int|string, mixed>,而不是mixed类型!

列表

如果您想确保您有一个值列表(而不是关联数组),可以使用listOf

use function Dgame\Cast\Assume\listOf;

$values = listOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type int[]|null or, to be more accurate, of type array<int, int>|null

映射

如果您想确保您有一个关联数组(而不是列表),可以使用mapOf

use function Dgame\Cast\Assume\mapOf;

$values = mapOf('Dgame\Cast\Assume\int', $data['values'] ?? null); // $values is of type array<string, int>|null