iqomp / class-builder
基于结构化数组创建PHP类/接口内容
1.5.0
2024-07-16 04:36 UTC
Requires (Dev)
README
基于结构化数组创建PHP类/接口内容
安装
composer require iqomp/class-builder
使用方法
此模块创建了一个名为 Iqomp\ClassBuilder\Builder
的新库,可以用于根据结构化数组生成PHP类文件内容
<?php use Iqomp\ClassBuilder\Builder; $structure = [ /* ... */ ]; $result = Builder::build($structure);
数据结构
以下是迄今为止已知完整的数组数据结构列表
$structure = [ // the file comments 'comments' => [ 'My first library', '@package vendor/module', '@version 0.0.1' ], // the class comment 'class_comments' => [ '@RpcService(name="ClassName")' ], 'namespace' => 'Vendor\\Module', 'type' => 'class', // interface 'name' => 'ClassName', 'extends' => [ '\\Other\\Module\\Class', // may add more for interface ], 'implements' => [ '\\Other\\Module\\Iface', // may add more ], 'properties' => [ 'first' => [ 'static' => false, 'visibility' => 'public', 'type' => 'string', 'attribute' => 'string', 'default' => null // remove to not set the default ], 'second' => [ 'visibility' => 'protected', 'type' => '\\Other\\Module\\Class' ] ], 'methods' => [ 'getOne' => [ 'static' => false, 'visibility' => 'public', 'return' => '?object', 'attribute' => 'string', 'arguments' => [ 'first' => [ 'type' => 'int' ], 'second' => [ 'type' => 'bool', 'default' => 0 ] ], 'content' => 'return false;', 'comment' => [ '@param first int first arguments', '@param second bool second arguments', '@return bool' ] ] ], 'uses' => [ 'App\\Library\\Class' => 'XClass', 'App\\Library\\Awesome' => null ] ];
基于上述数据结构,调用 Builder::build
将得到以下结果,包括 <?php
行
<?php /** * My first library * @package vendor/module * @version 0.0.1 */ namespace Vendor\Module; use App\Library\Class as XClass; use App\Library\Awesome; /** * @RpcService(name="ClassName") */ class ClassName extends \Other\Module\Class implements \Other\Module\Iface { #[string] public string $first = null; protected \Other\Module\Class $second; /** * @param first int first arguments * @param second bool second arguments * @return bool */ #[string] public function getOne (int $first, bool $second = 0): ?object { return false; } }
待办事项
- 使用
use
类来简化使用的类。