psx / schema
Parse and generate data schema formats
Fund package maintenance!
chriskapp
Patreon
www.paypal.me/fusioapi
Installs: 188 419
Dependents: 16
Suggesters: 0
Security: 0
Stars: 56
Watchers: 6
Forks: 11
Open Issues: 0
Requires
- php: >=8.1
- nikic/php-parser: ^4.0|^5.0
- phpdocumentor/type-resolver: ^1.0
- psr/cache: ^1.0|^2.0|^3.0
- psx/datetime: ^3.0
- psx/http: ^4.0
- psx/json: ^3.0
- psx/record: ^3.0
- psx/uri: ^3.0
- psx/validate: ^2.0
- symfony/cache: ^5.0|^6.0|^7.0
- symfony/console: ^5.0|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- symfony/yaml: ^5.0|^6.0|^7.0
- vimeo/psalm: ^5.0
- dev-master
- v7.2.7
- v7.2.6
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.9
- v7.1.8
- v7.1.7
- v7.1.6
- v7.1.5
- v7.1.4
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.9
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- 6.x-dev
- v6.4.2
- v6.4.1
- v6.4.0
- v6.3.9
- v6.3.8
- v6.3.7
- v6.3.6
- v6.3.5
- v6.3.4
- v6.3.3
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.7
- v6.2.6
- v6.2.5
- v6.2.4
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.9
- v6.1.8
- v6.1.7
- v6.1.6
- v6.1.5
- v6.1.4
- v6.1.3
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.9
- v6.0.8
- v6.0.7
- v6.0.6
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- 5.x-dev
- v5.2.1
- v5.2.0
- v5.1.9
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.4
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.9
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- 4.x-dev
- v4.2.8
- v4.2.7
- v4.2.6
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.x-dev
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-RC9
- v3.0.0-RC8
- v3.0.0-RC7
- v3.0.0-RC6
- v3.0.0-RC5
- v3.0.0-RC4
- v3.0.0-RC3
- v3.0.0-RC2
- v3.0.0-RC1
- 2.x-dev
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-feat/typeschema-next
This package is auto-updated.
Last update: 2024-11-17 22:07:23 UTC
README
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 specification
- Generate DTOs in different languages like TypeScript, Java, C# etc.
We provide also a hosted version of this code generator. For more integration options you can also take a look at the SDKgen project which provides a CLI binary or GitHub action to integrate the code generator.
Object mapper
This example reads raw JSON data and transform it into the provided Person
class.
$json = <<<'JSON' { "firstName": "Ludwig", "lastName": "Beethoven", "age": 254 } JSON; $objectMapper = new ObjectMapper(new SchemaManager()); $person = $objectMapper->readJson($json, SchemaSource::fromClass(Person::class)); assert('Ludwig' === $person->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 = <<<'JSON' [ { "firstName": "Ludwig", "lastName": "Beethoven", "age": 254 } ] JSON; $objectMapper = new ObjectMapper(new SchemaManager()); $personList = $objectMapper->readJson($json, SchemaSource::fromType('array<Person>')); assert(1 === count($personList)); assert('Ludwig' === $personList[0]->getFirstName()); $json = $objectMapper->writeJson($person);
Code generation
It is possible to transform any DTO class into a TypeSchema 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
It is possible to transform an existing TypeSchema 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, '<?php' . "\n" . $code); }
This would result in the following PHP class:
<?php declare(strict_types = 1); class Person implements \JsonSerializable, \PSX\Record\RecordableInterface { protected ?string $firstName = null; protected ?string $lastName = null; #[Description("Age in years")] 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(); } }
Every generated PHP class implements also the JsonSerializable
interface so you can simply encode an object to json.
$person = new Person(); $person->setFirstName('foo'); $person->setLastName('bar'); $person->setAge(32); echo json_encode($person); // would result in // {"firstName": "foo", "lastName": "bar", "age": 32}
Attributes
The following attributes are available: