PHP code example of alexanevsky / output-normalizer-bundle

1. Go to this page and download the library: Download alexanevsky/output-normalizer-bundle 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/ */

    

alexanevsky / output-normalizer-bundle example snippets


class User
{
    private int $id = 1;

    private string $userName = 'John Doe';

    private string $skippedProperty = 'Lorem Ipsum';

    public function getId(): int
    {
        return $this->id;
    }

    public function getUserName(): string
    {
        return $this->userName;
    }

    public function getSkippedProperty(): string
    {
        return $this->skippedProperty;
    }
}

use Alexanevsky\OutputNormalizerBundle\Output\OutputInterface;

class UserOutput implements OutputInterface
{
    public int $id;

    public string $userName;
}

use Alexanevsky\OutputNormalizerBundle\OutputNormalizer;

public function __construct(
    private OutputNormalizer $outputNormalizer
) {}

$output = $this->outputNormalizer->normalize($user, UserOutput::class);

['id' => 1, 'user_name' => 'John Doe']

class User
{
    private string $phone = '8002752273';

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

class UserOutput implements OutputInterface
{
    public string $phone;
}

['phone' => '8002752273']

class UserOutput implements OutputInterface
{
    public string $phone;

    public function getPhone(): string
    {
        return '+1' . $this->phone;
    }
}

['phone' => '+18002752273']

use Alexanevsky\OutputNormalizerBundle\Output\OutputInterface;

class UserOutput implements OutputInterface
{
    private int $id;

    public function setId(int $id): void
    {
        $this->id = $id;
    }

    public function getId(): int
    {
        return $this->id;
    }
}

class Phone
{
    private string $prefix = '+1';

    private string $number = '8002752273';

    private string $country = 'USA';

    // Getters and setters of prefix, number and country...
}

class User
{
    private Phone $phone;
}

class UserOutput implements OutputInterface
{
    public Phone $phone;
}

['phone' => ['prefix' => '+1', 'number' => '8002752273', 'country' => 'USA']]

class PhoneOutput implements OutputInterface
{
    private string $prefix = '+1';

    private string $number = '8002752273';
}

class UserOutput implements OutputInterface
{
    public PhoneOutput $phone;
}

['phone' => ['prefix' => '+1', 'number' => '8002752273']]

class UserOutput implements OutputInterface
{
    public Phone $phone;

    public function getPhone(): string
    {
        return $this->phone->getPrefix() . $this->phone->getNumber();
    }
}

['phone' => '+18002752273']

use Alexanevsky\OutputNormalizerBundle\ObjectNormalizer\ObjectNormalizerInterface;

class PhoneNormalizer implements ObjectNormalizerInterface
{
    public function supports(object $object): bool
    {
        return $object instanceof Phone;
    }

    public function normalize(object $object): string
    {
        return $phone->getPrefix() . $phone->getNumber();
    }
}

class UserOutput implements OutputInterface
{
    public Phone $phone;
}

['phone' => '+18002752273']

class City
{
    private int $id = 1;

    private string $name = 'Los Angeles';

    // Getters and setters of id and name...
}

class User
{
    private City $city;

    public function getCity(): City
    {
        return $this->city;
    }
}

use Alexanevsky\OutputNormalizerBundle\Output\Attribute\EntityToId;

class UserOutput implements OutputInterface
{
    #[EntityToId]
    public City $city;
}

['city_id' => 1]

class User
{
    private Collection $cities;

    public function getCities(): Collection
    {
        return $this->cities;
    }
}

class UserOutput implements OutputInterface
{
    #[EntityToId]
    public Collection|array $cities;
}

['cities_ids' => [1]]

class Airport
{
    private string $code = 'LAX';

    private string $name = 'Los Angeles';

    // Getters and setters of code and name...
}

class User
{
    private Airport $airport;

    public function getAirport(): Airport
    {
        return $this->airport;
    }
}

class UserOutput implements OutputInterface
{
    #[EntityToId('code')]
    public Airport $airport;
}

['airport_code' => 'LAX']

class UserOutput implements OutputInterface
{
    #[EntityToId('code', 'identifier')]
    public Airport $airport;
}

['airport_identifier' => 'LAX']

class UserOutput implements OutputInterface
{
    #[EntityToId('code', false)]
    public Airport $airport;
}

['airport' => 'LAX']

class User
{
    private array $roles = ['ROLE_USER', 'ROLE_ADMIN'];

    // Getter and setter of roles
}

class UserOutput implements OutputInterface
{
    public array $roles;
}

use Alexanevsky\OutputNormalizerBundle\OutputModifier\OutputModifierInterface;

class UserOutputModifier extends OutputModifierInterface
{
    public function supports(object $output, object $source): bool
    {
        return $output instanceof UserOutput;
    }

    /**
     * @param UserOutput $output;
     */
    public function modify(OutputInterface $output, object $source): void
    {
        $output->roles = array_diff($output->roles, ['ROLE_USER']);
    }
}

['roles' => ['ROLE_ADMIN']]