PHP Integration

DTO

<?php

declare(strict_types = 1);

namespace TypeSchema\DTO;

use PSX\Schema\Attribute\Description;

#[Description('A simple student struct')]
class Student implements \JsonSerializable, \PSX\Record\RecordableInterface
{
    protected ?string $firstName = null;
    protected ?string $lastName = null;
    protected ?int $age = null;
    public function setFirstName(?string $firstName) : void
    {
        $this->firstName = $firstName;
    }
    public function getFirstName() : ?string
    {
        return $this->firstName;
    }
    public function setLastName(?string $lastName) : void
    {
        $this->lastName = $lastName;
    }
    public function getLastName() : ?string
    {
        return $this->lastName;
    }
    public function setAge(?int $age) : void
    {
        $this->age = $age;
    }
    public function getAge() : ?int
    {
        return $this->age;
    }
    public function toRecord() : \PSX\Record\RecordInterface
    {
        /** @var \PSX\Record\Record<mixed> $record */
        $record = new \PSX\Record\Record();
        $record->put('firstName', $this->firstName);
        $record->put('lastName', $this->lastName);
        $record->put('age', $this->age);
        return $record;
    }
    public function jsonSerialize() : object
    {
        return (object) $this->toRecord()->getAll();
    }
}

Integration

<?php

require_once __DIR__ . '/vendor/autoload.php';

$data = json_decode(file_get_contents(__DIR__ . '/input.json'));

$objectMapper = new \PSX\Schema\ObjectMapper();

$student = $objectMapper->readJson($data, \PSX\Schema\SchemaSource::fromClass(Student::class));

file_put_contents(__DIR__ . '/output.json', \json_encode($student));