helicon / type-converter
Simple typecasting system for PHP
Installs: 1 199
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=8.1
- helicon/object-type-parser: ^1.0
- laminas/laminas-hydrator: ^4.1
- nesbot/carbon: ^2.26
- psr/simple-cache: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
README
This library is convert array data to definition types.
using
$ composer req helicon/type-converter
<?php use Helicon\ObjectTypeParser\Parser; use Helicon\TypeConverter\Resolver; use Helicon\TypeConverter\Converter; use Helicon\TypeConverter\TypeCaster\ClassTypeCaster; use Helicon\TypeConverter\TypeCaster\DateTimeCaster; use Helicon\TypeConverter\TypeCaster\ScalarTypeCaster; use Laminas\Hydrator\ReflectionHydrator; $hydrator = new ReflectionHydrator(); $resolver = new Resolver(); $parser = new Parser(); $resolver->addTypeCaster(new ScalarTypeCaster()); $resolver->addTypeCaster(new DateTimeCaster()); $resolver->addTypeCaster(new ClassTypeCaster($resolver, $parser, $hydrator)); $converter = new Converter($resolver); $row = [ 'age' => '11', 'year' => '3', 'enable' => '0', 'createdAt' => '2019-10-29 11:00:00', 'friend' => [ 'id' => '21', 'name' => 'taro yamda', ], ]; $schemas = [ 'age' => [ 'type' => 'integer', ], 'year' => [ 'type' => 'int', ], 'enable' => [ 'type' => 'bool', ], 'createdAt' => [ 'type' => \DateTime::class, ], 'friend' => [ 'type' => Friend::class, ], ]; $convertedRows = ($converter)($row, $schemas); var_dump($convertedRows);