|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 07.12.15 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Type\Object; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
|
12
|
|
|
use Youshido\GraphQL\Type\Config\Object\EnumTypeConfig; |
|
13
|
|
|
use Youshido\GraphQL\Type\Traits\AutoNameTrait; |
|
14
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractEnumType extends AbstractType |
|
17
|
|
|
{ |
|
18
|
|
|
use AutoNameTrait; |
|
19
|
|
|
/** |
|
20
|
|
|
* ObjectType constructor. |
|
21
|
|
|
* @param $config |
|
22
|
|
|
*/ |
|
23
|
9 |
|
public function __construct($config = []) |
|
24
|
|
|
{ |
|
25
|
9 |
|
if (empty($config)) { |
|
26
|
9 |
|
$config['name'] = $this->getName(); |
|
27
|
9 |
|
$config['values'] = $this->getValues(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
9 |
|
$this->config = new EnumTypeConfig($config, $this); |
|
|
|
|
|
|
31
|
9 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return String predefined type kind |
|
35
|
|
|
*/ |
|
36
|
6 |
|
public function getKind() |
|
37
|
|
|
{ |
|
38
|
6 |
|
return TypeMap::KIND_ENUM; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $value mixed |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function isValidValue($value) |
|
47
|
|
|
{ |
|
48
|
|
|
return in_array($value, array_map(function ($item) { return $item['value']; }, $this->getConfig()->get('values'))); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
abstract public function getValues(); |
|
52
|
|
|
|
|
53
|
|
|
public function build($config) {} |
|
54
|
|
|
|
|
55
|
1 |
View Code Duplication |
public function serialize($value) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
1 |
|
foreach ($this->getConfig()->get('values') as $valueItem) { |
|
58
|
1 |
|
if ($value == $valueItem['name']) { |
|
59
|
1 |
|
return $valueItem['value']; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
View Code Duplication |
public function resolve($value) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
1 |
|
foreach ($this->getConfig()->get('values') as $valueItem) { |
|
67
|
1 |
|
if ($value == $valueItem['value']) { |
|
68
|
1 |
|
return $valueItem['name']; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..