Skip to content

Data

Data schemas are generated automatically based on the actual data classes, Sugoi provides a wide range of attributes allowing you to customize the output.

In addition to (de)serialization, contracts and validation, these schemas are also used to auto-generate documentation for your entire API with OpenAPI compliant output by default.

TIP

All data objects are assumed immutable by default. The base Data class is a convenience, not a requirement if you'd rather use a readonly class.

src/Data/Project.php

php
#[Schema]
readonly class Project
{
    public function __construct(
        #[Property(example: 1, required: true, access: 'read-only')]
        public ?int      $id,

        #[Property(example: 'Sugoi Cloud')]
        public string    $name,

        #[Property(example: 'A type-safe PHP framework for the cloud-native future!')]
        public string    $description,

        #[PropertyAlias('demo_url')]
        #[Property(example: 'https://sugoi.cloud')]
        public ?string   $demoUrl,

        #[PropertyDateTime]
        public ?DateTime $createdAt,
    )
    {
    }
}

Your data objects do not require attributes to work, schemas will be inferred on the fly depending on the context, the attributes are primarily used for overriding defaults or adding more explicit information.