dgame / php-cast
Requires
- php: ^8.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.4
- ergebnis/phpstan-rules: ^0.15
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/phpstan: ^0.12
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^9.4
- roave/security-advisories: dev-latest
- slevomat/coding-standard: dev-master
- spaceemotion/php-coding-standard: dev-master
- spaze/phpstan-disallowed-calls: ^1.5
- symplify/easy-coding-standard: ^9.3
- thecodingmachine/phpstan-safe-rule: ^1.0
- thecodingmachine/phpstan-strict-rules: ^0.12
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');
你可以为 int、float、bool、string、number、scalar 和 array 值执行此操作。
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
,你可以获得对于 true
、false
、1
、0
、on
、off
、yes
、no
或 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
,你可以获得 int
或 float
值或 null,如果它既不是 int
也不是 float
。
use function Dgame\Cast\Assume\number; $range = number($data['range'] ?? null); // $range is of type int|float|null
scalar
使用 scalar
,你可以获得 int
、float
、bool
或 string
值或 null,如果它既不是 int
、float
、bool
也不是 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
,你可以测试你的值是否是类型为 T
的 array
。
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