omasn/object-handler

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

Installs: 8 029

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 2

Open Issues: 1

pkg:composer/omasn/object-handler

4.3.1 2023-03-30 13:07 UTC

This package is auto-updated.

Last update: 2025-09-29 02:50:06 UTC


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