dgame/php-cast 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dgame/php-cast

最新稳定版本:v0.9.0

Composer 安装命令:

composer require dgame/php-cast

包简介

无描述信息

README 文档

README

Have you ever had to validate user data? Probably you have used something like webmozarts/assert:

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

The problem is, even though we checked that $id must be an int, it is actually still seen as mixed (see this example for phpstan). To change this, you need to write / use your own phpstan rule that makes phpstan believe that it will now always be an int. So if you use your own verification methods, you must also write / use your own phpstan rules.

This package tries to simplify that. To verify that something is an int, you can assume that it must be an int. If it is not, you get null:

use function Dgame\Cast\Assume\int; $id = int($data['id'] ?? null);

With this, $id is of type int|null for phpstan, psalm, phpstorm and so on.

If you want to assert that it is an int, you can do that too by using:

use function Dgame\Cast\Assert\int; $id = int($data['id'] ?? null);

Now $id is of type int or it fails with an AssertionError. A message for the AssertionError can also be optionally set:

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

You can do that for int, float, bool, string, number, scalar and array values.

int

intify

With intify you get either the int-value or the int-casted scalar value, if any:

use function Dgame\Cast\Assume\intify; $id = intify($data['id'] ?? null); // $id is of type int|null

unsigned

unsigned will return a non-null value, if the given value is a number that is >= 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 will return a non-null value, if the given value is a number that is > 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 will return a non-null value, if the given value is a number that is < 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

With floatify you get either the float-value or the float-casted scalar value, if any:

use function Dgame\Cast\Assume\float; $money = float($data['money'] ?? null); // $money is of type float|null

bool

With bool you get the bool-value for true, false, 1, 0, on, off, yes, no or null.

use function Dgame\Cast\Assume\bool; $checked = bool($data['checked'] ?? null); // $checked is of type bool|null

boolify

With boolify you get either the bool-value or the bool-casted scalar value, if any:

use function Dgame\Cast\Assume\boolify; $checked = boolify($data['checked'] ?? null); // $checked is of type bool|null

string

stringify

With stringify you get either the string-value or the string-casted scalar value, if any:

use function Dgame\Cast\Assume\stringify; $value = stringify($data['value'] ?? null); // $value is of type string|null

number

With number you get either the int or float-value or null, if it is neither.

use function Dgame\Cast\Assume\number; $range = number($data['range'] ?? null); // $range is of type int|float|null

scalar

With scalar you get either the int, float, bool or string-value or null, if it is neither.

use function Dgame\Cast\Assume\scalar; $value = scalar($data['value'] ?? null); // $value is of type int|float|bool|string|null

array

With collection you can test whether your value is an array:

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

And with collectionOf you can test whether your value is an array of type T:

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

If not all values in the array are of type int, you get null. If you just want to filter the non-int values, you can do that by using filter:

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

But be aware that filter expects an array<int|string, mixed> as input and not mixed!

list

If you want to make sure, that you have a list of values (and not an assoc. array) you can use 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

map

If you want to make sure, that you have an assoc. array (and not a list) you can use mapOf:

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

统计信息

  • 总下载量: 8.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固