summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/attributes/References.php
blob: 8271ebb498455f389c5f78a636886f64b79e254b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

namespace mystic\forum\attributes;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class References {
    public const NO_ACTION = 0;
    public const CASCADE = 1;
    public const SET_NULL = 2;
    public const SET_DEFAULT = 3;

    public function __construct(
        public readonly string $foreignTableName,
        public readonly ?string $foreignColumnName = null,
        public readonly int $onDelete = self::NO_ACTION,
    ) {}

    public function __toString(): string {
        return $this->foreignTableName
            . ($this->foreignColumnName !== null ? " ({$this->foreignColumnName})" : "")
            . ([
                    self::NO_ACTION => "",
                    self::CASCADE => " ON DELETE CASCADE",
                    self::SET_NULL => " ON DELETE SET NULL",
                    self::SET_DEFAULT => " ON DELETE SET DEFAULT",
               ][$this->onDelete] ?? "");
    }
}