omasn / object-handler
The component allows filling objects from an associative array based on class strict property maps
Installs: 6 331
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 2
Open Issues: 1
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
The component allows filling objects from an associative array based on class strict property maps
Installation
Install the latest version with
$ composer require omasn/object-handler
Basic Usage
<?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) // }
About
Author
Roman Yastrebov (Telegram: https://t.me/omasn)
License
Object handler is licensed under the MIT License - see the LICENSE
file for details