@@ -199,54 +199,89 @@ const getAttributeBlocks = (
199
199
} ;
200
200
201
201
const getAttributeType = ( attr : Eloquent . Attribute ) : string => {
202
- const type = getActualType ( attr . cast || attr . type ) ;
202
+ const type = getActualType ( attr . cast , attr . type ) ;
203
203
204
- return attr . nullable ? `${ type } |null` : type ;
204
+ if ( attr . nullable && type !== "mixed" ) {
205
+ return `${ type } |null` ;
206
+ }
207
+
208
+ return type ;
209
+ } ;
210
+
211
+ type TypeMapping = Record < string , ( string | RegExp ) [ ] > ;
212
+
213
+ const castMapping : TypeMapping = {
214
+ array : [ "json" , "encrypted:json" , "encrypted:array" ] ,
215
+ int : [ "timestamp" ] ,
216
+ mixed : [ "attribute" , "accessor" , "encrypted" ] ,
217
+ object : [ "encrypted:object" ] ,
218
+ string : [ "hashed" ] ,
219
+ "\\Illuminate\\Support\\Carbon" : [ "date" , "datetime" ] ,
220
+ "\\Illuminate\\Support\\Collection" : [ "encrypted:collection" ] ,
205
221
} ;
206
222
207
- const mapType = ( type : string ) : string => {
208
- const mapping : Record < string , ( string | RegExp ) [ ] > = {
209
- bool : [
210
- "boolean(1)" ,
211
- "boolean(0)" ,
212
- "tinyint" ,
213
- "tinyint unsigned" ,
214
- "boolean" ,
215
- / t i n y i n t \( \d + \) / ,
216
- ] ,
217
- string : [
218
- "longtext" ,
219
- "mediumtext" ,
220
- "text" ,
221
- / v a r c h a r \( \d + \) / ,
222
- / c h a r \( \d + \) / ,
223
- ] ,
224
- float : [ / d o u b l e \( \d + \, \d + \) / ] ,
225
- int : [ "bigint" , "bigint unsigned" , "integer" , "int unsigned" ] ,
226
- mixed : [ "attribute" , "accessor" , "encrypted" ] ,
227
- array : [ "encrypted:json" , "encrypted:array" , "json" ] ,
228
- "\\Illuminate\\Support\\Carbon" : [ "datetime" , "timestamp" ] ,
229
- "\\Illuminate\\Support\\Collection" : [ "encrypted:collection" ] ,
230
- object : [ "encrypted:object" ] ,
231
- } ;
223
+ const typeMapping : TypeMapping = {
224
+ bool : [ / ^ b o o l e a n ( \( ( 0 | 1 ) \) ) ? $ / , / ^ t i n y i n t ( u n s i g n e d ) ? ( \( \d + \) ) ? $ / ] ,
225
+ float : [
226
+ "real" ,
227
+ "money" ,
228
+ "double precision" ,
229
+ / ^ ( d o u b l e | d e c i m a l | n u m e r i c ) ( \( \d + \, \d + \) ) ? $ / ,
230
+ ] ,
231
+ int : [ / ^ ( b i g ) ? s e r i a l $ / , / ^ ( s m a l l | b i g ) ? i n t ( e g e r ) ? ( u n s i g n e d ) ? $ / ] ,
232
+ resource : [ "bytea" ] ,
233
+ string : [
234
+ "box" ,
235
+ "cidr" ,
236
+ "inet" ,
237
+ "line" ,
238
+ "lseg" ,
239
+ "path" ,
240
+ "time" ,
241
+ "uuid" ,
242
+ "year" ,
243
+ "point" ,
244
+ "circle" ,
245
+ "polygon" ,
246
+ "interval" ,
247
+ / ^ j s o n ( b ) ? $ / ,
248
+ / ^ d a t e ( t i m e ) ? $ / ,
249
+ / ^ m a c a d d r ( 8 ) ? $ / ,
250
+ / ^ ( l o n g | m e d i u m ) ? t e x t $ / ,
251
+ / ^ ( v a r ) ? c h a r ( a c t e r ) ? ( v a r y i n g ) ? ?( \( \d + \) ) ? $ / ,
252
+ / ^ t i m e ( s t a m p ) ? ( \( \d + \) ) ? ( ( w i t h | w i t h o u t ) t i m e z o n e ) ? $ / ,
253
+ ] ,
254
+ } ;
255
+
256
+ const findInMapping = (
257
+ mapping : TypeMapping ,
258
+ value : string | null ,
259
+ ) : string | null => {
260
+ if ( value === null ) {
261
+ return null ;
262
+ }
232
263
233
264
for ( const [ newType , matches ] of Object . entries ( mapping ) ) {
234
265
for ( const match of matches ) {
235
- if ( type === match ) {
266
+ if ( match === value ) {
236
267
return newType ;
237
268
}
238
269
239
- if ( match instanceof RegExp && type . match ( match ) ) {
270
+ if ( match instanceof RegExp && value . match ( match ) ) {
240
271
return newType ;
241
272
}
242
273
}
243
274
}
244
275
245
- return type ;
276
+ return null ;
246
277
} ;
247
278
248
- const getActualType = ( type : string ) : string => {
249
- const finalType = mapType ( type ) ;
279
+ const getActualType = ( cast : string | null , type : string ) : string => {
280
+ const finalType =
281
+ findInMapping ( castMapping , cast ) ||
282
+ cast ||
283
+ findInMapping ( typeMapping , type ) ||
284
+ "mixed" ;
250
285
251
286
if ( finalType . includes ( "\\" ) && ! finalType . startsWith ( "\\" ) ) {
252
287
return `\\${ finalType } ` ;
0 commit comments