1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
6
|
|
|
|
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
8
|
|
|
|
9
|
|
|
class Resolver |
10
|
|
|
{ |
11
|
42 |
|
public static function defaultResolveFn($objectOrArray, $args, $context, ResolveInfo $info) |
12
|
|
|
{ |
13
|
42 |
|
$fieldName = $info->fieldName; |
14
|
42 |
|
$value = static::valueFromObjectOrArray($objectOrArray, $fieldName); |
15
|
|
|
|
16
|
42 |
|
return $value instanceof \Closure ? $value($objectOrArray, $args, $context, $info) : $value; |
17
|
|
|
} |
18
|
|
|
|
19
|
43 |
|
public static function valueFromObjectOrArray($objectOrArray, $fieldName) |
20
|
|
|
{ |
21
|
43 |
|
$value = null; |
22
|
43 |
|
if (\is_array($objectOrArray) && isset($objectOrArray[$fieldName])) { |
23
|
36 |
|
$value = $objectOrArray[$fieldName]; |
24
|
21 |
|
} elseif (\is_object($objectOrArray)) { |
25
|
19 |
|
if (null !== $getter = self::guessObjectMethod($objectOrArray, $fieldName, 'get')) { |
26
|
17 |
|
$value = $objectOrArray->$getter(); |
27
|
2 |
|
} elseif (isset($objectOrArray->$fieldName)) { |
28
|
1 |
|
$value = $objectOrArray->$fieldName; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
43 |
|
return $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
8 |
|
public static function setObjectOrArrayValue(&$objectOrArray, $fieldName, $value): void |
36
|
|
|
{ |
37
|
8 |
|
if (\is_array($objectOrArray)) { |
38
|
8 |
|
$objectOrArray[$fieldName] = $value; |
39
|
1 |
|
} elseif (\is_object($objectOrArray)) { |
40
|
1 |
|
$objectOrArray->$fieldName = $value; |
41
|
|
|
} |
42
|
8 |
|
} |
43
|
|
|
|
44
|
19 |
|
private static function guessObjectMethod($object, string $fieldName, string $prefix): ?string |
45
|
|
|
{ |
46
|
19 |
|
if (\is_callable([$object, $method = $prefix.\str_replace(' ', '', \ucwords(\str_replace('_', ' ', $fieldName)))])) { |
47
|
17 |
|
return $method; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return null; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|