/
GuardsAttributes.ts
157 lines (138 loc) · 4.05 KB
/
GuardsAttributes.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import CastsAttributes from './CastsAttributes';
import type { Attributes, AttributeKeys } from './HasAttributes';
export default class GuardsAttributes extends CastsAttributes {
/**
* The attributes that are mass assignable.
*
* @protected
*
* @type {string[]}
*/
protected fillableAttributes = this.fillable;
/**
* The attributes that aren't mass assignable
*
* @protected
*
* @type {string[]}
*/
protected guardedAttributes = this.guarded;
/**
* The attributes that are mass assignable.
*
* @type {string[]}
*/
public get fillable(): string[] {
return [];
}
/**
* The attributes that are not mass assignable.
*
* @type {string[]}
*/
public get guarded(): string[] {
return ['*'];
}
/**
* Get the guarded attributes for the model.
*
* @return {string[]}
*/
public getGuarded(): string[] {
return this.guardedAttributes;
}
/**
* Get the fillable attributes for the model.
*
* @return {string[]}
*/
public getFillable(): string[] {
return this.fillableAttributes;
}
/**
* Merge new fillable attributes with existing fillable attributes on the model.
*
* @param {string[]} fillable
*
* @return {this}
*/
public mergeFillable(fillable: (AttributeKeys<this> | string)[]): this;
public mergeFillable(fillable: string[]): this {
this.fillableAttributes = [...this.getFillable(), ...fillable] as string[];
return this;
}
/**
* Merge new guarded attributes with existing guarded attributes on the model.
*
* @param {string[]} guarded
*
* @return {this}
*/
public mergeGuarded(guarded: (AttributeKeys<this> | string)[]): this;
public mergeGuarded(guarded: string[]): this {
this.guardedAttributes = [...this.getGuarded(), ...guarded];
return this;
}
/**
* Set the fillable attributes for the model.
*
* @param {string[]} fillable
*
* @return {this}
*/
public setFillable(fillable: (AttributeKeys<this> | string)[]): this;
public setFillable(fillable: string[]): this {
this.fillableAttributes = fillable;
return this;
}
/**
* Set the guarded attributes for the model.
*
* @param {string[]} guarded
*
* @return {this}
*/
public setGuarded(guarded: (AttributeKeys<this> | string)[]): this;
public setGuarded(guarded: string[]): this {
this.guardedAttributes = guarded;
return this;
}
/**
* Determine if the given attribute may be mass assignable.
*
* @param {string} key
*/
public isFillable(key: AttributeKeys<this> | string): boolean;
public isFillable(key: string): boolean {
return this.getFillable().includes(key) || this.getFillable().includes('*');
}
/**
* Determine if the given attribute may not be mass assignable.
*
* @param {string} key
*/
public isGuarded(key: AttributeKeys<this> | string): boolean;
public isGuarded(key: string): boolean {
// if key is defined in both guarded and fillable, then fillable takes priority.
return (this.getGuarded().includes(key) || this.getGuarded().includes('*')) && !this.isFillable(key);
}
/**
* Get the fillable attributes from the given object.
*
* @param {object} attributes
*
* @return {object}
*/
protected getFillableFromObject(attributes: Attributes): Partial<Attributes> {
const fillable: Attributes = {};
if (this.getFillable().includes('*')) {
return attributes;
}
for (const [name, value] of Object.entries(attributes)) {
if (!this.isGuarded(name)) {
fillable[name] = value;
}
}
return fillable;
}
}