Skip to content

Commit 13e8b79

Browse files
author
Maksym Mykhailenko
committed
Refactoring && improving && added new field type "Enum"
1 parent cfc28c2 commit 13e8b79

File tree

8 files changed

+125
-6
lines changed

8 files changed

+125
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]
1212
},
1313
"require": {
14-
"php": ">=5.4.0"
14+
"php": ">=5.5"
1515
},
1616
"require-dev": {
1717
"phpunit/phpunit": "^9.6"

src/Core/TypeBuilder.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace MaxGraphQL\Core;
44

5+
use MaxGraphQL\Interfaces\FieldType;
6+
57
abstract class TypeBuilder
68
{
9+
/**
10+
* @return string
11+
*/
712
protected function convert()
813
{
914
$str = $this->getType() . '{' . $this->getName();
@@ -61,10 +66,10 @@ private function checkArgumentsString($string)
6166
$str .= $string . ',';
6267
} elseif (is_bool($string)) {
6368
$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() . ',';
6671
} else {
67-
$str .= '"' . $string .'",';
72+
$str .= '"' . $string . '",';
6873
}
6974

7075
return $str;
@@ -123,6 +128,10 @@ private function isArrayAssociative($array)
123128
return array_keys($array) !== range(0, count($array) - 1);
124129
}
125130

131+
/**
132+
* @param $field
133+
* @return bool
134+
*/
126135
protected function isDuplicate($field)
127136
{
128137
foreach ($this->getSelect() as $item) {

src/FieldTypes/Enum.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/Interfaces/FieldType.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace MaxGraphQL\Interfaces;
4+
5+
interface FieldType
6+
{
7+
/**
8+
* @return string
9+
*/
10+
public function getValue();
11+
}

src/Types/Mutation.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ class Mutation extends TypeBuilder implements Type
1212
private $select = [];
1313
private $arguments = [];
1414

15+
/**
16+
* @param string $name
17+
*/
1518
public function __construct($name)
1619
{
1720
$this->name = $name;
1821
}
1922

23+
/**
24+
* @return string
25+
*/
2026
public function __toString()
2127
{
2228
return $this->convert();
2329
}
2430

31+
/**
32+
* @param string|array $field
33+
* @return self
34+
*/
2535
public function addSelect($field)
2636
{
2737
if (is_array($field)) {
@@ -38,37 +48,62 @@ public function addSelect($field)
3848
return $this;
3949
}
4050

51+
/**
52+
* @return string
53+
*/
4154
public function getPreparedQuery()
4255
{
4356
return $this->convert();
4457
}
4558

59+
/**
60+
* @param string $name
61+
* @param string|array $select
62+
* @param array $arguments
63+
* @return string
64+
*/
4665
public static function getPreparedQueryFrom($name, $select, $arguments = [])
4766
{
4867
return (new self($name))->addSelect($select)->addArguments($arguments)->convert();
4968
}
5069

70+
/**
71+
* @param array $arguments
72+
* @return self
73+
*/
5174
public function addArguments($arguments)
5275
{
5376
$this->arguments = $arguments;
5477
return $this;
5578
}
5679

80+
/**
81+
* @return array
82+
*/
5783
public function getSelect()
5884
{
5985
return $this->select;
6086
}
6187

88+
/**
89+
* @return array
90+
*/
6291
public function getArguments()
6392
{
6493
return $this->arguments;
6594
}
6695

96+
/**
97+
* @return string
98+
*/
6799
public function getType()
68100
{
69101
return self::TYPE;
70102
}
71103

104+
/**
105+
* @return string
106+
*/
72107
public function getName()
73108
{
74109
return $this->name;

src/Types/Query.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@ class Query extends TypeBuilder implements Type
1212
private $select = [];
1313
private $arguments = [];
1414

15+
/**
16+
* @param $name
17+
*/
1518
public function __construct($name)
1619
{
1720
$this->name = $name;
1821
}
1922

23+
/**
24+
* @return string
25+
*/
2026
public function __toString()
2127
{
2228
return $this->convert();
2329
}
2430

31+
/**
32+
* @param string|array $field
33+
* @return self
34+
*/
2535
public function addSelect($field)
2636
{
2737
if (is_array($field)) {
@@ -38,37 +48,62 @@ public function addSelect($field)
3848
return $this;
3949
}
4050

51+
/**
52+
* @return string
53+
*/
4154
public function getPreparedQuery()
4255
{
4356
return $this->convert();
4457
}
4558

59+
/**
60+
* @param string $name
61+
* @param string|array $select
62+
* @param array $arguments
63+
* @return string
64+
*/
4665
public static function getPreparedQueryFrom($name, $select, $arguments = [])
4766
{
4867
return (new self($name))->addSelect($select)->addArguments($arguments)->getPreparedQuery();
4968
}
5069

70+
/**
71+
* @param array $arguments
72+
* @return $this
73+
*/
5174
public function addArguments($arguments)
5275
{
5376
$this->arguments = $arguments;
5477
return $this;
5578
}
5679

80+
/**
81+
* @return array
82+
*/
5783
public function getSelect()
5884
{
5985
return $this->select;
6086
}
6187

88+
/**
89+
* @return array
90+
*/
6291
public function getArguments()
6392
{
6493
return $this->arguments;
6594
}
6695

96+
/**
97+
* @return string
98+
*/
6799
public function getType()
68100
{
69101
return self::TYPE;
70102
}
71103

104+
/**
105+
* @return string
106+
*/
72107
public function getName()
73108
{
74109
return $this->name;

tests/MutationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PHPUnit\Framework\TestCase;
44
use MaxGraphQL\Types\Mutation;
5+
use \MaxGraphQL\FieldTypes\Enum;
56

67
class MutationTest extends TestCase
78
{
@@ -36,7 +37,7 @@ public function testMutation3()
3637
$mutation = new Mutation('testing');
3738
$mutation
3839
->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]]);
4041

4142
$this->assertSame($string, $mutation->getPreparedQuery());
4243
}

tests/QueryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PHPUnit\Framework\TestCase;
44
use MaxGraphQL\Types\Query;
5+
use \MaxGraphQL\FieldTypes\Enum;
56

67
class QueryTest extends TestCase
78
{
@@ -24,7 +25,7 @@ public function testQuery2()
2425
$query = new Query('testing');
2526
$query
2627
->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]]);
2829

2930
$this->assertSame($string, $query->getPreparedQuery());
3031
}

0 commit comments

Comments
 (0)