|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of GraphQL project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 3:40 PM 4/29/16 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Type; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Config\Traits\ConfigAwareTrait; |
|
13
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
|
14
|
|
|
|
|
15
|
|
|
final class NonNullType extends AbstractType implements CompositeTypeInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use ConfigAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
private $_typeOf; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* NonNullType constructor. |
|
23
|
|
|
* @param AbstractType|string $fieldType |
|
24
|
|
|
* @throws ConfigurationException |
|
25
|
|
|
*/ |
|
26
|
41 |
|
public function __construct($fieldType) |
|
27
|
|
|
{ |
|
28
|
41 |
|
if (!TypeService::isGraphQLType($fieldType)) { |
|
29
|
1 |
|
throw new ConfigurationException('NonNullType accepts only GraphpQL Types as argument'); |
|
30
|
|
|
} |
|
31
|
40 |
|
if (TypeService::isScalarType($fieldType)) { |
|
32
|
39 |
|
$fieldType = TypeFactory::getScalarType($fieldType); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
40 |
|
$this->_typeOf = $fieldType; |
|
36
|
40 |
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
public function getName() |
|
39
|
|
|
{ |
|
40
|
6 |
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
20 |
|
public function getKind() |
|
44
|
|
|
{ |
|
45
|
20 |
|
return TypeMap::KIND_NON_NULL; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function resolve($value) |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $value; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function isValidValue($value) |
|
54
|
|
|
{ |
|
55
|
4 |
|
return $value !== null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
10 |
|
public function isCompositeType() |
|
59
|
|
|
{ |
|
60
|
10 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
14 |
|
public function getNamedType() |
|
64
|
|
|
{ |
|
65
|
14 |
|
return $this->getTypeOf(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
33 |
|
public function getNullableType() |
|
69
|
|
|
{ |
|
70
|
33 |
|
return $this->getTypeOf(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
35 |
|
public function getTypeOf() |
|
74
|
|
|
{ |
|
75
|
35 |
|
return $this->_typeOf; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
public function parseValue($value) |
|
79
|
|
|
{ |
|
80
|
4 |
|
return $this->getNullableType()->parseValue($value); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.