kba-team/typecast

Typecast the values of any given data structure.

1.1.0 2020-01-27 12:55 UTC

This package is auto-updated.

Last update: 2024-11-27 23:17:49 UTC


README

License: MIT Build Status Maintainability Test Coverage

Typecast the values of any given data structure.

Installation

composer install kba-team/typecast

Usage

<?php
use kbATeam\TypeCast\TypeCastArray;
use \kbATeam\TypeCast\TypeCastValue;

//prepare typecast rules
$cast = new TypeCastArray();
$cast['myIntValue'] = new TypeCastValue('integer');
$cast['myFloatValues'] = new TypeCastArray();
$cast['myFloatValues'][0] = new TypeCastValue('float');
$cast['mySettings'] = new TypeCastArray();
$cast['mySettings']['a'] = new TypeCastValue('boolean');
$cast['mySettings']['b'] = new TypeCastValue('boolean');

//the raw data array
$raw_data = [
    'myIntValue' => ' 123    ',
    'myFloatValues' => [
        '  3.00',
        '  4.50'
    ],
    'mySettings' => [
        'a' => '0',
        'b' => '1'
    ]
];

//cast the raw data
$data = $cast($raw_data);

echo serialize($data);
/* Output:
a:3:{s:10:"myIntValue";i:123;s:13:"myFloatValues";a:2:{i:0;d:3;i:1;d:4.5;}s:10:"mySettings";a:2:{s:1:"a";b:0;s:1:"b";b:1;}}
 */