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) return true; |
22
|
|
|
|
23
|
|
|
if (!empty($this->data['directives'])) { |
24
|
|
|
$this->addDirectives($this->data['directives']); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
$this->_isDirectivesBuilt = true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function addDirectives($directiveList) |
30
|
|
|
{ |
31
|
|
|
foreach ($directiveList as $directiveName => $directiveInfo) { |
32
|
|
|
if ($directiveInfo instanceof Directive) { |
33
|
|
|
$this->directives[$directiveInfo->getName()] = $directiveInfo; |
34
|
|
|
continue; |
35
|
|
|
} else { |
36
|
|
|
$this->addDirective($directiveName, $this->buildConfig($directiveName, $directiveInfo)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addDirective($directive, $directiveInfo = null) |
44
|
|
|
{ |
45
|
|
|
if (!($directive instanceof Directive)) { |
46
|
|
|
$directive = new Directive($this->buildConfig($directive, $directiveInfo)); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
$this->directives[$directive->getName()] = $directive; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $name |
55
|
|
|
* |
56
|
|
|
* @return InputField |
57
|
|
|
*/ |
58
|
|
|
public function getDirective($name) |
59
|
|
|
{ |
60
|
|
|
return $this->hasDirective($name) ? $this->directives[$name] : null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $name |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function hasDirective($name) |
69
|
|
|
{ |
70
|
|
|
return array_key_exists($name, $this->directives); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function hasDirectives() |
74
|
|
|
{ |
75
|
|
|
return !empty($this->directives); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return InputField[] |
80
|
|
|
*/ |
81
|
|
|
public function getDirectives() |
82
|
|
|
{ |
83
|
|
|
return $this->directives; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function removeDirective($name) |
87
|
|
|
{ |
88
|
|
|
if ($this->hasDirective($name)) { |
89
|
|
|
unset($this->directives[$name]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
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: