Skip to content
On this page

Statement

Statements can be thought of as child generators. They are used to generate code for a specific part of the application. For example, the RouteRedirectStatement would generate code for a route redirect.

Interface

php
<?php

declare(strict_types=1);

namespace BaseCodeOy\Arch\Contract;

interface StatementInterface
{
    public function code(array $context = []): string;

    public function test(array $context = []): string;

    public function imports(array $context = []): array;

    public function traits(array $context = []): array;
}

Example

php
<?php

declare(strict_types=1);

namespace BaseCodeOy\Arch\Statement;

use BaseCodeOy\Arch\Renderer\FileRenderer;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redirect;

final readonly class RouteRedirectStatement implements StatementInterface
{
    public function __construct(
        private string $name,
        private ?array $parameters = [],
    ) {
        //
    }

    public function code(array $context = []): string
    {
        $hasParameters = \count($this->parameters) > 0;

        return \sprintf(
            $hasParameters ? "return Redirect::route('%s', %s);" : "return Redirect::route('%s');",
            $this->name,
            $hasParameters ? Arr::propertiesToArray($this->parameters) : '',
        );
    }

    public function test(array $context = []): string
    {
        return FileRenderer::render('redirect/test/route', [
            'name' => $this->name,
        ]);
    }

    public function imports(array $context = []): array
    {
        return [
            Redirect::class,
        ];
    }

    public function traits(array $context = []): array
    {
        return [];
    }
}

References