Lines 100.00% 5 / 5
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ForeignKeyAction 100.00% 5 / 5 100.00% 1 / 1 1 100.00% 1 / 1
 fromChecked 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace BlueprintAU\Radiant\Database\Schema\Enums;
6
7/**
8 * The referential actions a foreign key's `ON DELETE` / `ON UPDATE` clause
9 * can take.
10 */
11enum ForeignKeyAction: string
12{
13    /** Delete/update the child rows too. */
14    case Cascade = 'CASCADE';
15
16    /** Set the child columns to `NULL` (they must be nullable). */
17    case SetNull = 'SET NULL';
18
19    /** Refuse the delete/update (error, transaction-compatible). */
20    case Restrict = 'RESTRICT';
21
22    /** Refuse the delete/update (SQL-standard variant). */
23    case NoAction = 'NO ACTION';
24
25    /** Set the child columns to their declared default. */
26    case SetDefault = 'SET DEFAULT';
27
28    /**
29     * Resolve a string to a case, failing fast on anything else.
30     *
31     * @param  string  $action
32     * @return self
33     * @throws \InvalidArgumentException
34     */
35    public static function fromChecked(string $action): self
36    {
37        $normalized = strtoupper(str_replace(['-', '_'], ' ', trim($action)));
38
39        return self::tryFrom($normalized)
40            ?? throw new \InvalidArgumentException(
41                "A foreign key action must be one of CASCADE, SET NULL, RESTRICT, NO ACTION, or SET DEFAULT; got [{$action}]."
42            );
43    }
44}