open-code-modeling / json-schema-to-php-ast
提供从JSON模式创建PhpParser节点访问器的工厂,例如值对象
0.5.4
2021-07-27 14:32 UTC
Requires
- php: ^7.4 || ^8.0
- open-code-modeling/json-schema-to-php: ^0.3.0 || 0.4.x-dev
- open-code-modeling/php-code-ast: ^0.12.0 || 0.13.x-dev
Requires (Dev)
- laminas/laminas-filter: ^2.9
- open-code-modeling/php-filter: ^0.1.1
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5.0
- prooph/php-cs-fixer-config: ^v0.4.0
- psalm/plugin-phpunit: ^0.15.0
- roave/security-advisories: dev-master
- vimeo/psalm: ^4.4
Suggests
- open-code-modeling/php-filter: For pre-configured filters for proper class / method / property names etc.
- 0.6.x-dev
- 0.5.x-dev
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.x-dev
- 0.4.0
- 0.3.x-dev
- 0.3.0
- 0.2.x-dev
- 0.2.1
- 0.2.0
- 0.1.x-dev
- 0.1.0
- dev-0.5.x-merge-up-into-0.6.x_gTYrRklu
- dev-0.5.x-merge-up-into-0.6.x_602edca860b1d9.95335338
- dev-feature/array-value-object
- dev-0.2.x-merge-up-into-0.3.x_5fb7df24d250b5.63854803
- dev-master
This package is auto-updated.
Last update: 2024-08-24 18:59:28 UTC
README
通过PHP AST将JSON模式编译为PHP类/值对象。
安装
$ composer require open-code-modeling/json-schema-to-php-ast --dev
使用
请参阅“tests”文件夹中的单元测试以获取全面示例。
您可以使用每个值对象工厂将PHP AST节点访问器或高级构建器API与您的值对象组合。使用此库的最简单方法是使用ValueObjectFactory
。
假设您有以下JSON模式
{ "$schema": "https://json-schema.fullstack.org.cn/draft-07/schema#", "definitions": { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": ["string", "null"] }, "federal_state": { "$ref": "#/definitions/state" } }, "required": [ "street_address", "city", "federal_state" ] }, "state": { "type": "string", "enum": ["NY", "DC"] } }, "type": "object", "properties": { "billing_address": { "$ref": "#/definitions/address" }, "shipping_addresses": { "type": "array", "items": { "$ref": "#/definitions/address" } } }, "required": [ "billing_address" ] }
然后您可以使用ValueObjectFactory
生成以下类的PHP代码
订单
配送地址
地址
街道地址
城市
州
<?php use OpenCodeModeling\CodeAst\Builder\FileCollection; use OpenCodeModeling\CodeAst\Package\ClassInfoList; use OpenCodeModeling\CodeAst\Package\Psr4Info; use OpenCodeModeling\Filter\FilterFactory; use OpenCodeModeling\JsonSchemaToPhp\Type\Type; use OpenCodeModeling\JsonSchemaToPhpAst\ValueObjectFactory; $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7); $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); // configure your Composer info // FilterFactory of library open-code-modeling/php-filter is used for sake of brevity $classInfoList = new ClassInfoList( ...Psr4Info::fromComposer( 'src/', file_get_contents('composer.json'), FilterFactory::directoryToNamespaceFilter(), FilterFactory::namespaceToDirectoryFilter(), ) ); $valueObjectFactory = new ValueObjectFactory( $classInfoList, $parser, $printer, true, FilterFactory::classNameFilter(), FilterFactory::propertyNameFilter(), FilterFactory::methodNameFilter(), FilterFactory::constantNameFilter(), FilterFactory::constantValueFilter() ); // $json contains the json string from above $decodedJson = \json_decode($json, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); $typeSet = Type::fromDefinition($decodedJson); $srcFolder = 'tmp/'; $fileCollection = FileCollection::emptyList(); $classBuilder = ClassBuilder::fromScratch('Order', 'YourNamespaceFromComposer')->setFinal(true); $valueObjectFactory->generateClasses($classBuilder, $fileCollection, $typeSet, $srcFolder); // $fileCollection contains 6 classes // now let's add constants and getter methods of properties for non value objects $valueObjectFactory->addGetterMethodsForProperties($fileCollection, true); $valueObjectFactory->addClassConstantsForProperties($fileCollection); // generate PHP code $files = $valueObjectFactory->generateFiles($fileCollection); foreach ($files as $filename => $code) { // store PHP code to filesystem }