1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Relay\Node; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Config; |
15
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
16
|
|
|
use Overblog\GraphQLBundle\Definition\Type; |
17
|
|
|
|
18
|
|
|
class PluralIdentifyingRootFieldDefinition implements MappingInterface |
19
|
|
|
{ |
20
|
15 |
|
public function toMappingDefinition(array $config) |
21
|
|
|
{ |
22
|
15 |
View Code Duplication |
if (!isset($config['argName']) || !is_string($config['argName'])) { |
|
|
|
|
23
|
2 |
|
throw new \InvalidArgumentException('A valid pluralIdentifyingRoot "argName" config is required.'); |
24
|
|
|
} |
25
|
|
|
|
26
|
13 |
View Code Duplication |
if (!isset($config['inputType']) || !is_string($config['inputType'])) { |
|
|
|
|
27
|
2 |
|
throw new \InvalidArgumentException('A valid pluralIdentifyingRoot "inputType" config is required.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
11 |
View Code Duplication |
if (!isset($config['outputType']) || !is_string($config['outputType'])) { |
|
|
|
|
31
|
2 |
|
throw new \InvalidArgumentException('A valid pluralIdentifyingRoot "outputType" config is required.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
9 |
|
if (!array_key_exists('resolveSingleInput', $config)) { |
35
|
1 |
|
throw new \InvalidArgumentException('PluralIdentifyingRoot "resolveSingleInput" config is required.'); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
$argName = $config['argName']; |
39
|
|
|
|
40
|
|
|
return [ |
41
|
8 |
|
'type' => "[${config['outputType']}]", |
42
|
|
|
'args' => [$argName => ['type' => "[${config['inputType']}!]!"]], |
43
|
8 |
|
'resolve' => sprintf( |
44
|
8 |
|
"@=resolver('relay_plural_identifying_field', [args['$argName'], resolveSingleInputCallback(%s)])", |
45
|
8 |
|
$this->cleanResolveSingleInput($config['resolveSingleInput']) |
46
|
8 |
|
), |
47
|
8 |
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
private function cleanResolveSingleInput($resolveSingleInput) |
|
|
|
|
51
|
|
|
{ |
52
|
8 |
|
if (is_string($resolveSingleInput) && 0 === strpos($resolveSingleInput, '@=')) { |
53
|
2 |
|
$cleanResolveSingleInput = substr($resolveSingleInput, 2); |
54
|
2 |
|
} else { |
55
|
6 |
|
$cleanResolveSingleInput = json_encode($resolveSingleInput); |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
return $cleanResolveSingleInput; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.