PHP code example of stougeiro / immutable

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

    

stougeiro / immutable example snippets


$data = Immutable::fromArray([
    'user' => [
        'name' => 'Sidney',
        'email' => '[email protected]',
    ],
]);

echo $data->user->name; // "Sidney"
echo $data->user->email; // "[email protected]"

$data = Immutable::fromArray([
    'roles' => ['admin', 'editor', 'viewer'],
]);

echo $data->roles[0]; // "admin"
echo $data->roles[1]; // "editor"
echo $data->roles[2]; // "viewer"

$data = Immutable::fromArray([
    'user' => [
        'name' => 'Sidney',
        'roles' => ['admin', 'editor'],
    ],
]);

$array = $data->user->toArray();

print_r($array); /*
[
    'name' => 'Sidney',
    'roles' => ['admin', 'editor'],
] */

$response = [
    'user' => [
        'id' => 10,
        'name' => 'Sidney',
        'roles' => ['admin', 'editor'],
        'addresses' => [
            [
                'type' => 'home',
                'street' => 'Rua das Flores, 123',
                'city' => 'São Paulo',
                'zip' => '01000-000',
            ],
            [
                'type' => 'work',
                'street' => 'Av. Central, 456',
                'city' => 'Rio de Janeiro',
                'zip' => '20000-000',
            ],
        ],
    ],
];

$data = Immutable::fromArray($response);

// Safe access
echo $data->user->name; // "Sidney"
echo $data->user->addresses[1]->city; // "Rio de Janeiro"

// Convert back to array for output
return json_encode($data->toArray());

class UserDTO
{
    public function __construct(
        public Immutable $data
    ) {}
}

$userDto = new UserDTO(
    Immutable::fromArray([
        'user' => [
            'name' => 'Sidney',
            'roles' => ['admin', 'editor'],
            'addresses' => [
                [
                    'type' => 'home',
                    'street' => 'Rua das Flores, 123',
                    'city' => 'São Paulo',
                    'zip' => '01000-000',
                ],
                [
                    'type' => 'work',
                    'street' => 'Av. Central, 456',
                    'city' => 'Rio de Janeiro',
                    'zip' => '20000-000',
                ],
            ],
        ],
    ])
);

// DTO usage
echo $userDto->data->user->roles[0]; // "admin"
echo $userDto->data->user->addresses[0]->zip; // "01000-000"

function fetchUserFromService(): Immutable
{
    $payload = externalService()->get('/user/10');

    return Immutable::fromArray($payload);
}

$data = fetchUserFromService();

// Safe deep access
$city = $data->user->addresses[0]->city; // "São Paulo"

// Safe isset chain
if (isset($data->user->addresses[1]->zip)) {
    // zip exists
    // "20000-000"
}

// Deep conversion for logging or debugging
log(print_r($data->toArray(), true));