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:07 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Config\Traits; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Field\InputField; |
13
|
|
|
|
14
|
|
|
trait ArgumentsAwareConfigTrait |
15
|
|
|
{ |
16
|
|
|
protected $arguments = []; |
17
|
|
|
protected $_isArgumentsBuilt; |
18
|
|
|
|
19
|
107 |
|
public function buildArguments() |
20
|
|
|
{ |
21
|
107 |
|
if ($this->_isArgumentsBuilt) { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
107 |
|
if (!empty($this->data['args'])) { |
26
|
67 |
|
$this->addArguments($this->data['args']); |
|
|
|
|
27
|
67 |
|
} |
28
|
107 |
|
$this->_isArgumentsBuilt = true; |
29
|
107 |
|
} |
30
|
|
|
|
31
|
68 |
|
public function addArguments($argsList) |
32
|
|
|
{ |
33
|
68 |
|
foreach ($argsList as $argumentName => $argumentInfo) { |
34
|
68 |
|
if ($argumentInfo instanceof InputField) { |
35
|
23 |
|
$this->arguments[$argumentInfo->getName()] = $argumentInfo; |
36
|
23 |
|
continue; |
37
|
|
|
} else { |
38
|
62 |
|
$this->addArgument($argumentName, $this->buildConfig($argumentName, $argumentInfo)); |
39
|
|
|
} |
40
|
68 |
|
} |
41
|
|
|
|
42
|
68 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
78 |
|
public function addArgument($argument, $argumentInfo = null) |
46
|
|
|
{ |
47
|
78 |
|
if (!($argument instanceof InputField)) { |
48
|
66 |
|
$argument = new InputField($this->buildConfig($argument, $argumentInfo)); |
49
|
66 |
|
} |
50
|
78 |
|
$this->arguments[$argument->getName()] = $argument; |
51
|
|
|
|
52
|
78 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
66 |
View Code Duplication |
protected function buildConfig($name, $info = null) |
|
|
|
|
56
|
|
|
{ |
57
|
66 |
|
if (!is_array($info)) { |
58
|
|
|
return [ |
59
|
51 |
|
'type' => $info, |
60
|
|
|
'name' => $name |
61
|
51 |
|
]; |
62
|
|
|
} |
63
|
66 |
|
if (empty($info['name'])) { |
64
|
28 |
|
$info['name'] = $name; |
65
|
28 |
|
} |
66
|
|
|
|
67
|
66 |
|
return $info; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $name |
72
|
|
|
* |
73
|
|
|
* @return InputField |
74
|
|
|
*/ |
75
|
38 |
|
public function getArgument($name) |
76
|
|
|
{ |
77
|
38 |
|
return $this->hasArgument($name) ? $this->arguments[$name] : null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $name |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
40 |
|
public function hasArgument($name) |
86
|
|
|
{ |
87
|
40 |
|
return array_key_exists($name, $this->arguments); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
public function hasArguments() |
91
|
|
|
{ |
92
|
8 |
|
return !empty($this->arguments); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return InputField[] |
97
|
|
|
*/ |
98
|
69 |
|
public function getArguments() |
99
|
|
|
{ |
100
|
69 |
|
return $this->arguments; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function removeArgument($name) |
104
|
|
|
{ |
105
|
2 |
|
if ($this->hasArgument($name)) { |
106
|
2 |
|
unset($this->arguments[$name]); |
107
|
2 |
|
} |
108
|
|
|
|
109
|
2 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
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: