PHP code example of nutgram / hydrator

1. Go to this page and download the library: Download nutgram/hydrator 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/ */

    

nutgram / hydrator example snippets


use SergiX44\Hydrator\Hydrator;

$hydrator = new Hydrator();

// create and hydrate an object with an array
$data = [/* the class props here */];
$object = $hydrator->hydrate(SomeDto::class, $data);

// hydrate an object with an array
$data = [/* the class props here */];
$hydrator->hydrate($object, $data);

// creates and hydrate an object with JSON
$json = '{...}';
$object = $hydrator->hydrateWithJson(SomeDto::class, $json);

// hydrate an object with JSON
$json = '{...}';
$hydrator->hydrateWithJson($object, $json);

// pass JSON decoding flags
$options = JSON_OBJECT_AS_ARRAY|JSON_BIGINT_AS_STRING;
$hydrator->hydrateWithJson($object, $json, $options);

public string $value;

public string $value = 'foo';

public ?string $value;

public ?string $value = null;

public bool $value;

['value' => true];
['value' => 'yes'];

public int $value;

['value' => 42];
['value' => '42'];

public float $value;

['value' => 42.0];
['value' => '42.0'];

public string $value;

['value' => 'foo'];

public array $value;

['value' => [1, 2, 'foo']];

final class SomeDto {
    public readonly string $value;
}

use SergiX44\Hydrator\Annotation\ArrayType;

#[ArrayType(SomeDto::class)]
public array $value;

[
    'value' => [
        [
            'value' => 'foo',
        ],
        [
            'value' => 'bar',
        ],
    ],
],

public object $value;

['value' => new stdClass];

public DateTimeImmutable $value;

// 2010-01-01
['value' => 1262304000];
// 2010-01-01
['value' => '1262304000'];
// normal date
['value' => '2010-01-01'];

public DateInterval $value;

['value' => 'P1Y']

enum SomeEnum: int {
    case foo = 0;
    case bar = 1;
}

public SomeEnum $value;

['value' => 0]
['value' => '1']

final class SomeDto {
    public string $value;
}

public SomeDto $value;

[
    'value' => [
        'value' => 'foo',
    ],
]

use SergiX44\Hydrator\Annotation\Alias;

final class RecaptchaVerificationResult {
    public bool $success;

    #[Alias('error-codes')]
    public array $errorCodes = [];
}

final class Product {
    public string $name;
    public Category $category;
    #[ArrayType(Tag::class)]
    public array $tags;
    public Status $status;
}

final class Category {
    public string $name;
}

final class Tag {
    public string $name;
}

enum Status: int {
    case ENABLED = 1;
    case DISABLED = 0;
}

$product = $hydrator->hydrate(Product::class, [
    'name' => 'Stool',
    'category' => [
        'name' => 'Furniture',
    ],
    'tags' => [
        [
            'name' => 'Wood',
        ],
        [
            'name' => 'Lacquered',
        ],
    ],
    'status' => 0,
]);