定制 dgame/php-cast 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

dgame/php-cast

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

dgame/php-cast 适用场景与选型建议

dgame/php-cast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.01k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 dgame/php-cast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dgame/php-cast 我们能提供哪些服务?
定制开发 / 二次开发

基于 dgame/php-cast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-11