@@ -37,24 +37,38 @@ private function recursiveCreateFile($sample, $namespaceString)
3737 $ namespace = $ phpFile ->addNamespace ($ namespaceString );
3838 $ class = $ namespace ->addClass ($ className );
3939
40+ $ class ->addMethod ('create ' )->setStatic (true )->setBody ('return new self; ' )->addComment ('@return ' . $ className );
4041 $ toArray = "return [ \n" ;
4142 foreach ($ data as $ itemKey => $ item ) {
4243 $ camelCase = $ this ->convert ($ itemKey );
43- $ class ->addProperty ($ camelCase );
44- if (is_array ($ item )) {
4544
45+ if (is_object ($ item )) {
4646 $ subClassName = $ this ->convertClassName ($ itemKey );
4747
48- $ toArray .= " ' $ itemKey' => " . 'collect($this-> ' . $ camelCase . ')->map(function ( ' . "$ subClassName " . ' $data){
49- return $data->toArray();
50- })->toArray() ' . ', ' . "\n" ;
48+ $ class ->addProperty ($ camelCase )->addComment ('@var ' . ucfirst ($ camelCase ) . ' $ ' . $ camelCase );
49+ $ class ->addMethod ('add ' . ucfirst ($ camelCase ))->addComment ('@param ' . ucfirst ($ camelCase ) . ' $ ' . $ camelCase . "\n" . '@return $this ' )->setBody ('$this-> ' . $ camelCase . ' = ' . '$ ' . $ camelCase . '; ' . "\n" . 'return $this; ' )->addParameter ($ camelCase );
50+
51+ $ toArray .= " ' $ itemKey' => " . '$this-> ' . $ camelCase . '->toArray() ' . ', ' . "\n" ;
5152 $ this ->recursiveCreateFile ([$ itemKey => $ item ], $ namespaceString );
5253 } else {
53- $ toArray .= " ' $ itemKey' => " . '$this-> ' . $ camelCase . ', ' . "\n" ;
54+ if (is_array ($ item )) {
55+ $ subClassName = $ this ->convertClassName ($ itemKey );
56+
57+ $ class ->addProperty ($ camelCase )->addComment ('@var ' . ucfirst ($ camelCase ) . ' $ ' . $ camelCase );
58+ $ class ->addMethod ('add ' . ucfirst ($ camelCase ))->addComment ('@param ' . ucfirst ($ camelCase ) . ' $ ' . $ camelCase . "\n" . '@return $this ' )->setBody ('$this-> ' . $ camelCase . '[] = ' . '$ ' . $ camelCase . '; ' . "\n" . 'return $this; ' )->addParameter ($ camelCase );
59+
60+ $ toArray .= " ' $ itemKey' => " . 'collect($this-> ' . $ camelCase . ')->map(function ( ' . "$ subClassName " . ' $data){
61+ return $data->toArray();
62+ })->toArray() ' . ', ' . "\n" ;
63+ $ this ->recursiveCreateFile ([$ itemKey => $ item ], $ namespaceString );
64+ } else {
65+ $ class ->addProperty ($ camelCase )->addComment ('@var $ ' . $ camelCase );
66+ $ toArray .= " ' $ itemKey' => " . '$this-> ' . $ camelCase . ', ' . "\n" ;
67+ }
5468 }
5569 }
5670 $ toArray .= "]; " ;
57- $ class ->addMethod ('toArray ' )->setReturnType ('array ' )->setBody ($ toArray ); // method return type;
71+ $ class ->addMethod ('toArray ' )->addComment ( ' @return array ' )-> setReturnType ('array ' )->setBody ($ toArray ); // method return type;
5872
5973 $ file = $ className . '.php ' ;
6074
@@ -84,4 +98,4 @@ private function convertClassName($string)
8498
8599 return $ str ;
86100 }
87- }
101+ }
0 commit comments