定制 cedriccourteau/variant 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

cedriccourteau/variant

最新稳定版本:v0.1

Composer 安装命令:

composer require --dev cedriccourteau/variant

包简介

A simple ValueObject and ResultType generator for PHP

README 文档

README

Variant

A simple ValueObject and ResultType generator for PHP

About The Project

Screenshot

Creating Value Objects or Result Types in PHP can be time-consuming compared to creating custom types in other languages. Variant aims to simplify this process by providing a custom, easy-to-use language for prototyping them.

Here's an example of how a type can be generated with Variant:

type UserKind {
	User(string name, int companyId)
	Moderator(string name, int companyId)
	Admin(string name)
}

type UpdateError {
	Unauhtorized
	NotFound
}

record Company(string name, int id)

result UpdateCompanyResult(Company, UpdateError)

Getting Started

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

Installation

composer require cedriccourteau/variant

Usage

  1. Add a file with the .variant extension the desired folder, such as src/Users/Types/users.variant
  2. Define your types in this file
  3. Launch php ./vendor/bin/variant
  4. Voilà

Syntax of .variant files

Overview

type UserKind {
    User(string name, int companyId)
    Moderator(string name, int companyId)
    Admin(string name)
}

type GetUserError{
    NotFound
    InvalidCredentials
    Unauthorized
    BusyDB
}

result GetUserResult(UserKind, GetUserError)

Available instructions

record

Hold a single ValueObject with custom constructor. Everything between the parenthesis are like in php but without the $ for param declaration.

record <NAME>(<TYPE> <ARG_NAME>, etc)

type

It encapsulates records into a global type, translated into a Interface. Between the {} you add custom variants for this type

type <NAME> {
  <VARIANT_NAME>
  <VARIANT_NAME>(<TYPE> <ARG_NAME>, ...)
}

result

Create a custom Result type with an Ok value and an Error type.

result <RESULT_NAME>(<OK_TYPE>, <ERROR_TYPE>)

If the ERROR_TYPE is a defined type in the current .variant file, it binds the type to a type union of all of its variants.

Example:

type GetUserError{
    NotFound
    InvalidCredentials
    Unauthorized
    BusyDB
}

# It will translate GetUserError onto NotFound|InvalidCredentials|Unauthorized|BusyDB
result GetUserResult(UserKind, GetUserError)

Cli options

Options:
  --force          Force mode; recreate files even if they exist.
  --path=<path>    Specify the path to search in (default: src/).
  --namespace=<ns> Specify the namespace to use.
  --help           Display this help message.

Contributing

Contributions are welcome! Feel free to fork, use, and improve the project.

License

Distributed under the MIT License. Fork it, use it, make it better.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-09-26