PHP code example of isq / maio

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

    

isq / maio example snippets


use ISQ\MAIO\MergeArrayIntoObject;

class CreateUserDTO {
	public readonly string $firstName;
	public readonly string $lastName;
}

$data = [
	'firstName' => 'Foo',
	'lastName' => 'Baz'
];

$maio = MergeArrayIntoObject::getInstance();
$createUserDTO = $maio->merge(new CreateUserDTO, $data);

echo $createUserDTO::class; // CreateUserDTO
echo $createUserDTO->firstName; // Foo
echo $createUserDTO->lastName; // Baz

use ISQ\MAIO\MergeArrayIntoObject;
use ISQ\MAIO\Attributes\Key;

class CreateUserDTO {
	public readonly string $firstName;
	public readonly string $lastName;
	#[Key('user_age')]
	public readonly int $age;
	#[Key('address.postal_code')]
	public readonly int $postalCode;
}

$data = [
	'firstName' => 'Foo',
	'lastName' => 'Baz',
	'user_age' => 34,
	'address' => [
		'postal_code' => 12312311
	]
];

$maio = MergeArrayIntoObject::getInstance();
$createUserDTO = $maio->merge(new CreateUserDTO, $data);

echo $createUserDTO::class; // CreateUserDTO
echo $createUserDTO->firstName; // Foo
echo $createUserDTO->lastName; // Baz
echo $createUserDTO->age; // 34
echo $createUserDTO->postalCode; // 12312311

use ISQ\MAIO\MergeArrayIntoObject;
use ISQ\MAIO\Attributes\Call;

class MyHelper {
	public static function add(int $value): int
	{
		return $value + 1;
	}

	public function toUpperCase(string $value): string
	{
		return strtoupper($value);
	}
}

class CreateUserDTO {
	#[Call(MyHelper::class, 'add')]
	public readonly int $id;

	#[Call('strtoupper')]
	public readonly string $firstName;

	#[Call(new MyHelper, 'toUpperCase')]
	public readonly string $lastName;
}

$data = [
	'id' => 0,
	'firstName' => 'Foo',
	'lastName' => 'Baz'
];

$maio = MergeArrayIntoObject::getInstance();
$createUserDTO = $maio->merge(new CreateUserDTO, $data);

echo $createUserDTO::class; // CreateUserDTO
echo $createUserDTO->id; // 1
echo $createUserDTO->firstName; // FOO
echo $createUserDTO->lastName; // BAZ

use ISQ\MAIO\MergeArrayIntoObject;
use ISQ\MAIO\Attributes\ArrayOf;

class User {
	public readonly string $id;
}

class UsersDTO {
	#[ArrayOf(User::class)]
	public readonly array $data;
}

$data = [
	'data' => [
		['id' => 1],
		['id' => 2],
		['id' => 3],
		['id' => 4]
	]
];

$maio = MergeArrayIntoObject::getInstance();
$usersDTO = $maio->merge(new CreateUserDTO, $data);

echo $createUserDTO::class; // UsersDTO
echo $usersDTO[0]::class; // User
echo $usersDTO[0]->id; // 1
echo $usersDTO[1]->id; // 2
echo $usersDTO[2]->id; // 3
echo $usersDTO[3]->id; // 4