File tree Expand file tree Collapse file tree 8 files changed +125
-6
lines changed Expand file tree Collapse file tree 8 files changed +125
-6
lines changed Original file line number Diff line number Diff line change 11
11
]
12
12
},
13
13
"require" : {
14
- "php" : " >=5.4.0 "
14
+ "php" : " >=5.5 "
15
15
},
16
16
"require-dev" : {
17
17
"phpunit/phpunit" : " ^9.6"
Original file line number Diff line number Diff line change 2
2
3
3
namespace MaxGraphQL \Core ;
4
4
5
+ use MaxGraphQL \Interfaces \FieldType ;
6
+
5
7
abstract class TypeBuilder
6
8
{
9
+ /**
10
+ * @return string
11
+ */
7
12
protected function convert ()
8
13
{
9
14
$ str = $ this ->getType () . '{ ' . $ this ->getName ();
@@ -61,10 +66,10 @@ private function checkArgumentsString($string)
61
66
$ str .= $ string . ', ' ;
62
67
} elseif (is_bool ($ string )) {
63
68
$ str .= $ string ? 'true, ' : 'false, ' ;
64
- } elseif (ctype_upper ($ string )) {
65
- $ str .= $ string . ', ' ;
69
+ } elseif (is_object ($ string) && is_subclass_of ( $ string , FieldType::class )) {
70
+ $ str .= $ string-> getValue () . ', ' ;
66
71
} else {
67
- $ str .= '" ' . $ string .'", ' ;
72
+ $ str .= '" ' . $ string . '", ' ;
68
73
}
69
74
70
75
return $ str ;
@@ -123,6 +128,10 @@ private function isArrayAssociative($array)
123
128
return array_keys ($ array ) !== range (0 , count ($ array ) - 1 );
124
129
}
125
130
131
+ /**
132
+ * @param $field
133
+ * @return bool
134
+ */
126
135
protected function isDuplicate ($ field )
127
136
{
128
137
foreach ($ this ->getSelect () as $ item ) {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MaxGraphQL \FieldTypes ;
4
+
5
+ use MaxGraphQL \Interfaces \FieldType ;
6
+
7
+ class Enum implements FieldType
8
+ {
9
+ /** @var string $value */
10
+ private $ value ;
11
+
12
+ /**
13
+ * @param string $value
14
+ */
15
+ public function __construct ($ value )
16
+ {
17
+ $ this ->value = $ value ;
18
+ }
19
+
20
+ /**
21
+ * @return string
22
+ */
23
+ public function getValue ()
24
+ {
25
+ return $ this ->value ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace MaxGraphQL \Interfaces ;
4
+
5
+ interface FieldType
6
+ {
7
+ /**
8
+ * @return string
9
+ */
10
+ public function getValue ();
11
+ }
Original file line number Diff line number Diff line change @@ -12,16 +12,26 @@ class Mutation extends TypeBuilder implements Type
12
12
private $ select = [];
13
13
private $ arguments = [];
14
14
15
+ /**
16
+ * @param string $name
17
+ */
15
18
public function __construct ($ name )
16
19
{
17
20
$ this ->name = $ name ;
18
21
}
19
22
23
+ /**
24
+ * @return string
25
+ */
20
26
public function __toString ()
21
27
{
22
28
return $ this ->convert ();
23
29
}
24
30
31
+ /**
32
+ * @param string|array $field
33
+ * @return self
34
+ */
25
35
public function addSelect ($ field )
26
36
{
27
37
if (is_array ($ field )) {
@@ -38,37 +48,62 @@ public function addSelect($field)
38
48
return $ this ;
39
49
}
40
50
51
+ /**
52
+ * @return string
53
+ */
41
54
public function getPreparedQuery ()
42
55
{
43
56
return $ this ->convert ();
44
57
}
45
58
59
+ /**
60
+ * @param string $name
61
+ * @param string|array $select
62
+ * @param array $arguments
63
+ * @return string
64
+ */
46
65
public static function getPreparedQueryFrom ($ name , $ select , $ arguments = [])
47
66
{
48
67
return (new self ($ name ))->addSelect ($ select )->addArguments ($ arguments )->convert ();
49
68
}
50
69
70
+ /**
71
+ * @param array $arguments
72
+ * @return self
73
+ */
51
74
public function addArguments ($ arguments )
52
75
{
53
76
$ this ->arguments = $ arguments ;
54
77
return $ this ;
55
78
}
56
79
80
+ /**
81
+ * @return array
82
+ */
57
83
public function getSelect ()
58
84
{
59
85
return $ this ->select ;
60
86
}
61
87
88
+ /**
89
+ * @return array
90
+ */
62
91
public function getArguments ()
63
92
{
64
93
return $ this ->arguments ;
65
94
}
66
95
96
+ /**
97
+ * @return string
98
+ */
67
99
public function getType ()
68
100
{
69
101
return self ::TYPE ;
70
102
}
71
103
104
+ /**
105
+ * @return string
106
+ */
72
107
public function getName ()
73
108
{
74
109
return $ this ->name ;
Original file line number Diff line number Diff line change @@ -12,16 +12,26 @@ class Query extends TypeBuilder implements Type
12
12
private $ select = [];
13
13
private $ arguments = [];
14
14
15
+ /**
16
+ * @param $name
17
+ */
15
18
public function __construct ($ name )
16
19
{
17
20
$ this ->name = $ name ;
18
21
}
19
22
23
+ /**
24
+ * @return string
25
+ */
20
26
public function __toString ()
21
27
{
22
28
return $ this ->convert ();
23
29
}
24
30
31
+ /**
32
+ * @param string|array $field
33
+ * @return self
34
+ */
25
35
public function addSelect ($ field )
26
36
{
27
37
if (is_array ($ field )) {
@@ -38,37 +48,62 @@ public function addSelect($field)
38
48
return $ this ;
39
49
}
40
50
51
+ /**
52
+ * @return string
53
+ */
41
54
public function getPreparedQuery ()
42
55
{
43
56
return $ this ->convert ();
44
57
}
45
58
59
+ /**
60
+ * @param string $name
61
+ * @param string|array $select
62
+ * @param array $arguments
63
+ * @return string
64
+ */
46
65
public static function getPreparedQueryFrom ($ name , $ select , $ arguments = [])
47
66
{
48
67
return (new self ($ name ))->addSelect ($ select )->addArguments ($ arguments )->getPreparedQuery ();
49
68
}
50
69
70
+ /**
71
+ * @param array $arguments
72
+ * @return $this
73
+ */
51
74
public function addArguments ($ arguments )
52
75
{
53
76
$ this ->arguments = $ arguments ;
54
77
return $ this ;
55
78
}
56
79
80
+ /**
81
+ * @return array
82
+ */
57
83
public function getSelect ()
58
84
{
59
85
return $ this ->select ;
60
86
}
61
87
88
+ /**
89
+ * @return array
90
+ */
62
91
public function getArguments ()
63
92
{
64
93
return $ this ->arguments ;
65
94
}
66
95
96
+ /**
97
+ * @return string
98
+ */
67
99
public function getType ()
68
100
{
69
101
return self ::TYPE ;
70
102
}
71
103
104
+ /**
105
+ * @return string
106
+ */
72
107
public function getName ()
73
108
{
74
109
return $ this ->name ;
Original file line number Diff line number Diff line change 2
2
3
3
use PHPUnit \Framework \TestCase ;
4
4
use MaxGraphQL \Types \Mutation ;
5
+ use \MaxGraphQL \FieldTypes \Enum ;
5
6
6
7
class MutationTest extends TestCase
7
8
{
@@ -36,7 +37,7 @@ public function testMutation3()
36
37
$ mutation = new Mutation ('testing ' );
37
38
$ mutation
38
39
->addSelect (['test3 ' => ['test4 ' ]])
39
- ->addArguments (['id ' => ['user_id ' => '5 ' , 'user_type ' => 'ACCOUNT ' ], 'test ' => ['test2 ' => 5 ]]);
40
+ ->addArguments (['id ' => ['user_id ' => '5 ' , 'user_type ' => new Enum ( 'ACCOUNT ' ) ], 'test ' => ['test2 ' => 5 ]]);
40
41
41
42
$ this ->assertSame ($ string , $ mutation ->getPreparedQuery ());
42
43
}
Original file line number Diff line number Diff line change 2
2
3
3
use PHPUnit \Framework \TestCase ;
4
4
use MaxGraphQL \Types \Query ;
5
+ use \MaxGraphQL \FieldTypes \Enum ;
5
6
6
7
class QueryTest extends TestCase
7
8
{
@@ -24,7 +25,7 @@ public function testQuery2()
24
25
$ query = new Query ('testing ' );
25
26
$ query
26
27
->addSelect (['test3 ' => ['test4 ' ]])
27
- ->addArguments (['id ' => '5 ' , 'user_type ' => 'ACCOUNT ' , 'test ' => ['test2 ' => 5 ]]);
28
+ ->addArguments (['id ' => '5 ' , 'user_type ' => new Enum ( 'ACCOUNT ' ) , 'test ' => ['test2 ' => 5 ]]);
28
29
29
30
$ this ->assertSame ($ string , $ query ->getPreparedQuery ());
30
31
}
You can’t perform that action at this time.
0 commit comments