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