iqomp/class-builder

Create PHP class/interface content based on structured array

1.4.1 2021-11-18 06:02 UTC

This package is auto-updated.

Last update: 2024-04-18 11:03:42 UTC


README

Create PHP class/interface content based on structured array

Installation

composer require iqomp/class-builder

Usage

This module create new library named Iqomp\ClassBuilder\Builder that can be used to generate php class file content based on structured array:

<?php

use Iqomp\ClassBuilder\Builder;

$structure = [ /* ... */ ];
$result = Builder::build($structure);

Data Structure

Below is a complete list of array data structure known so far:

$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',
            'default' => null // remove to not set the default
        ],
        'second' => [
            'visibility' => 'protected',
            'type' => '\\Other\\Module\\Class'
        ]
    ],
    'methods' => [
        'getOne' => [
            'static' => false,
            'visibility' => 'public',
            'return' => '?object',
            '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
    ]
];

Based on above data structure, calling Builder::build will result as below include the <?php line:

<?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
{
    public string $first = null;

    protected \Other\Module\Class $second;

    /**
     * @param first int first arguments
     * @param second bool second arguments
     * @return bool
     */
    public function getOne (int $first, bool $second = 0): ?object
    {
        return false;
    }
}

TODO

  1. Use use class to short used classes.