1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 03/17/2017 |
4
|
|
|
* |
5
|
|
|
* @author Volodymyr Rashchepkin <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Config\Traits; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Directive\Directive; |
12
|
|
|
use Youshido\GraphQL\Field\InputField; |
13
|
|
|
|
14
|
|
|
trait DirectivesAwareConfigTrait |
15
|
|
|
{ |
16
|
|
|
protected $directives = []; |
17
|
|
|
protected $_isDirectivesBuilt; |
18
|
|
|
|
19
|
|
|
public function buildDirectives() |
20
|
|
|
{ |
21
|
|
|
if ($this->_isDirectivesBuilt) { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (!empty($this->data['directives'])) { |
26
|
|
|
$this->addDirectives($this->data['directives']); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
$this->_isDirectivesBuilt = true; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function addDirectives($directiveList) |
32
|
|
|
{ |
33
|
|
|
foreach ($directiveList as $directiveName => $directiveInfo) { |
34
|
|
|
if ($directiveInfo instanceof Directive) { |
35
|
|
|
$this->directives[$directiveInfo->getName()] = $directiveInfo; |
36
|
|
|
continue; |
37
|
|
|
} else { |
38
|
|
|
$this->addDirective($directiveName, $this->buildConfig($directiveName, $directiveInfo)); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addDirective($directive, $directiveInfo = null) |
46
|
|
|
{ |
47
|
|
|
if (!($directive instanceof Directive)) { |
48
|
|
|
$directive = new Directive($this->buildConfig($directive, $directiveInfo)); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
$this->directives[$directive->getName()] = $directive; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $name |
57
|
|
|
* |
58
|
|
|
* @return InputField |
59
|
|
|
*/ |
60
|
|
|
public function getDirective($name) |
61
|
|
|
{ |
62
|
|
|
return $this->hasDirective($name) ? $this->directives[$name] : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $name |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function hasDirective($name) |
71
|
|
|
{ |
72
|
|
|
return array_key_exists($name, $this->directives); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function hasDirectives() |
76
|
|
|
{ |
77
|
|
|
return !empty($this->directives); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return InputField[] |
82
|
|
|
*/ |
83
|
|
|
public function getDirectives() |
84
|
|
|
{ |
85
|
|
|
return $this->directives; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function removeDirective($name) |
89
|
|
|
{ |
90
|
|
|
if ($this->hasDirective($name)) { |
91
|
|
|
unset($this->directives[$name]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
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: