VersionOptions::saveVersionTableForeignKeyTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace JPNut\Versioning;
4
5
class VersionOptions
6
{
7
    /**
8
     * @var string
9
     */
10
    public string $versionKeyName;
11
12
    /**
13
     * @var string|null
14
     */
15
    public ?string $versionTableName;
16
17
    /**
18
     * @var string
19
     */
20
    public string $versionTableKeyName;
21
22
    /**
23
     * @var string
24
     */
25
    public string $versionTableForeignKeyName;
26
27
    /**
28
     * @var string
29
     */
30
    public string $versionTableCreatedAtName;
31
32
    /**
33
     * @var array
34
     */
35
    public array $versionableAttributes;
36
37
    /**
38
     * @param array $options
39
     */
40
    public function __construct(array $options = [])
41
    {
42
        $this->versionKeyName = $options['versionKeyName'] ?? 'version';
43
        $this->versionTableName = $options['versionTableName'] ?? null;
44
        $this->versionTableKeyName = $options['versionTableKeyName'] ?? 'version';
45
        $this->versionTableForeignKeyName = $options['versionTableForeignKeyName'] ?? 'parent_id';
46
        $this->versionTableCreatedAtName = $options['versionTableCreatedAtName'] ?? 'created_at';
47
        $this->versionableAttributes = $options['versionableAttributes'] ?? [];
48
    }
49
50
    /**
51
     * @param array $options
52
     *
53
     * @return static
54
     */
55
    public static function create(array $options = []): self
56
    {
57
        return new static($options);
58
    }
59
60
    /**
61
     * @param string $fieldName
62
     *
63
     * @return self
64
     */
65
    public function saveVersionKeyTo(string $fieldName): self
66
    {
67
        $this->versionKeyName = $fieldName;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param string $table
74
     *
75
     * @return self
76
     */
77
    public function useVersionTable(string $table): self
78
    {
79
        $this->versionTableName = $table;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param string $fieldName
86
     *
87
     * @return self
88
     */
89
    public function saveVersionTableKeyTo(string $fieldName): self
90
    {
91
        $this->versionTableKeyName = $fieldName;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $fieldName
98
     *
99
     * @return self
100
     */
101
    public function saveVersionTableForeignKeyTo(string $fieldName): self
102
    {
103
        $this->versionTableForeignKeyName = $fieldName;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param string|null $fieldName
110
     *
111
     * @return self
112
     */
113
    public function saveVersionTableCreatedAtTo(?string $fieldName): self
114
    {
115
        $this->versionTableCreatedAtName = $fieldName;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param array $attributes
122
     *
123
     * @return self
124
     */
125
    public function setVersionableAttributes(array $attributes): self
126
    {
127
        $this->versionableAttributes = $attributes;
128
129
        return $this;
130
    }
131
}
132