apie/schema-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

apie/schema-generator

Composer 安装命令:

composer require apie/schema-generator

包简介

Composer package of the apie library: schema generator

README 文档

README

schema-generator

Latest Stable Version Total Downloads Latest Unstable Version License PHP Composer

PHP Composer

This package is part of the Apie library. The code is maintained in a monorepo, so PR's need to be sent to the monorepo

Documentation

The schema generator creates a JSON Schema from an object with typehints. It supports entities, lists, hashmaps, value objects and DTO made for Apie. It returns objects made with the library cebe/php-openapi.

This library does not generate an entire OpenAPI schema, but instead it just creates the JSON schema section of all the objects.

Standard usage

In general you make multiple schemas for multiple objects with references. Because of that we basically create a Components section

Code example:

<?php
use Apie\Fixtures\Enums\Gender;
use Apie\CommonValueObjects\Ranges\DateTimeRange;
use Apie\SchemaGenerator\ComponentsBuilderFactory;

$factory = ComponentsBuilderFactory::createComponentsBuilderFactory();
// $schema = ['type' => 'enum', 'enum' => ['M', 'V']]
$schema = $factory->addCreationSchemaFor(Gender::class);
/**
 * $schema = [
 *     'type' => 'object',
 *     'properties' => [
 *          'start' => ['$ref' => '#/components/schemas/DateWithTimezone-post'],
 *          'end' => ['$ref' => '#/components/schemas/DateWithTimezone-post'],
 *     ]
 * ]
 */
$schema = $factory->addCreationSchemaFor(DateTimeRange::class);
// $components = ['mixed', 'Gender-post', 'DateTimeRange-post', 'DateWithTimezone-post']
$components = array_keys($factory->getComponents()->schemas);

DTO's

DTO's will be mapped as objects and all fields required unless it has the Optional attribute or a default value.

<?php
use Apie\Core\Attributes\Optional;
use Apie\Core\Dto\DtoInterface;

class ExampleDto implements DtoInterface {
    string $example;

    int $number = 42;

    #[Optional()]
    Gender $gender;
}

Will Result in this schema:

ExampleDto-post:
    required: ['example']
    properties:
        example:
            type: string
        number:
            type: number
        gender:
            $ref: "Gender-post"

Enums

Enums will be mapped with type string or int and all the values in the enum property.

Backed enums will use the value of the enum. Enums without values use the names and are always mapped as strings.

Entities

Entities will be mapped by reading the constructor arguments and all the methods starting with set or with.

Any constructor argument is considered a required option, unless it has a default value.

For all methods starting with set or with it expects the last argument to be the type needed. Other arguments are considered for contextual reasons (like being authenticated or the current locale).

Value Objects

It tries to figure out what value objects the object is. For example if it uses one of the standard traits it will map those to objects or strings or read the toNative() return typehint.

  • If it implements HasRegexValueObjectInterface, pattern is filled in.
  • If it implements StringValueObjectInterface, type is filled in as string and the format is the name of the class without namespace.
  • If the class uses the CompositeValueObject trait, it will be mapped as an object.

Complete customization

You can add SchemaMethod attribute to a class and add a static method class to specify an openAPI schema for a class.

<?php
use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
use Apie\Core\ValueObjects\IsStringValueObject;

#[SchemaMethod('getSchema')]
class Example implements StringValueObject {
    use IsStringValueObject;

    public static function getSchema(): array
    {
        return [
            'type' => 'string',
            'format' => 'password',
            'max' => 12,
        ];
    }
}

The method can return an instance of cebe\Openapi\Schema (you require cebe/php-openapi") or return an array of the schema.

统计信息

  • 总下载量: 7.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固