omasn / object-handler
该组件允许根据类严格属性映射从关联数组填充对象
4.3.1
2023-03-30 13:07 UTC
Requires
- php: >=7.4
- symfony/polyfill-php80: ^1.25
- symfony/property-access: ^4.4|^5.4|^6.2
- symfony/property-info: ^4.4|^5.4|^6.2
- symfony/validator: ^4.4|^5.4|^6.2
Requires (Dev)
- phpdocumentor/reflection-docblock: ^3.0|^4.0|^5.0
- phpunit/phpunit: ^8.0|^9.5|^10.0
- symfony/var-dumper: ^4.4|^5.4|^6.2
README
该组件允许根据类严格属性映射从关联数组填充对象
安装
使用以下命令安装最新版本
$ composer require omasn/object-handler
基本用法
<?php use Omasn\ObjectHandler\HandleTypes\HandleBoolType; use Omasn\ObjectHandler\HandleTypes\HandleIntType; use Omasn\ObjectHandler\HandleTypes\HandleStringType; use Omasn\ObjectHandler\ObjectHandler; use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; use Symfony\Component\PropertyInfo\PropertyInfoExtractor; // create a object handler and configure project handle types $objectHandler = ObjectHandler::createSimple([ new HandleStringType(), new HandleIntType(), new HandleBoolType(), ]); $object = new class { public string $text; public int $count; public bool $active; }; try { $objectHandler->handleObject($object, [ 'text' => 123, 'count' => '5', 'active' => 0, ]); } catch (\Omasn\ObjectHandler\Exception\ViolationListException $e) { $e->getViolationList()->count(); // Count handle validation errors } var_dump($object); // object(class@anonymous)#277 (3) { // ["text"]=> // string(3) "123" // ["count"]=> // int(5) // ["active"]=> // bool(false) // }
关于
作者
Roman Yastrebov (Telegram: https://t.me/omasn)
许可证
Object handler 采用 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE
文件