efureev/laravel-support-db

View on GitHub
src/Schema/Postgres/Compilers/UniqueCompiler.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Php\Support\Laravel\Database\Schema\Postgres\Compilers;

use Php\Support\Laravel\Database\Schema\Postgres\Blueprint;
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\Unique\UniqueBuilder;
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\Unique\UniquePartialBuilder;
use Php\Support\Laravel\Database\Schema\Postgres\Grammar;

class UniqueCompiler
{
    use WheresBuilder;

    public static function compile(
        Grammar $grammar,
        Blueprint $blueprint,
        UniqueBuilder $fluent,
        UniquePartialBuilder $command
    ): string {
        $wheres = static::build($grammar, $blueprint, $command);

        return sprintf(
            'CREATE UNIQUE INDEX %s ON %s (%s) WHERE %s',
            $fluent->get('index'),
            $blueprint->getTable(),
            implode(',', (array)$fluent->get('columns')),
            static::removeLeadingBoolean(implode(' ', $wheres))
        );
    }
}