定制 yzen.dev/typer 二次开发

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

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

yzen.dev/typer

最新稳定版本:0.2.1

Composer 安装命令:

composer require yzen.dev/typer

包简介

Get property from dynamic instance

README 文档

README

Packagist Version GitHub Workflow Status (with branch) Coverage Packagist Downloads Packagist Downloads

Mutation testing badge type-coverage psalm-level

This is a simple helper package that helps make the code cleaner. Often, when working with data from third-party sources, such as website parsing, you need to write hundreds of lines of code to check for a particular property.

Most likely, you write a lot of if or ternary operators, and your code looks something like this:

$user = new User();
$user->id = isset($dynamicArray['id']) ? (int)$dynamicArray['id'] : null;
$user->email = isset($dynamicArray['email']) ? (string)$dynamicArray['email'] : null;
$user->balance = isset($dynamicArray['balance']) ? (float)$dynamicArray['balance'] : null;
$user->blocked = isset($dynamicArray['blocked']) ? ($dynamicArray['blocked'] === 'true' ? true : false) : null;

When using Typer, you don't need to worry about a lot of checks and transformations. Simply wrap the code in the typer method:

$user = new User();
$user->id = Typer::int($dynamicArray, 'id');
$user->email = Typer::string($dynamicArray, 'email');
$user->balance = Typer::float($dynamicArray, 'balance');
$user->blocked = Typer::bool($dynamicArray, 'blocked');

If, in the absence of a parameter, you need to specify a default value other than "null", you can simply pass it as the second argument:

$user->balance = Typer::float($dynamicArray, 'balance', 10.0);

Installation

The package can be installed via composer:

composer require yzen.dev/typer

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-18