1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
6
|
|
|
use Overblog\GraphQLBundle\Definition\Argument; |
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
9
|
|
|
|
10
|
|
|
class Resolver |
11
|
|
|
{ |
12
|
|
|
/** @var PropertyAccessor */ |
13
|
|
|
private static $accessor; |
14
|
|
|
|
15
|
52 |
|
public static function defaultResolveFn($objectOrArray, $args, $context, ResolveInfo $info) |
16
|
|
|
{ |
17
|
52 |
|
$fieldName = $info->fieldName; |
18
|
52 |
|
$value = static::valueFromObjectOrArray($objectOrArray, $fieldName); |
19
|
|
|
|
20
|
52 |
|
return $value instanceof \Closure ? $value($objectOrArray, $args, $context, $info) : $value; |
21
|
|
|
} |
22
|
|
|
|
23
|
53 |
|
public static function valueFromObjectOrArray($objectOrArray, $fieldName) |
24
|
|
|
{ |
25
|
53 |
|
$value = null; |
26
|
53 |
|
$index = sprintf('[%s]', $fieldName); |
27
|
|
|
|
28
|
53 |
View Code Duplication |
if (self::isReadable($objectOrArray, $index)) { |
|
|
|
|
29
|
35 |
|
$value = self::getAccessor()->getValue($objectOrArray, $index); |
30
|
31 |
|
} elseif (is_object($objectOrArray) && self::isReadable($objectOrArray, $fieldName)) { |
31
|
29 |
|
$value = self::getAccessor()->getValue($objectOrArray, $fieldName); |
32
|
|
|
} |
33
|
|
|
|
34
|
53 |
|
return $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
public static function setObjectOrArrayValue(&$objectOrArray, $fieldName, $value) |
38
|
|
|
{ |
39
|
6 |
|
$index = sprintf('[%s]', $fieldName); |
40
|
|
|
|
41
|
6 |
View Code Duplication |
if (self::isWritable($objectOrArray, $index)) { |
|
|
|
|
42
|
6 |
|
self::getAccessor()->setValue($objectOrArray, $index, $value); |
43
|
1 |
|
} elseif (is_object($objectOrArray) && self::isWritable($objectOrArray, $fieldName)) { |
44
|
1 |
|
self::getAccessor()->setValue($objectOrArray, $fieldName, $value); |
45
|
|
|
} |
46
|
6 |
|
} |
47
|
|
|
|
48
|
|
|
public static function wrapArgs(callable $resolver) |
49
|
|
|
{ |
50
|
69 |
|
return static function () use ($resolver) { |
51
|
48 |
|
$args = func_get_args(); |
52
|
48 |
|
if (count($args) > 1) { |
53
|
47 |
|
$args[1] = new Argument($args[1]); |
54
|
|
|
} |
55
|
|
|
|
56
|
48 |
|
return $resolver(...$args); |
57
|
69 |
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
53 |
|
private static function isReadable($objectOrArray, $indexOrProperty) |
61
|
|
|
{ |
62
|
53 |
|
return self::getAccessor()->isReadable($objectOrArray, $indexOrProperty); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
private static function isWritable($objectOrArray, $indexOrProperty) |
66
|
|
|
{ |
67
|
6 |
|
return self::getAccessor()->isWritable($objectOrArray, $indexOrProperty); |
68
|
|
|
} |
69
|
|
|
|
70
|
54 |
|
private static function getAccessor() |
71
|
|
|
{ |
72
|
54 |
|
if (null === self::$accessor) { |
73
|
1 |
|
self::$accessor = PropertyAccess::createPropertyAccessor(); |
74
|
|
|
} |
75
|
|
|
|
76
|
54 |
|
return self::$accessor; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.