承接 dazet/type-utils 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dazet/type-utils

Composer 安装命令:

composer require dazet/type-utils

包简介

Type casting utils for PHP.

README 文档

README

Build Status

StringUtil

StringUtil::canBeString(mixed $value): bool

Returns true if $value can be safely casted to string: is scalar or object with __toString method or null.

StringUtil::canBeString method shortcut:

$array = ['string', 123, [], null];
array_filter($array, StringUtil::canBeString); // ['string', 123, null]

StringUtil::toStringOrNull(mixed $value): ?string

Casts value to string if possible or returns null.

StringUtil::toStringOrNull method shortcut:

$a = ['string', 123, [], new stdClass()];
array_map(StringUtil::toStringOrNull, $a); // ['string', '123', null, null]

StringUtil::toString(mixed $value): string

Casts value to string if possible or throws InvalidTypeException.

StringUtil::toString method shortcut:

$a = ['string', 123, null];
array_map(StringUtil::toString, $a); // ['string', '123', '']

$b = ['string', new stdClass()];
array_map(StringUtil::toString, $b); // throws InvalidTypeException

NumberUtil

NumberUtil::canBeNumber(mixed $value): bool

Returns true if value can be safely casted to int|float, which means that:

  • it is number
  • or numeric string
  • or can be casted to numeric string
  • or is a boolean that can be transformed to 0|1
  • or is null

NumberUtil::canBeNumber method shortcut:

$array = [123, 1.23, '123', 'string', null];
array_filter($array, NumberUtil::canBeNumber); // [123, 1.23, '123', null]

NumberUtil::toIntOrNull(mixed $value): ?int

Casts value to int if possible or returns null.

NumberUtil::toIntOrNull method shortcut:

$a = [123, 1.23, 'string'];
array_map(NumberUtil::toIntOrNull, $a); // [123, 1, null]

NumberUtil::toInt(mixed $value): int

Casts value to int if possible or throws InvalidTypeException.

NumberUtil::toFloatOrNull(mixed $value): ?float

Casts value to float if possible or returns null.

NumberUtil::toFloatOrNull method shortcut:

$a = [123, 1.23, 'string'];
array_map(NumberUtil::toFloatOrNull, $a); // [123.0, 1.23, null]

NumberUtil::toFloat(mixed $value): float

Casts value to float if possible or throws InvalidTypeException.

ArrayUtil

ArrayUtil::canBeArray(mixed $value): bool

Returns true if $value is or can be converted to array, which means it is iterable or null.

ArrayUtil::canBeArray method shortcut:

$array = [['array'], new ArrayObject(['object']), 'string'];
array_filter($array, ArrayUtil::canBeArray); // [['array'], new ArrayObject(['object'])]

ArrayUtil::toArrayOrNull(mixed $value): ?array<int|string, mixed>

Transforms $value to array (keeping keys) or returns null.

ArrayUtil::toArrayOrNull method shortcut:

$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayOrNull, $a); // ['a' => ['array'], 'b' => null, 'c' => ['object']]

$iterate = function() {
    yield 9 => 'nine';
    yield 9 => 'nine';
    yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // [9 => 'nine']

ArrayUtil::toArrayListOrNull(mixed $value): ?array<int, mixed>

Transforms $value to array-list (reindex keys) or returns null.

ArrayUtil::toArrayListOrNull method shortcut:

$a = ['a' => ['array'], 'b' => 'string', 'c' => new ArrayObject(['object'])];
array_map(ArrayUtil::toArrayListOrNull, $a); // [['array'], null, ['object']]

$iterate = function() {
    yield 9 => 'nine';
    yield 9 => 'nine';
    yield 9 => 'nine';
};
array_map(ArrayUtil::toArrayOrNull, $iterate()); // ['nine', 'nine', 'nine']

ArrayUtil::toArray(mixed $value): array<int|string, mixed>

Transforms $value to array (keeping keys) or throws InvalidTypeException.

ArrayUtil::toArrayList(mixed $value): array<int, mixed>

Transforms $value to array-list (reindex keys) or throws InvalidTypeException.

ArrayUtil::isCountable(mixed $value): bool

Same as standard is_countable, additionally it allows to shortcut callable by ArrayUtil::isCountable.

ArrayUtil::countOrNull(mixed $value): ?int

Returns count if $value is countable or returns null.

BooleanUtil

BooleanUtil::canBeBool(mixed $value): bool

Returns true if $value can be taken as boolean (true, false, 0, 1).

BooleanUtil::canBeBool shortcut also available.

BooleanUtil::toBoolOrNull(mixed $value): ?bool

Casts value to bool if possible or returns null.

BooleanUtil::toBool(mixed $value): ?bool

Casts value to bool if possible or throws InvalidTypeException.

DateUtil

DateUtil::canBeDate(mixed $value): bool

Returns true if $value can be converted to DateTimeImmutable, which means it has DateTimeInterface or is a string supported by strtotime.

DateUtil::canBeDate method shortcut:

$now = new DateTime('now');
$a = ['today', $now, 'never'];
array_filter($a, DateUtil::canBeDate); // ['today', $now]

DateUtil::toDatetimeOrNull(mixed $value): ?DateTimeImmutable

Transforms value to DateTimeImmutable if possible or returns null.

Shortcut DateUtil::toDatetimeOrNull also available.

DateUtil::toDatetime(mixed $value): DateTimeImmutable

Transforms value to DateTimeImmutable if possible or throws InvalidTypeException.

Shortcut DateUtil::toDatetime also available.

DateUtil::toDateFormatOrNull(mixed $value, string $format): ?string

Transforms $value to given date format if $value can be date or returns null otherwise.

DateUtil::toDateFormat(mixed $value, string $format): string

Transforms $value to given date format if $value can be date or throws InvalidTypeException.

DateUtil::toTimestampOrNull(mixed $value): ?int

Transforms $value to Unix timestamp if $value can be date or returns null otherwise.

DateUtil::toTimestamp(mixed $value): int

Transforms $value to Unix timestamp if $value can be date or throws InvalidTypeException.

dazet/type-utils 适用场景与选型建议

dazet/type-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.38k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 11 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 dazet/type-utils 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-18