sushi-market/smart-cast
最新稳定版本:v1.1.1
Composer 安装命令:
composer require sushi-market/smart-cast
包简介
Smart casts for PHP
README 文档
README
Smart Cast - An elegant and type-safe library for intelligent type casting in PHP. Eliminate boilerplate code and work with data confidently!
✨ Features
- 🎯 Intuitive API - Simple and clear syntax
- 🛡️ Type Safety - Comprehensive validation and exception handling
- 🚀 Performance - Optimized conversions with minimal overhead
- 📦 Zero Dependencies - No external dependencies required
🚀 Quick Start
Installation
composer require sushi-market/smart-cast
<?php use DF\SmartCast\SmartCast; use DF\SmartCast\NumberSign; // Convert string to integer with strict type $intValue = SmartCast::stringToInt('123', strictType: false); // Returns 123 // Convert with sign and zero validation $positiveInt = SmartCast::stringToInt('456', sign: NumberSign::POSITIVE, acceptsZero: false); // Convert to float with null validation $floatValue = SmartCast::stringToFloat('123.45', acceptNull: false); // Returns 123.45 // Convert to boolean $boolValue = SmartCast::stringToBoolean('true'); // Returns true // Convert JSON-like array string $array = SmartCast::stringToArray('[1,2,3]'); // Returns: [1, 2, 3] // Convert delimited string to array $array = SmartCast::stringToArray('1,2,3,4'); // Returns: [1, 2, 3, 4] // Validate against array of allowed values $status = SmartCast::ensureAllowedString( value: 'foo', allowedValues: ['foo', 'bar', 'baz'] ); // Returns: 'foo' // Validate against backed enum $role = SmartCast::ensureAllowedString( value: 'admin', allowedValues: UserRoleEnum::class ); // Returns: 'admin'
or use helpers
<?php // Convert string to integer with strict type $intValue = stringToInt('123', strictType: false); // Returns 123 // Convert with sign and zero validation $positiveInt = stringToInt('456', sign: NumberSign::POSITIVE, acceptsZero: false); // Convert to float with null validation $floatValue = stringToFloat('123.45', acceptNull: false); // Returns 123.45 // Convert to boolean $boolValue = stringToBoolean('true'); // Returns true // Convert JSON-like array string $array = stringToArray('[1,2,3]'); // Returns: [1, 2, 3] // Convert delimited string to array $array = stringToArray('1,2,3,4'); // Returns: [1, 2, 3, 4] // Validate against array of allowed values $status = ensureAllowedString( value: 'foo', allowedValues: ['foo', 'bar', 'baz'] ); // Returns: 'foo' // Validate against backed enum $role = ensureAllowedString( value: 'admin', allowedValues: UserRoleEnum::class ); // Returns: 'admin'
统计信息
- 总下载量: 802
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-24