1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Traits; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Annotation\Plugin\PluginConfigAnnotation; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\MetaAwareInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class MetaAwareTrait |
18
|
|
|
*/ |
19
|
|
|
trait MetaAwareTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $metas = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
2 |
|
public function getMetas(): array |
30
|
|
|
{ |
31
|
2 |
|
return $this->metas; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $key |
36
|
|
|
* @param null|mixed $default |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
40 |
|
public function getMeta(string $key, $default = null) |
41
|
|
|
{ |
42
|
40 |
|
return $this->metas[$key] ?? $default; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $key |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
3 |
|
public function hasMeta(string $key): bool |
51
|
|
|
{ |
52
|
3 |
|
return array_key_exists($key, $this->metas); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $metas |
57
|
|
|
* |
58
|
|
|
* @return MetaAwareInterface |
59
|
|
|
*/ |
60
|
37 |
|
public function setMetas(array $metas): MetaAwareInterface |
61
|
|
|
{ |
62
|
37 |
|
$this->metas = $metas; |
63
|
|
|
|
64
|
|
|
//convert pluginAnnotation meta to correct key:value pair |
65
|
37 |
|
foreach ($this->metas as $index => $meta) { |
66
|
11 |
|
if ($meta instanceof PluginConfigAnnotation) { |
67
|
11 |
|
unset($this->metas[$index]); |
68
|
11 |
|
$this->setMeta($meta->getName(), $meta->getConfig()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
37 |
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $key |
77
|
|
|
* @param mixed $value |
78
|
|
|
* |
79
|
|
|
* @return MetaAwareInterface |
80
|
|
|
*/ |
81
|
35 |
|
public function setMeta(string $key, $value): MetaAwareInterface |
82
|
|
|
{ |
83
|
|
|
//convert pluginAnnotation meta to correct key:value pair |
84
|
35 |
|
if ($value instanceof PluginConfigAnnotation) { |
85
|
14 |
|
$key = $value->getName(); |
86
|
14 |
|
$value = $value->getConfig(); |
87
|
|
|
} |
88
|
|
|
|
89
|
35 |
|
$this->metas[$key] = $value; |
90
|
|
|
|
91
|
35 |
|
return $this; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $key |
96
|
|
|
* |
97
|
|
|
* @return MetaAwareInterface |
98
|
|
|
*/ |
99
|
1 |
|
public function removeMeta(string $key): MetaAwareInterface |
100
|
|
|
{ |
101
|
1 |
|
unset($this->metas[$key]); |
102
|
|
|
|
103
|
1 |
|
return $this; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|