@@ -14,74 +14,76 @@ class QueryStatement
14
14
private $ operation ;
15
15
16
16
/**
17
- * @var string
17
+ * @var array
18
18
*/
19
- private $ reference ;
19
+ private $ clauses ;
20
20
21
21
/**
22
- * @var array
22
+ * @var string
23
23
*/
24
- private $ clauses ;
24
+ private $ model = null ;
25
25
26
- public function __construct (string $ operation , string $ reference , array $ clauses = [])
26
+ public function __construct (string $ operation , array $ clauses = [])
27
27
{
28
28
$ this ->operation = $ operation ;
29
- $ this ->reference = $ reference ;
30
29
$ this ->clauses = $ clauses ;
30
+
31
+ if ($ operation === 'all ' && !empty ($ this ->clauses ())) {
32
+ $ this ->model = Str::studly (Str::singular ($ this ->clauses ()[0 ]));
33
+ }
31
34
}
32
35
33
36
public function operation (): string
34
37
{
35
38
return $ this ->operation ;
36
39
}
37
40
38
- public function reference (): string
39
- {
40
- return $ this ->reference ;
41
- }
42
-
43
- public function model (): string
41
+ public function model (): ?string
44
42
{
45
- return Str:: studly (Str:: singular ( $ this ->reference ())) ;
43
+ return $ this ->model ;
46
44
}
47
45
48
46
public function clauses ()
49
47
{
50
48
return $ this ->clauses ;
51
49
}
52
50
53
- public function output ()
51
+ public function output (string $ controller ): string
54
52
{
53
+ $ model = $ this ->determineModel ($ controller );
54
+
55
55
if ($ this ->operation () === 'all ' ) {
56
- return '$ ' . $ this ->reference () . ' = ' . $ this ->model () . '::all(); ' ;
56
+ if (is_null ($ this ->model ())) {
57
+ return '$ ' . Str::lower (Str::plural ($ model )) . ' = ' . $ model . '::all(); ' ;
58
+ } else {
59
+ return '$ ' . Str::lower ($ this ->clauses ()[0 ]) . ' = ' . $ this ->model () . '::all(); ' ;
60
+ }
57
61
}
58
62
59
63
$ methods = [];
60
- $ pluck_field = null ;
61
64
foreach ($ this ->clauses as $ clause ) {
62
- [$ method , $ value ] = explode (': ' , $ clause );
65
+ [$ method , $ argument ] = explode (': ' , $ clause );
63
66
64
67
if (in_array ($ method , ['where ' , 'order ' , 'pluck ' ])) {
65
- $ value = $ this ->columnName ($ value );
68
+ $ column = $ this ->columnName ($ model , $ argument );
66
69
}
67
70
68
71
if ($ method === 'where ' ) {
69
- $ methods [] = $ method . '( ' . "' {$ value }', $ " . $ value . ') ' ;
72
+ $ methods [] = $ method . '( ' . "' {$ column }', $ " . str_replace ( ' . ' , ' -> ' , $ argument ) . ') ' ;
70
73
} elseif ($ method === 'pluck ' ) {
71
- $ pluck_field = $ value ;
72
- $ methods [] = "pluck(' {$ value }') " ;
74
+ $ pluck_field = $ argument ;
75
+ $ methods [] = "pluck(' {$ column }') " ;
73
76
} elseif ($ method === 'order ' ) {
74
- $ methods [] = "orderBy(' {$ value }') " ;
77
+ $ methods [] = "orderBy(' {$ column }') " ;
75
78
} else {
76
- $ methods [] = $ method . '( ' . $ value . ') ' ;
79
+ $ methods [] = $ method . '( ' . $ argument . ') ' ;
77
80
}
78
81
}
79
82
80
- // TODO: leverage model/context...
81
- $ model = 'Post ' ;
82
-
83
- if ($ pluck_field ) {
83
+ if ($ this ->operation () === 'pluck ' ) {
84
84
$ variable_name = $ this ->pluckName ($ pluck_field );
85
+ } elseif ($ this ->operation () === 'count ' ) {
86
+ $ variable_name = Str::lower ($ model ) . '_count ' ;
85
87
} else {
86
88
$ variable_name = Str::lower (Str::plural ($ model ));
87
89
}
@@ -90,19 +92,22 @@ public function output()
90
92
91
93
$ code .= implode ('-> ' , $ methods );
92
94
93
- if (! $ pluck_field ) {
94
- $ code .= '->get () ' ;
95
+ if (in_array ( $ this -> operation (), [ ' get ' , ' count ' ]) ) {
96
+ $ code .= '-> ' . $ this -> operation () . ' () ' ;
95
97
}
96
98
97
99
$ code .= '; ' ;
98
100
99
101
return $ code ;
100
102
}
101
103
102
- private function columnName ($ value )
104
+ private function columnName ($ model , $ value )
103
105
{
104
106
if (Str::contains ($ value , '. ' )) {
105
- return Str::after ($ value , '. ' );
107
+ $ reference = Str::before ($ value , '. ' );
108
+ if (strcasecmp ($ model , $ reference ) === 0 ) {
109
+ return Str::after ($ value , '. ' );
110
+ }
106
111
}
107
112
108
113
return $ value ;
@@ -111,10 +116,18 @@ private function columnName($value)
111
116
private function pluckName (string $ field )
112
117
{
113
118
if (Str::contains ($ field , '. ' )) {
114
- dump ('here ' );
115
119
return Str::lower (Str::plural (str_replace ('. ' , '_ ' , $ field )));
116
120
}
117
121
118
122
return Str::lower ('Post ' . '_ ' . Str::plural ($ field ));
119
123
}
124
+
125
+ private function determineModel (string $ prefix )
126
+ {
127
+ if (empty ($ this ->model ())) {
128
+ return Str::studly (Str::singular ($ prefix ));
129
+ }
130
+
131
+ return Str::studly ($ this ->model ());
132
+ }
120
133
}
0 commit comments