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