Skip to content

The component allows filling objects from an associative array based on class strict property maps

License

Notifications You must be signed in to change notification settings

korvin3/object-handler

 
 

Repository files navigation

PHP - Object Handler

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

About

The component allows filling objects from an associative array based on class strict property maps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%