PHPackages                             psx/schema - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. psx/schema

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

psx/schema
==========

Parse and generate data schema formats

v7.7.1(1mo ago)57238.7k—4.2%1216Apache-2.0PHPPHP &gt;=8.1CI passing

Since Apr 2Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/apioo/psx-schema)[ Packagist](https://packagist.org/packages/psx/schema)[ Docs](https://phpsx.org)[ Fund](https://www.paypal.me/fusioapi)[ GitHub Sponsors](https://github.com/chriskapp)[ RSS](/packages/psx-schema/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (28)Versions (244)Used By (16)

Schema
======

[](#schema)

This library helps you to work with fully typed objects, it provides the following features:

- Transform raw JSON data into fully typed objects
- Parse PHP classes into a [TypeSchema](https://typeschema.org/) specification
- Generate DTOs in different languages like TypeScript, Java, C# etc.

We provide also a hosted version of this [code generator](https://typeschema.org/generator). For more integration options you can also take a look at the [SDKgen](https://sdkgen.app/) project which provides a CLI binary or GitHub action to integrate the code generator.

Object mapper
-------------

[](#object-mapper)

This example reads raw JSON data and transform it into the provided `Person` class.

```
$json = getFirstName());
assert('Beethoven' === $person->getLastName());
assert(254 === $person->getAge());

$json = $objectMapper->writeJson($person);
```

Besides a simple class there are multiple ways to specify a source, for example to parse an array of persons s.

```
$json = getFirstName());

$json = $objectMapper->writeJson($person);
```

Code generation
---------------

[](#code-generation)

It is possible to transform any DTO class into a [TypeSchema](https://typeschema.org/) specification. This schema can then be used to generate DTOs in different languages which helps to work with type-safe objects across different environments.

```
$schemaManager = new SchemaManager();
$factory = new GeneratorFactory();

$schema = $schemaManager->getSchema(Person::class);

$generator = $factory->getGenerator(GeneratorFactory::TYPE_JAVA, Config::of('org.typeschema.model'));

$result = $generator->generate();

$result->writeTo('/my_model.zip');
```

TypeSchema specification
------------------------

[](#typeschema-specification)

It is possible to transform an existing [TypeSchema](https://typeschema.org/) specification into a PHP DTO class. For example lets take a look at the following specification, which describes a person:

```
{
  "definitions": {
    "Person": {
      "type": "struct",
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "age": {
          "description": "Age in years",
          "type": "integer"
        }
      }
    }
  },
  "root": "Person"
}
```

It is then possible to turn this specification into a ready-to-use PHP class s.

```
$schemaManager = new SchemaManager();
$factory = new GeneratorFactory();

$schema = $schemaManager->getSchema(__DIR__ . '/my_schema.json');

$generator = $factory->getGenerator(GeneratorFactory::TYPE_PHP, Config::of('App\\Model'));

$result = $generator->generate();

foreach ($result as $file => $code) {
    file_put_contents(__DIR__ . '/' . $file, '
