|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
9
|
|
|
use function is_array; |
|
10
|
|
|
use function is_callable; |
|
11
|
|
|
use function is_object; |
|
12
|
|
|
use function str_replace; |
|
13
|
|
|
|
|
14
|
|
|
class FieldResolver |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param mixed $parentValue |
|
18
|
|
|
* @param mixed $args |
|
19
|
|
|
* @param mixed $context |
|
20
|
|
|
* |
|
21
|
|
|
* @return mixed|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __invoke($parentValue, $args, $context, ResolveInfo $info) |
|
24
|
44 |
|
{ |
|
25
|
|
|
$fieldName = $info->fieldName; |
|
26
|
44 |
|
$value = static::valueFromObjectOrArray($parentValue, $fieldName); |
|
27
|
44 |
|
|
|
28
|
|
|
return $value instanceof Closure ? $value($parentValue, $args, $context, $info) : $value; |
|
29
|
44 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param object|array $objectOrArray |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function valueFromObjectOrArray($objectOrArray, string $fieldName) |
|
37
|
45 |
|
{ |
|
38
|
|
|
if (is_array($objectOrArray) && isset($objectOrArray[$fieldName])) { |
|
39
|
45 |
|
return $objectOrArray[$fieldName]; |
|
40
|
45 |
|
} |
|
41
|
35 |
|
|
|
42
|
24 |
|
if (is_object($objectOrArray)) { |
|
43
|
22 |
|
if (null !== $getter = static::checkGetterExists($objectOrArray, $fieldName, 'get')) { |
|
44
|
17 |
|
return $objectOrArray->$getter(); |
|
45
|
5 |
|
} |
|
46
|
1 |
|
|
|
47
|
4 |
|
if (null !== $getter = static::checkGetterExists($objectOrArray, $fieldName, 'is')) { |
|
48
|
1 |
|
return $objectOrArray->$getter(); |
|
49
|
3 |
|
} |
|
50
|
2 |
|
|
|
51
|
|
|
if (null !== $getter = static::checkGetterExists($objectOrArray, $fieldName, 'has')) { |
|
52
|
|
|
return $objectOrArray->$getter(); |
|
53
|
|
|
} |
|
54
|
45 |
|
|
|
55
|
|
|
if (null !== $getter = static::checkGetterExists($objectOrArray, $fieldName)) { |
|
56
|
|
|
return $objectOrArray->$getter(); |
|
57
|
22 |
|
} |
|
58
|
|
|
|
|
59
|
22 |
|
if (isset($objectOrArray->$fieldName)) { |
|
60
|
19 |
|
return $objectOrArray->$fieldName; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
5 |
|
|
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private static function checkGetterExists(object $object, string $fieldName, string $prefix = ''): ?string |
|
68
|
|
|
{ |
|
69
|
|
|
if (is_callable([$object, $method = $prefix.str_replace('_', '', $fieldName)])) { |
|
70
|
|
|
return $method; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @deprecated deprecated since version 1.0. Use `checkGetterExists` instead. |
|
78
|
|
|
*/ |
|
79
|
|
|
private static function guessObjectMethod(object $object, string $fieldName, string $prefix): ?string |
|
80
|
|
|
{ |
|
81
|
|
|
return static::checkGetterExists($object, $fieldName, $prefix); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|