5
5
namespace Overblog \GraphQLBundle \Resolver ;
6
6
7
7
use GraphQL \Type \Definition \ResolveInfo ;
8
- use Symfony \Component \PropertyAccess \PropertyAccess ;
9
- use Symfony \Component \PropertyAccess \PropertyAccessor ;
10
8
11
9
class Resolver
12
10
{
13
- /** @var PropertyAccessor */
14
- private static $ accessor ;
15
-
16
11
public static function defaultResolveFn ($ objectOrArray , $ args , $ context , ResolveInfo $ info )
17
12
{
18
13
$ fieldName = $ info ->fieldName ;
@@ -24,44 +19,34 @@ public static function defaultResolveFn($objectOrArray, $args, $context, Resolve
24
19
public static function valueFromObjectOrArray ($ objectOrArray , $ fieldName )
25
20
{
26
21
$ value = null ;
27
- $ index = \sprintf ('[%s] ' , $ fieldName );
28
-
29
- if (self ::isReadable ($ objectOrArray , $ index )) {
30
- $ value = self ::getAccessor ()->getValue ($ objectOrArray , $ index );
31
- } elseif (\is_object ($ objectOrArray ) && self ::isReadable ($ objectOrArray , $ fieldName )) {
32
- $ value = self ::getAccessor ()->getValue ($ objectOrArray , $ fieldName );
22
+ if (\is_array ($ objectOrArray ) && isset ($ objectOrArray [$ fieldName ])) {
23
+ $ value = $ objectOrArray [$ fieldName ];
24
+ } elseif (\is_object ($ objectOrArray )) {
25
+ if (null !== $ getter = self ::guessObjectMethod ($ objectOrArray , $ fieldName , 'get ' )) {
26
+ $ value = $ objectOrArray ->$ getter ();
27
+ } elseif (isset ($ objectOrArray ->$ fieldName )) {
28
+ $ value = $ objectOrArray ->$ fieldName ;
29
+ }
33
30
}
34
31
35
32
return $ value ;
36
33
}
37
34
38
35
public static function setObjectOrArrayValue (&$ objectOrArray , $ fieldName , $ value ): void
39
36
{
40
- $ index = \sprintf ('[%s] ' , $ fieldName );
41
-
42
- if (self ::isWritable ($ objectOrArray , $ index )) {
43
- self ::getAccessor ()->setValue ($ objectOrArray , $ index , $ value );
44
- } elseif (\is_object ($ objectOrArray ) && self ::isWritable ($ objectOrArray , $ fieldName )) {
45
- self ::getAccessor ()->setValue ($ objectOrArray , $ fieldName , $ value );
37
+ if (\is_array ($ objectOrArray )) {
38
+ $ objectOrArray [$ fieldName ] = $ value ;
39
+ } elseif (\is_object ($ objectOrArray )) {
40
+ $ objectOrArray ->$ fieldName = $ value ;
46
41
}
47
42
}
48
43
49
- private static function isReadable ($ objectOrArray , $ indexOrProperty )
50
- {
51
- return self ::getAccessor ()->isReadable ($ objectOrArray , $ indexOrProperty );
52
- }
53
-
54
- private static function isWritable ($ objectOrArray , $ indexOrProperty )
55
- {
56
- return self ::getAccessor ()->isWritable ($ objectOrArray , $ indexOrProperty );
57
- }
58
-
59
- private static function getAccessor ()
44
+ private static function guessObjectMethod ($ object , string $ fieldName , string $ prefix ): ?string
60
45
{
61
- if (null === self :: $ accessor ) {
62
- self :: $ accessor = PropertyAccess:: createPropertyAccessor () ;
46
+ if (\is_callable ([ $ object , $ method = $ prefix . \str_replace ( ' ' , '' , \ucwords ( \str_replace ( ' _ ' , ' ' , $ fieldName )))]) ) {
47
+ return $ method ;
63
48
}
64
49
65
- return self :: $ accessor ;
50
+ return null ;
66
51
}
67
52
}
0 commit comments