44
55
66use Illuminate \Support \Facades \File ;
7+ use Nette \PhpGenerator \ClassType ;
78use Nette \PhpGenerator \PhpFile ;
9+ use Nette \PhpGenerator \PhpNamespace ;
810
911/**
1012 * Class JsonToClassGenerator
@@ -25,44 +27,126 @@ public function generate()
2527 $ this ->recursiveCreateFile ($ this ->getData (), config ('jsontoclassgenerator.namespace ' ));
2628 }
2729
30+ public function getType ($ value )
31+ {
32+
33+ if ($ value === null ) {
34+ return ;
35+ }
36+
37+ if (is_string ($ value )) {
38+ return 'string ' ;
39+ }
40+
41+ if (($ value === true || $ value === false ) === true ) {
42+ return 'bool ' ;
43+ }
44+
45+ if (is_int ($ value )) {
46+ return 'int ' ;
47+ }
48+
49+ if (is_float ($ value )) {
50+ return 'float ' ;
51+ }
52+ return ;
53+ }
54+
55+ public function addMethod (ClassType $ class , $ type , $ name ,$ isArray = false )
56+ {
57+
58+ $ realType = last (explode ('\\' , $ type ));
59+
60+ $ getComment = empty ($ realType ) ? 'mixed ' : $ realType ;
61+
62+
63+ if ($ isArray === true ){
64+ $ class ->addMethod ('get ' . ucfirst ($ name ))
65+ ->addComment ('@return ' . $ getComment )
66+ ->setBody ('return $this-> ' . $ name . '; ' );
67+
68+ $ class ->addMethod ('set ' . ucfirst ($ name ))
69+ ->setReturnType ('self ' )
70+ ->addComment ('@param ' . $ realType . ' $ ' . $ name . "\n" . '@return $this ' )
71+ ->setBody ('$this-> ' . $ name . ' = ' . '$ ' . $ name . '; ' . "\n" . 'return $this; ' )
72+ ->addParameter ($ name );
73+ }else {
74+ $ class ->addMethod ('get ' . ucfirst ($ name ))
75+ ->setReturnType ($ type )
76+ ->addComment ('@return ' . $ getComment )
77+ ->setBody ('return $this-> ' . $ name . '; ' );
78+
79+ $ class ->addMethod ('set ' . ucfirst ($ name ))
80+ ->setReturnType ('self ' )
81+ ->addComment ('@param ' . $ realType . ' $ ' . $ name . "\n" . '@return $this ' )
82+ ->setBody ('$this-> ' . $ name . ' = ' . '$ ' . $ name . '; ' . "\n" . 'return $this; ' )
83+ ->addParameter ($ name )
84+ ->setTypeHint ($ type );
85+ }
86+
87+
88+ }
89+
2890 /**
2991 * @param $sample
3092 * @param $namespaceString
3193 */
3294 private function recursiveCreateFile ($ sample , $ namespaceString )
3395 {
3496 foreach ($ sample as $ key => $ data ) {
97+
3598 $ className = $ this ->convertClassName ($ key );
3699 $ phpFile = new PhpFile ();
37100 $ namespace = $ phpFile ->addNamespace ($ namespaceString );
38101 $ class = $ namespace ->addClass ($ className );
39-
40102 $ class ->addMethod ('create ' )->setStatic (true )->setBody ('return new self; ' )->addComment ('@return ' . $ className );
41103 $ toArray = "return [ \n" ;
42104 foreach ($ data as $ itemKey => $ item ) {
43105 $ camelCase = $ this ->convert ($ itemKey );
44106
45107 if (is_object ($ item )) {
46108 $ subClassName = $ this ->convertClassName ($ itemKey );
47-
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 );
109+ $ namespace ->addUse ($ namespaceString . '\\' . ucfirst ($ camelCase ), ucfirst ($ camelCase ));
110+ $ class ->addProperty ($ camelCase )->setVisibility ('private ' )->addComment ('@var ' . ucfirst ($ camelCase ) . ' $ ' . $ camelCase );
111+ $ class ->addMethod ('add ' . ucfirst ($ camelCase ))
112+ ->setReturnType ('self ' )
113+ ->addComment ('@param ' . $ camelCase . ' $ ' . $ camelCase . "\n" . '@return $this ' )
114+ ->setBody ('$this-> ' . $ camelCase . ' = ' . '$ ' . $ camelCase . '; ' . "\n" . 'return $this; ' )
115+ ->addParameter ($ camelCase )
116+ ->setTypeHint ($ namespaceString . '\\' . ucfirst ($ camelCase ));
50117
51118 $ toArray .= " ' $ itemKey' => " . '$this-> ' . $ camelCase . '->toArray() ' . ', ' . "\n" ;
119+ $ this ->addMethod ($ class , $ namespaceString . '\\' . ucfirst ($ camelCase ), $ camelCase );
52120 $ this ->recursiveCreateFile ([$ itemKey => $ item ], $ namespaceString );
53121 } else {
122+
54123 if (is_array ($ item )) {
55124 $ 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" ;
125+ $ namespace ->addUse ($ namespaceString . '\\' . ucfirst ($ camelCase ), ucfirst ($ camelCase ));
126+ $ class ->addProperty ($ camelCase ,
127+ [])->addComment ('@var ' . ucfirst ($ camelCase ) . '[] $ ' . $ camelCase )->setVisibility ('private ' );
128+ $ class ->addMethod ('add ' . ucfirst ($ camelCase ))
129+ ->setReturnType ('self ' )
130+ ->addComment ('@param ' . $ camelCase . ' $ ' . $ camelCase . "\n" . '@return $this ' )
131+ ->setBody ('$this-> ' . $ camelCase . '[] = ' . '$ ' . $ camelCase . '; ' . "\n" . 'return $this; ' )
132+ ->addParameter ($ camelCase )
133+ ->setTypeHint ($ namespaceString . '\\' . ucfirst ($ camelCase ));
134+ //
135+ // $toArray .= " '$itemKey' => " . 'collect($this->' . $camelCase . ')->map(function (' . ucfirst($camelCase) . ' $data){
136+ // return $data->toArray();
137+ // })->toArray()' . ',' . "\n";
138+
139+ $ toArray .=" ' $ itemKey' => " ."array_map(function ( " .ucfirst ($ camelCase ). ' $data){
140+ return $data->toArray();
141+ }, $this-> ' .$ camelCase .") " .', ' . "\n" ;
142+
143+
144+ $ this ->addMethod ($ class , $ namespaceString . '\\' . ucfirst ($ camelCase ).'[] ' , $ camelCase ,true );
63145 $ this ->recursiveCreateFile ([$ itemKey => $ item ], $ namespaceString );
64146 } else {
65- $ class ->addProperty ($ camelCase )->addComment ('@var $ ' . $ camelCase );
147+ $ propertyType = $ this ->getType ($ item );
148+ $ class ->addProperty ($ camelCase )->setVisibility ('private ' )->addComment ('@var ' . $ propertyType . ' $ ' . $ camelCase );
149+ $ this ->addMethod ($ class , $ propertyType , $ camelCase , $ namespaceString );
66150 $ toArray .= " ' $ itemKey' => " . '$this-> ' . $ camelCase . ', ' . "\n" ;
67151 }
68152 }
0 commit comments