Lines 100.00% 6 / 6
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ConstraintNamer 100.00% 6 / 6 100.00% 1 / 1 5 100.00% 1 / 1
 derive 100.00% 6 / 6 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace BlueprintAU\Radiant\Database\Schema;
6
7/**
8 * The one constraint-name derivation — the shared naming convention.
9 */
10final class ConstraintNamer
11{
12    /**
13     * Derive a constraint name: `{table}_{columns}_{suffix}`.
14     *
15     * @param  string  $table
16     * @param  list<string>  $columns
17     * @param  string  $suffix
18     * @return string
19     * @throws \InvalidArgumentException
20     */
21    public static function derive(string $table, array $columns, string $suffix): string
22    {
23        if ($table === '' || $suffix === '' || $columns === [] || in_array('', $columns, true)) {
24            throw new \InvalidArgumentException(
25                'A derived constraint name requires a non-empty table, suffix, and columns; got '
26                . "table [{$table}], suffix [{$suffix}], columns [" . implode(', ', $columns) . '].'
27            );
28        }
29
30        return implode('_', [$table, ...$columns, $suffix]);
31    }
32}