PHP code example of omasn / object-handler

1. Go to this page and download the library: Download omasn/object-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

omasn / object-handler example snippets




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)
//   }