承接 jasny/typecast 相关项目开发

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

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

jasny/typecast

最新稳定版本:v2.1.1

Composer 安装命令:

composer require jasny/typecast

包简介

Type casting with basic logic

关键字:

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

This library does type casting in PHP.

Type casting is natively supported in PHP. This library adds some basic logic to the process, like triggering a warning when casting a string like "FOO" to an integer.

In contrary to PHP's internal type casting, casting null always results in null.

Installation

The Jasny TypeCast package is available on packagist. Install it using composer:

composer require jasny/typecast

Usage

use Jasny\TypeCast;

$typecast = new TypeCast();

$typecast->to('string')->cast(null); // null

$typecast->to('integer')->cast('987'); // 987
$typecast->to(DateTime::class)->cast('2015-01-01'); // new DateTime('2015-01-01)
$typecast->to(FooBar::class)->cast($data); // FooBar::__set_state($data)

// Unable to cast
$typecast->to('float')->cast('red'); // 'red' + triggers a notice
$typecast->to('int')->cast(new stdClass()); // stdClass object + triggers a notice

Alias

You can set aliases in cases where you might need to cast to an interface or abstract class or when you want to cast to a child class.

$typecast = new TypeCast();
$typecast->alias(FooBarInterface::class, FooBar::class);

$typecast->to(FooBarInterface::class)->cast($data); // FooBar::__set_state($data)

Errors

By default an E_NOTICE is triggered if a value can't be casted to desired type. Jasny\TypeCast follows stricter rules than PHP for casting values.

Instead of a notice an error of any severity can be triggered. Alternatively any Throwable like an exception or error can be thrown.

$typecast = new TypeCast();

$typecast->failWith(E_USER_WARNING);
$typecast->failWith(TypeError::class);
$typecast->failWith(UnexpectedValueException::class);

Variable name in error

You can use the setName() method to set the property or variable name that is casted. This name will be included in any error triggered when type casting. This can be useful when determining an issue.

$foo = 'red';
$typecast->value($foo)->setName('foo')->to('float');

Dependency injection

If your application supports dependency injection through containers, create a new TypeCast object and add it to the container as a service.

The value() method will clone the TypeCast object. Settings like any aliases or custom handlers will propagate.

use Jasny\TypeCast;
use Jasny\TypeCastInterface;

$container = new Container([
  TypeCastInterface::class => function() {
    $typecast = new TypeCast();
    $typecast->alias(FooBarInterface::class, FooBar::class);
    
    return $typecast;
  }
]);

$container->get(TypeCastInterface::class)->value('987')->to('integer');

Assume that Container is any PSR-11 compatible container.

Handlers

The Typecast object uses handlers to cast a value. Each handler can cast a value to a specific type. The following handlers are defined:

  • array (includes typed arrays as string[] and DateTime[])
  • boolean
  • float
  • integer
  • number (int|float)
  • mixed
  • object (includes casting to a specific class)
  • resource
  • string
  • multiple (e.g. int|string and string|string[])

You may overwrite the handlers when creating the TypeCast object.

Desire

The desire method will return the handler. This is an alternative approach of using the value method. If you need to cast multiple values to the same type, it's recommendable to get the handler once using desire.

use Jasny\TypeCast;

$typecast = new TypeCast();
$typecast->desire('integer')->cast('10');

$arrayHandler = $typecast->desire('array'); 
foreach ($items as &$item) {
  $item = $arrayHandler->cast($item);
}

Multiple handler

In cast multiple types are specified, the handler will try to guess the type the value should be cast in. This might hurt performance. You may use NoTypeGuess to have the handler give an error if the type can't be determined.

use Jasny\TypeCast;

$multipleHandler = new TypeCast\Handlers\MultipleHandler(new TypeCast\NoTypeGuess()); 
$typecast = new TypeCast(null, ['multiple' => $multipleHandler] + TypeCast::getDefaultHandlers());

jasny/typecast 适用场景与选型建议

jasny/typecast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.17k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 08 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「type casting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 jasny/typecast 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 7
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-30