psx/schema
Composer 安装命令:
composer require psx/schema
包简介
Parse and generate data schema formats
README 文档
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:
| Attribute | Target | Example |
|---|---|---|
| Deprecated | Property | #[Deprecated(true)] |
| DerivedType | Class | #[DerivedType(Person::class, 'person')] |
| Description | Class/Property | #[Description("content")] |
| Discriminator | Class | #[Discriminator('type')] |
| Exclude | Property | #[Exclude] |
| Format | Property | #[Format('date-time')] |
| Key | Property | #[Key('my-complex-name')] |
| Nullable | Property | #[Nullable(true)] |
psx/schema 适用场景与选型建议
psx/schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 244.43k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2016 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「json」 「schema」 「typeschema」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psx/schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psx/schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psx/schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Kinikit - PHP Application development framework MVC component
Extension for Opis JSON Schema
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
ext-json wrapper with sane defaults
统计信息
- 总下载量: 244.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 14
- 依赖项目数: 17
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-03-31