@@ -19,6 +19,7 @@ final class ValidatorBuilder {
1919 private HackCodegenFactory $cg ;
2020 private bool $discardChanges = false ;
2121 private bool $createAbstract = false ;
22+ private RootBuilder $builder ;
2223 private ?Codegen :: TSanitizeStringConfig $sanitizeString = null ;
2324
2425 public function __construct (
@@ -28,6 +29,22 @@ public function __construct(
2829 private dict <arraykey , mixed > $schema ,
2930 ) {
3031 $this -> cg = new HackCodegenFactory ($hackCodegenConfig );
32+
33+ $config = $codegenConfig [' validator' ];
34+
35+ $class = $this -> cg -> codegenClass($config [' class' ]);
36+
37+ $file = $this -> cg
38+ -> codegenFile($config [' file' ])
39+ -> setFileType(CodegenFileType :: HACK_STRICT )
40+ -> useNamespace(' Slack\Hack\JsonSchema' );
41+
42+ if (Shapes :: keyExists($config , ' namespace' )) {
43+ $file -> setNamespace($config [' namespace' ]);
44+ }
45+
46+ $this -> builder =
47+ new RootBuilder ($this -> codegenConfig , $this -> cg , $this -> jsonSchemaCodegenConfig , $this -> schema , $class , $file );
3148 }
3249
3350 public function setCreateAbstractClass (bool $abstract ): this {
@@ -36,12 +53,12 @@ public function setCreateAbstractClass(bool $abstract): this {
3653 }
3754
3855 public function setGeneratedFrom (CodegenGeneratedFrom $generated_from ): this {
39- $this -> generatedFrom = $generated_from ;
56+ $this -> builder -> getFile() -> setGeneratedFrom( $generated_from ) ;
4057 return $this ;
4158 }
4259
4360 public function setDiscardChanges (bool $discard ): this {
44- $this -> discardChanges = $discard ;
61+ $this -> builder -> getFile() -> setDoClobber( $discard ) ;
4562 return $this ;
4663 }
4764
@@ -50,59 +67,40 @@ public function setSanitizeString(?Codegen::TSanitizeStringConfig $sanitize_stri
5067 return $this ;
5168 }
5269
53- public function renderToFile (string $filename , ?string $namespace , string $classname ): CodegenFile {
54- $file = $this -> getCodegenFile($filename , $namespace , $classname );
55- $file -> save();
56- return $file ;
57- }
58-
59- private function getCodegenFile (string $filename , ?string $namespace , string $classname ): CodegenFile {
60- $file = $this -> cg
61- -> codegenFile($filename )
62- -> setDoClobber($this -> discardChanges )
63- -> setFileType(CodegenFileType :: HACK_STRICT )
64- -> setGeneratedFrom($this -> generatedFrom ?? $this -> cg -> codegenGeneratedFromScript())
65- -> useNamespace(' Slack\Hack\JsonSchema' );
66-
67- $this -> buildCodegenClass($classname , $file );
68-
69- if ($namespace !== null ) {
70- $file -> setNamespace($namespace );
71- }
72-
73- return $file ;
70+ public function getBuilder (): RootBuilder {
71+ return $this -> builder ;
7472 }
7573
76- private function buildCodegenClass (string $classname , CodegenFile $file ): void {
77- $class = $this -> cg -> codegenClass($classname );
78-
79- $root =
80- new RootBuilder ($this -> codegenConfig , $this -> cg , $this -> jsonSchemaCodegenConfig , $this -> schema , $class , $file );
81-
82- $root -> build();
74+ public function build (): CodegenFile {
75+ $this -> builder -> build();
8376
8477 $abstract = $this -> createAbstract ;
85- $class
86- -> setExtends(" JsonSchema\BaseValidator<{$root->getType()} >" )
78+ $class = $this -> builder
79+ -> getClass()
80+ -> setExtends(" JsonSchema\BaseValidator<{$this->builder->getType()} >" )
8781 -> setIsAbstract($abstract )
8882 -> setIsFinal(! $abstract )
89- -> addMethod($this -> getCodegenClassProcessMethod($root ));
83+ -> addMethod($this -> getCodegenClassProcessMethod());
9084
85+ $file = $this -> builder -> getFile();
9186 $file -> addClass($class );
9287
9388 $contents = $file -> render();
9489 if (Str \contains ($contents , ' Constraints\\ ' )) {
9590 $file -> useNamespace(' Slack\Hack\JsonSchema\Constraints' );
9691 }
92+
93+ $file -> save();
94+ return $file ;
9795 }
9896
99- private function getCodegenClassProcessMethod (RootBuilder $root ): CodegenMethod {
97+ private function getCodegenClassProcessMethod (): CodegenMethod {
10098 $hb = new HackBuilder ($this -> hackCodegenConfig );
10199 $hb -> addMultilineCall(' return self::check' , vec [' $this->input' , ' $this->pointer' ]);
102100
103101 return $this -> cg
104102 -> codegenMethod(' process' )
105- -> setReturnType($root -> getType())
103+ -> setReturnType($this -> builder -> getType())
106104 -> addEmptyUserAttribute(' __Override' )
107105 -> setProtected()
108106 -> setBody($hb -> getCode());
0 commit comments