Usage

Basic usage

Here's an example of how the generator can be used in a very basic way:

(new \Authanram\Generators\Generator())
    ->withTemplate('first {{ second }} third {{ fourth }}')
    ->withInput(['second' => '2nd', 'fourth' => '4th'])
    ->get();

This will lead to the following output:

first 2nd third 4th

Available Methods

To make things easier, there are a few more methods available while using the generator.

withDescriptor

The method withDescriptor expects one parameter of type string, containing the fully qualified class name of a descriptor.

(new \Authanram\Generators\Generator())
    ->withDescriptor(\Project\Descriptors\DescriptorA::class)
    // ...

In short, a descriptor contains the static methods path and fill as an alternative to the methods withInputPath and withFillCallback provided by the generator, to maintain reusability at this point.

To get in-depth information, head over to the section Descriptors.

withFillCallback

The method withFillCallback expects one parameter of type callable, used to transform the input before it will be used to replace the template-variables with. It will receive one parameter of type \Authanram\Generators\Input.

(new \Authanram\Generators\Generator())
    ->withTemplate('first {{ second }} third {{ fourth }}')
    ->withInput(['second' => '2nd', 'fourth' => '4TH'])
    ->withFillCallback(fn (\Authanram\Generators\Input $input) => [
        'second' => $input->str('second')->upper(),
        'fourth' => $input->str('fourth')->lower(),
    ])
    ->get();

This will lead to the following output:

first 2ND third 4th

To get in-depth information, head over to the section Fill Callback.

withInputPath

The method withInputPath expects one parameter of type string, defining the path the template should be read from.

(new \Authanram\Generators\Generator())
    ->withInputPath(__DIR__.'/../stubs/filename.stub')
    // ...

To get in-depth information, head over to the section Input Path.

withInput

The method withInput expects one parameter of type array<string, mixed>, defining the map used to replace the template-variables with.

(new \Authanram\Generators\Generator())
    ->withInput(['second' => '2nd', 'fourth' => '4th'])
    // ...

To get in-depth information, head over to the section Input.

withOutputPath

The method withOutputPath expects one parameter of type string, defining the path the generated result should be stored at.

(new \Authanram\Generators\Generator())
    ->withOutputPath(__DIR__.'/../generated/filename.php')
    // ...

To get in-depth information, head over to the section Output Path.

withPattern

The method withPattern expects one parameter of type string, representing the pattern to indicate template-variables.

(new \Authanram\Generators\Generator())
    ->withInput(['second' => '2nd', 'fourth' => '4th'])
    ->withTemplate('first ## second !! third ## fourth !!')
    ->withPattern('## %s !!')
    ->get();

This will lead to the following output:

first 2nd third 4th

To get in-depth information, head over to the section Pattern.

withPipes

The method withPipes expects one parameter of type array<string>, containing the fully qualified class names of the pipes your template will be passed through.

Pipes must implement Authanram\Generators\Contracts\Pipe.

use Authanram\Generators\Generator;
use Project\Pipes;

(new Generator())
    ->withPipes([
        ...Generator::PIPES,
        Pipes\CustomPipeA::class,
        Pipes\CustomPipeB::class,
    ])
    // ...

To get in-depth information, head over to the section Pipes.

withTemplate

The method withTemplate expects one parameter of type string, representing the expected result after generation, including the template-variables to be replaced.

(new \Authanram\Generators\Generator())
    ->withTemplate('first {{ second }} third {{ fourth }}')
    // ...

To get in-depth information, head over to the section Templates.