8
8
use Spatie \TypeScriptTransformer \References \ClassStringReference ;
9
9
use Spatie \TypeScriptTransformer \Transformers \ClassPropertyProcessors \ClassPropertyProcessor ;
10
10
use Spatie \TypeScriptTransformer \TypeScript \TypeReference ;
11
+ use Spatie \TypeScriptTransformer \TypeScript \TypeScriptArray ;
11
12
use Spatie \TypeScriptTransformer \TypeScript \TypeScriptGeneric ;
13
+ use Spatie \TypeScriptTransformer \TypeScript \TypeScriptIdentifier ;
12
14
use Spatie \TypeScriptTransformer \TypeScript \TypeScriptProperty ;
13
15
use Spatie \TypeScriptTransformer \TypeScript \TypeScriptString ;
14
16
use Spatie \TypeScriptTransformer \TypeScript \TypeScriptUnion ;
17
+ use Spatie \TypeScriptTransformer \Visitor \Visitor ;
18
+ use Spatie \TypeScriptTransformer \Visitor \VisitorOperation ;
15
19
use Spatie \TypeScriptTransformer \Visitor \VisitTypeScriptTreeAction ;
16
20
17
21
class ReplaceLaravelCollectionByArrayClassPropertyProcessor implements ClassPropertyProcessor
18
22
{
19
- public function __construct (
20
- protected VisitTypeScriptTreeAction $ visitTypeScriptTreeAction = new VisitTypeScriptTreeAction (),
21
- ) {
22
- }
23
+ protected Visitor $ visitor ;
23
24
24
- public function execute (
25
- ReflectionProperty $ reflection ,
26
- ?TypeNode $ annotation ,
27
- TypeScriptProperty $ property
28
- ): ?TypeScriptProperty {
29
- $ this ->visitTypeScriptTreeAction ->execute ($ property ->type , function (TypeScriptGeneric $ generic ) {
25
+ public function __construct ()
26
+ {
27
+ $ this ->visitor = Visitor::create ()->before (function (TypeScriptGeneric $ generic ) {
30
28
$ isCollection = $ generic ->type instanceof TypeReference
31
29
&& $ generic ->type ->reference instanceof ClassStringReference
32
30
&& is_a ($ generic ->type ->reference ->classString , Collection::class, true );
@@ -42,12 +40,26 @@ public function execute(
42
40
43
41
$ isRecord = $ generic ->genericTypes [0 ] instanceof TypeScriptUnion || $ generic ->genericTypes [0 ] instanceof TypeScriptString;
44
42
45
- // $generic->type = new
46
-
47
- if ($ isCollection ) {
48
- // $generic->type = new TypeReference(new TypeScriptArray());
43
+ if ($ isRecord ){
44
+ return VisitorOperation::replace (new TypeScriptGeneric (
45
+ new TypeScriptIdentifier ('Record ' ),
46
+ [
47
+ $ generic ->genericTypes [0 ],
48
+ $ generic ->genericTypes [1 ]
49
+ ]
50
+ ));
49
51
}
52
+
53
+ return VisitorOperation::replace (new TypeScriptArray ([$ generic ->genericTypes [1 ]]));
50
54
}, [TypeScriptGeneric::class]);
55
+ }
56
+
57
+ public function execute (
58
+ ReflectionProperty $ reflection ,
59
+ ?TypeNode $ annotation ,
60
+ TypeScriptProperty $ property
61
+ ): ?TypeScriptProperty {
62
+ $ property ->type = $ this ->visitor ->execute ($ property ->type );
51
63
52
64
return $ property ;
53
65
}
0 commit comments