|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 13.05.16 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Field; |
|
9
|
|
|
|
|
10
|
|
|
use Youshido\GraphQL\Config\Field\FieldConfig; |
|
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
12
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
13
|
|
|
use Youshido\GraphQL\Type\Traits\AutoNameTrait; |
|
14
|
|
|
use Youshido\GraphQL\Type\Traits\FieldsArgumentsAwareObjectTrait; |
|
15
|
|
|
use Youshido\GraphQL\Type\TypeFactory; |
|
16
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
17
|
|
|
use Youshido\GraphQL\Type\TypeService; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractField implements FieldInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
use FieldsArgumentsAwareObjectTrait; |
|
23
|
|
|
use AutoNameTrait { |
|
24
|
|
|
getName as getAutoName; |
|
25
|
|
|
} |
|
26
|
|
|
protected $isFinal = false; |
|
27
|
|
|
|
|
28
|
|
|
private $resolveFunctionCache = null; |
|
29
|
|
|
private $nameCache = null; |
|
30
|
|
|
|
|
31
|
66 |
|
public function __construct(array $config = []) |
|
32
|
|
|
{ |
|
33
|
66 |
|
if (empty($config['type'])) { |
|
34
|
31 |
|
$config['type'] = $this->getType(); |
|
35
|
31 |
|
$config['name'] = $this->getName(); |
|
36
|
31 |
|
if (empty($config['name'])) { |
|
37
|
2 |
|
$config['name'] =$this->getAutoName(); |
|
38
|
2 |
|
} |
|
39
|
31 |
|
} |
|
40
|
|
|
|
|
41
|
66 |
|
if (TypeService::isScalarType($config['type'])) { |
|
42
|
58 |
|
$config['type'] = TypeFactory::getScalarType($config['type']); |
|
43
|
58 |
|
} |
|
44
|
66 |
|
$this->nameCache = isset($config['name']) ? $config['name'] : $this->getAutoName(); |
|
45
|
|
|
|
|
46
|
66 |
|
$this->config = new FieldConfig($config, $this, $this->isFinal); |
|
47
|
66 |
|
$this->build($this->config); |
|
48
|
66 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return AbstractObjectType |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract public function getType(); |
|
54
|
|
|
|
|
55
|
65 |
|
public function build(FieldConfig $config) |
|
56
|
|
|
{ |
|
57
|
65 |
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function setType($type) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->getConfig()->set('type', $type); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
24 |
|
public function resolve($value, array $args, ResolveInfo $info) |
|
65
|
|
|
{ |
|
66
|
24 |
|
if ($this->resolveFunctionCache === null) { |
|
67
|
24 |
|
$this->resolveFunctionCache = $this->getConfig()->getResolveFunction(); |
|
68
|
|
|
|
|
69
|
24 |
|
if (!$this->resolveFunctionCache) { |
|
70
|
20 |
|
$this->resolveFunctionCache = false; |
|
71
|
20 |
|
} |
|
72
|
24 |
|
} |
|
73
|
24 |
|
if ($this->resolveFunctionCache) { |
|
74
|
23 |
|
$resolveFunction = $this->resolveFunctionCache; |
|
75
|
|
|
|
|
76
|
23 |
|
return $resolveFunction($value, $args, $info); |
|
77
|
|
|
} else { |
|
78
|
20 |
|
if (is_array($value) && array_key_exists($this->getName(), $value)) { |
|
79
|
14 |
|
return $value[$this->getName()]; |
|
80
|
9 |
|
} elseif (is_object($value)) { |
|
81
|
6 |
|
return TypeService::getPropertyValue($value, $this->getName()); |
|
82
|
3 |
|
} elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
83
|
3 |
|
return null; |
|
84
|
|
|
} else { |
|
85
|
1 |
|
throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName())); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function getName() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->nameCache; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
public function isDeprecated() |
|
96
|
|
|
{ |
|
97
|
3 |
|
return $this->getConfigValue('isDeprecated'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
public function getDeprecationReason() |
|
101
|
|
|
{ |
|
102
|
3 |
|
return $this->getConfigValue('deprecationReason'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|