PHP code example of yeremi / schema-mapper

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

    

yeremi / schema-mapper example snippets


use Yeremi\SchemaMapper\Attributes\ApiSchema;

class User
{
    #[ApiSchema(key: 'first_name')]
    private string $firstName;

    #[ApiSchema(key: 'last_name')]
    private string $lastName;

    #[ApiSchema(key: 'email')]
    private string $email;

    // Getters
    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function getEmail(): string
    {
        return $this->email;
    }
}

use Yeremi\SchemaMapper\Normalizer\NormalizerInterface;

class MyUseCase {
    public function __construct(
        NormalizerInterface $normalizer
    ) {}
    
    public function fetchSomeData(){
        $response = [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => '[email protected]'
        ];

        $user = $this->normalizer->normalize($response, User::class);
        echo $user->getFirstName(); // Outputs: John
    }
}