PHP code example of efureev / php-type-normalizer

1. Go to this page and download the library: Download efureev/php-type-normalizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

efureev / php-type-normalizer example snippets


TypeNormalizer::toInt('1') // 1
TypeNormalizer::toBool('1') // true
TypeNormalizer::toFloat('1.2') // 1.2
TypeNormalizer::toString(true) // 'true'

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>'

TypeNormalizer::toInt('  -2  ', 'abs', ['pow', 2]); // 4
TypeNormalizer::toInt('  -2  ', 'abs', [fn(int $item, int $plus) => pow($item, 2) + $plus, 1]); // 5

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

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

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
bash
composer