efureev/php-type-normalizer

Normalize any types

v1.3.1 2024-10-15 12:39 UTC

This package is auto-updated.

Last update: 2024-10-15 12:40:01 UTC


README

PHP Package Latest Stable Version Total Downloads

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