1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of graphql-youshido project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 12/1/15 11:05 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Config\Traits; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Field\Field; |
13
|
|
|
use Youshido\GraphQL\Field\FieldInterface; |
14
|
|
|
use Youshido\GraphQL\Field\InputFieldInterface; |
15
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FieldsAwareTrait |
19
|
|
|
* @package Youshido\GraphQL\Config\Traits |
20
|
|
|
*/ |
21
|
|
|
trait FieldsAwareConfigTrait |
22
|
|
|
{ |
23
|
|
|
protected $fields = []; |
24
|
|
|
|
25
|
121 |
|
public function buildFields() |
26
|
|
|
{ |
27
|
121 |
|
if (!empty($this->data['fields'])) { |
28
|
85 |
|
$this->addFields($this->data['fields']); |
|
|
|
|
29
|
85 |
|
} |
30
|
121 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add fields from passed interface |
34
|
|
|
* @param AbstractInterfaceType $interfaceType |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
1 |
|
public function applyInterface(AbstractInterfaceType $interfaceType) |
38
|
|
|
{ |
39
|
1 |
|
$this->addFields($interfaceType->getFields()); |
40
|
|
|
|
41
|
1 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $fieldsList |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
88 |
|
public function addFields($fieldsList) |
49
|
|
|
{ |
50
|
88 |
|
foreach ($fieldsList as $fieldName => $fieldConfig) { |
51
|
|
|
|
52
|
88 |
|
if ($fieldConfig instanceof FieldInterface) { |
53
|
9 |
|
$this->fields[$fieldConfig->getName()] = $fieldConfig; |
54
|
9 |
|
continue; |
55
|
82 |
|
} elseif($fieldConfig instanceof InputFieldInterface) { |
56
|
2 |
|
$this->fields[$fieldConfig->getName()] = $fieldConfig; |
57
|
2 |
|
continue; |
58
|
|
|
} else { |
59
|
82 |
|
$this->addField($fieldName, $this->buildFieldConfig($fieldName, $fieldConfig)); |
60
|
|
|
} |
61
|
88 |
|
} |
62
|
|
|
|
63
|
88 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param FieldInterface|string $field Field name or Field Object |
68
|
|
|
* @param mixed $fieldInfo Field Type or Field Config array |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
98 |
|
public function addField($field, $fieldInfo = null) |
72
|
|
|
{ |
73
|
98 |
|
if (!($field instanceof FieldInterface)) { |
74
|
92 |
|
$field = new Field($this->buildFieldConfig($field, $fieldInfo)); |
75
|
92 |
|
} |
76
|
|
|
|
77
|
98 |
|
$this->fields[$field->getName()] = $field; |
78
|
|
|
|
79
|
98 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
92 |
View Code Duplication |
protected function buildFieldConfig($name, $info = null) |
|
|
|
|
83
|
|
|
{ |
84
|
92 |
|
if (!is_array($info)) { |
85
|
|
|
$info = [ |
86
|
68 |
|
'type' => $info, |
87
|
68 |
|
'name' => $name, |
88
|
68 |
|
]; |
89
|
92 |
|
} elseif (empty($info['name'])) { |
90
|
76 |
|
$info['name'] = $name; |
91
|
76 |
|
} |
92
|
|
|
|
93
|
92 |
|
return $info; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $name |
98
|
|
|
* |
99
|
|
|
* @return Field |
100
|
|
|
*/ |
101
|
76 |
|
public function getField($name) |
102
|
|
|
{ |
103
|
76 |
|
return $this->hasField($name) ? $this->fields[$name] : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $name |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
80 |
|
public function hasField($name) |
112
|
|
|
{ |
113
|
80 |
|
return array_key_exists($name, $this->fields); |
114
|
|
|
} |
115
|
|
|
|
116
|
84 |
|
public function hasFields() |
117
|
|
|
{ |
118
|
84 |
|
return !empty($this->fields); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Field[] |
123
|
|
|
*/ |
124
|
84 |
|
public function getFields() |
125
|
|
|
{ |
126
|
84 |
|
return $this->fields; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function removeField($name) |
130
|
|
|
{ |
131
|
1 |
|
if ($this->hasField($name)) { |
132
|
1 |
|
unset($this->fields[$name]); |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: