|
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\Type\Config\Traits; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Type\Field\InputField; |
|
13
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
14
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
|
15
|
|
|
|
|
16
|
|
|
trait ArgumentsAwareTrait |
|
17
|
|
|
{ |
|
18
|
|
|
protected $arguments = []; |
|
19
|
|
|
|
|
20
|
20 |
|
public function buildArguments() |
|
21
|
|
|
{ |
|
22
|
20 |
|
$sourceArguments = empty($this->data['args']) ? [] : $this->data['args']; |
|
|
|
|
|
|
23
|
20 |
|
foreach ($sourceArguments as $argumentName => $argumentInfo) { |
|
24
|
11 |
|
$this->addArgument($argumentName, $argumentInfo['type'], $argumentInfo); |
|
25
|
20 |
|
} |
|
26
|
20 |
|
} |
|
27
|
|
|
|
|
28
|
12 |
|
public function addArgument($name, $type, $config = []) |
|
29
|
|
|
{ |
|
30
|
12 |
|
if (!TypeMap::isInputType($type)) { |
|
31
|
|
|
throw new ConfigurationException('Argument input type ' . $type . ' is not supported'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
12 |
|
$config['name'] = $name; |
|
35
|
12 |
|
$config['type'] = is_string($type) ? TypeMap::getScalarTypeObject($type) : $type; |
|
36
|
|
|
|
|
37
|
12 |
|
$this->arguments[$name] = new InputField($config); |
|
38
|
|
|
|
|
39
|
12 |
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $name |
|
44
|
|
|
* |
|
45
|
|
|
* @return InputField |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function getArgument($name) |
|
48
|
|
|
{ |
|
49
|
5 |
|
return $this->hasArgument($name) ? $this->arguments[$name] : null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param $name |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function hasArgument($name) |
|
58
|
|
|
{ |
|
59
|
5 |
|
return array_key_exists($name, $this->arguments); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return InputField[] |
|
64
|
|
|
*/ |
|
65
|
13 |
|
public function getArguments() |
|
66
|
|
|
{ |
|
67
|
13 |
|
return $this->arguments; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function removeArgument($name) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->hasArgument($name)) { |
|
73
|
|
|
unset($this->arguments[$name]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
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: