承接 jeroenvanderlaan/php-types 相关项目开发

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

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

jeroenvanderlaan/php-types

最新稳定版本:1.0.0

Composer 安装命令:

composer require jeroenvanderlaan/php-types

包简介

Utilities for working with and formatting PHP types.

README 文档

README

Utilities for working with and formatting PHP types.

use Jeroenvanderlaan\Types\Type;
use Jeroenvanderlaan\Types\TypeFormatter;
use Jeroenvanderlaan\Types\TypeInflector;

Type::get('float')->isCastableTo(Type::Int); // true

TypeInflector::typeof('John'); // Type::String

TypeFormatter::formatTypeOf([1, 2, 'a', 'b']); // 'list<int|string>'

Installation

composer require jeroenvanderlaan/php-types

Usage

use Jeroenvanderlaan\Types\Type;
use Jeroenvanderlaan\Types\TypeFormatter;
use Jeroenvanderlaan\Types\TypeInflector;

// Get a Type by name or alias
Type::get('int'); // Type::Int
Type::get('integer'); // Type::Int

// Parse union Types
Type::parse('int|float'); // [Type::Int, Type::Float]

// Inflect a Type from a value
TypeInflector::typeof('foobar'); // Type::String
TypeInflector::typeof([1, 2]); // Type::List
TypeInflector::typeof(['a' => 'b'])); // Type::Map

// Format a Type
TypeFormatter::formatType(Type::Int); // 'int'
TypeFormatter::formatType(Type::Int, Type::Float); // 'int|float'

// Format the Type of value
TypeFormatter::formatTypeOf(1); // 'int'
TypeFormatter::formatTypeOf([1, 'a']); // 'list<int|string>'
TypeFormatter::formatTypeOf([['a'], ['b']]); // 'list<list<string>>'
TypeFormatter::formatTypeOf(new stdClass); // 'stdClass'

Examples

use Jeroenvanderlaan\Types\Type;
use Jeroenvanderlaan\Types\TypeFormatter;
use Jeroenvanderlaan\Types\TypeInflector;

Type::get('null'); // Type::Null
Type::get('bool'); // Type::Bool
Type::get('int'); // Type::Int
Type::get('float'); // Type::Float
Type::get('number'); // Type::Number
Type::get('string'); // Type::String
Type::get('scalar'); // Type::Scalar
Type::get('resource'); // Type::Resource
Type::get('callable'); // Type::Callable
Type::get('object'); // Type::Object
Type::get('list'); // Type::List
Type::get('map'); // Type::Map
Type::get('array'); // Type::Array
Type::get('iterable'); // Type::Iterable

TypeInflector::typeof(null); // Type::Null
TypeInflector::typeof(true); // Type::Bool
TypeInflector::typeof(1); // Type::Int
TypeInflector::typeof(1.0); // Type::Float
TypeInflector::typeof('1.0'); // Type::Number
TypeInflector::typeof('John'); // Type::String
TypeInflector::typeof(STDOUT); // Type::Resource
TypeInflector::typeof(fn() => null); // Type::Callable
TypeInflector::typeof(new stdClass()); // Type::Object
TypeInflector::typeof([1, 2]); // Type::List
TypeInflector::typeof(['a' => 'b']); // Type::Map
TypeInflector::typeof([1, 'a' => 'b']); // Type::Array
TypeInflector::typeof(new ArrayIterator([1, 2])); // Type::Iterable

TypeFormatter::formatTypeOf(null); // 'null'
TypeFormatter::formatTypeOf(true); // 'bool'
TypeFormatter::formatTypeOf(1); // 'int'
TypeFormatter::formatTypeOf(1.0); // 'float'
TypeFormatter::formatTypeOf('1.0'); // 'number'
TypeFormatter::formatTypeOf('John'); // 'string'
TypeFormatter::formatTypeOf(STDOUT); // 'resource'
TypeFormatter::formatTypeOf(fn() => null); // 'callable'
TypeFormatter::formatTypeOf(new stdClass()); // 'stdClass
TypeFormatter::formatTypeOf([1, 2]); // 'list<int>'
TypeFormatter::formatTypeOf([true, 1, 1.0, '1.0']); // 'list<bool|int|float|string>'
TypeFormatter::formatTypeOf(['a' => 'b']); // 'map<string>'
TypeFormatter::formatTypeOf(['a' => 'b', 'b' => 1]); // 'map<string|int>'
TypeFormatter::formatTypeOf([1, 'a' => 'b']); // 'array<int|string>'
TypeFormatter::formatTypeOf(new ArrayIterator([1, 2])); // 'ArrayIterator<int>'

Type::Bool->isEqualTo(Type::Bool); // true
Type::Int->isOneOf(Type::Int, Type::Float); // true
Type::Int->isCastableTo(Type::Float); // true
Type::Mixed->isMixed(); // true
Type::Number->isNumeric(); // true
Type::String->isScalar(); // true
Type::Resource->isComplex(); // true
Type::Iterable->isArrayLike(); // true
Type::Map->isObjectLike(); // true

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-22