sushi-market / smart-cast
Smart casts for PHP
Installs: 433
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/sushi-market/smart-cast
Requires
- php: ^8.1
Requires (Dev)
- laravel/pint: ^1.24.0
- peckphp/peck: ^0.1.3
- pestphp/pest: ^4.1.0
- pestphp/pest-plugin-type-coverage: ^4.0.2
- phpstan/phpstan: ^2.1.26
- psy/psysh: ^0.12.14
- rector/rector: ^2.1.7
- symfony/var-dumper: ^7.3.3
- dev-master
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-code_of_conduct
- dev-fix_aliases
- dev-update_docs
- dev-namespace_migration
- dev-feature/DF-4493_dobavit-v-smart-cast-metod-dlya-validacii-stroki
- dev-feature/DF-4486_dobavit-v-paket-kast-v-massiv
- dev-4-stringtoint-silently-overflows-on-large-integers-instead-of-throwing-an-exception
This package is auto-updated.
Last update: 2025-11-19 12:04:59 UTC
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'