efureev / php-type-normalizer
Normalize any types
Installs: 5 973
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
Requires (Dev)
- ergebnis/composer-normalize: ^2.42
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^10.5
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.9
README
Description
It transforms mixed types into you need.
Install
For php >= 8.2
composer require efureev/php-type-normalizer "^1.0"
Basic Using
TypeNormalizer::toInt('1') // 1 TypeNormalizer::toBool('1') // true TypeNormalizer::toFloat('1.2') // 1.2 TypeNormalizer::toString(true) // 'true'
Using with Middleware
Middleware may be any callable
variable.
TypeNormalizer::toInt(' -132323 ', 'abs'); // 132323 TypeNormalizer::toInt(' -2 ', 'abs', ['pow', 2]); // 4 TypeNormalizer::toString(' test ', 'trim', 'mb_strtoupper'); // 'TEST' TypeNormalizer::toString(null, static fn(string $item) => '<none>'); // '<none>'
Middleware may be defined with params:
TypeNormalizer::toInt(' -2 ', 'abs', ['pow', 2]); // 4 TypeNormalizer::toInt(' -2 ', 'abs', [fn(int $item, int $plus) => pow($item, 2) + $plus, 1]); // 5
Transformation Map
Integer
TypeNormalizer::toInt(1) // 1 TypeNormalizer::toInt(211) // 211 TypeNormalizer::toInt('211') // 211 TypeNormalizer::toInt(' 211 ') // 211 TypeNormalizer::toInt('1') // 1 TypeNormalizer::toInt('true') // 1 TypeNormalizer::toInt(' true ') // 1 TypeNormalizer::toInt(true) // 1 TypeNormalizer::toInt(1.00) // 1 TypeNormalizer::toInt('1.00') // 1 TypeNormalizer::toInt(false) // 0 TypeNormalizer::toInt(' false ') // 0 TypeNormalizer::toInt(' 0 ') // 0 TypeNormalizer::toInt(0) // 0 TypeNormalizer::toInt('') // 0 TypeNormalizer::toInt(' ') // 0 TypeNormalizer::toInt(null) // 0 TypeNormalizer::toInt('hello') // exception TypeNormalizer::toInt('1.2') // exception TypeNormalizer::toInt(1.2) // exception
Boolean
TypeNormalizer::toBool(1) // true TypeNormalizer::toBool(' 1 ') // true TypeNormalizer::toBool(true) // true TypeNormalizer::toBool(' true ') // true TypeNormalizer::toBool(' 1.00 ') // true TypeNormalizer::toBool(1.00) // true TypeNormalizer::toBool(0) // false TypeNormalizer::toBool(false) // false TypeNormalizer::toBool(' false ') // false TypeNormalizer::toBool(' 0 ') // false TypeNormalizer::toBool(' 0.0 ') // false TypeNormalizer::toBool('') // false TypeNormalizer::toBool(' ') // false TypeNormalizer::toBool(null) // false TypeNormalizer::toBool('hello') // exception TypeNormalizer::toBool(1.2) // exception TypeNormalizer::toBool(22) // exception TypeNormalizer::toBool('22') // exception TypeNormalizer::toBool(' 0. 00') // exception
Float
TypeNormalizer::toFloat(1) // 1 TypeNormalizer::toFloat(' 1 ') // 1 TypeNormalizer::toFloat(' -132323 ') // -1 TypeNormalizer::toFloat(true) // 1 TypeNormalizer::toFloat(' true ') // 1 TypeNormalizer::toFloat(' 1.00 ') // 1 TypeNormalizer::toFloat(' 21.21 ') // 21.21 TypeNormalizer::toFloat(1.00) // 1 TypeNormalizer::toFloat(0) // 0 TypeNormalizer::toFloat(false) // 0 TypeNormalizer::toFloat(' false ') // 0 TypeNormalizer::toFloat(' 0 ') // 0 TypeNormalizer::toFloat(' 0.0 ') // 0 TypeNormalizer::toFloat('') // 0 TypeNormalizer::toFloat(' ') // 0 TypeNormalizer::toFloat(null) // 0 TypeNormalizer::toFloat('hello') // exception
Test
composer test composer test-cover # with coverage