@@ -27,7 +27,7 @@ private function __construct()
27
27
public static function init (PreferenceGroup $ name , CastableEnum $ cast = Cast::STRING ): static
28
28
{
29
29
$ builder = new PreferenceBuilder ();
30
- return $ builder ->withName ($ name )->withCast ($ cast );
30
+ return $ builder ->withName ($ name )->withCast ($ cast )-> nullable ( false ) ;
31
31
}
32
32
33
33
public static function delete (PreferenceGroup $ name ): int
@@ -82,6 +82,13 @@ public function withRule(ValidationRule $rule): static
82
82
return $ this ;
83
83
}
84
84
85
+ public function nullable (bool $ nullable = true )
86
+ {
87
+ $ this ->preference ->nullable = $ nullable ;
88
+ return $ this ;
89
+ }
90
+
91
+
85
92
public function create (): Preference
86
93
{
87
94
return $ this ->updateOrCreate ();
@@ -103,7 +110,7 @@ public function updateOrCreate(): Preference
103
110
/**
104
111
* @throws ValidationException
105
112
*/
106
- public static function initBulk (array $ preferences ): void
113
+ public static function initBulk (array $ preferences, bool $ nullable = false ): void
107
114
{
108
115
if (empty ($ preferences )) {
109
116
throw new InvalidArgumentException ("no preferences provided " );
@@ -113,6 +120,10 @@ public static function initBulk(array $preferences): void
113
120
if (empty ($ preferenceData ['cast ' ])) {
114
121
$ preferenceData ['cast ' ] = Cast::STRING ;
115
122
}
123
+ if (!array_key_exists ('nullable ' , $ preferenceData )) {
124
+ $ preferenceData ['nullable ' ] = $ nullable ;
125
+ }
126
+
116
127
if (empty ($ preferenceData ['name ' ]) || !($ preferenceData ['name ' ] instanceof PreferenceGroup)) {
117
128
throw new InvalidArgumentException (
118
129
sprintf ("index: #%s name is required and needs to be a PreferenceGroup " , $ key )
@@ -130,7 +141,12 @@ public static function initBulk(array $preferences): void
130
141
}
131
142
132
143
if (!empty ($ preferenceData ['default_value ' ])) {
133
- ValidationHelper::validateValue ($ preferenceData ['default_value ' ], $ preferenceData ['cast ' ], $ preferenceData ['rule ' ]);
144
+ ValidationHelper::validateValue (
145
+ $ preferenceData ['default_value ' ],
146
+ $ preferenceData ['cast ' ] ?? null ,
147
+ $ preferenceData ['rule ' ] ?? null ,
148
+ $ preferenceData ['nullable ' ],
149
+ );
134
150
}
135
151
136
152
@@ -159,6 +175,7 @@ public static function initBulk(array $preferences): void
159
175
'default_value ' => null ,
160
176
'description ' => '' ,
161
177
'rule ' => null ,
178
+ 'nullable ' => false ,
162
179
], $ preferenceData );
163
180
}
164
181
0 commit comments