1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of graphql-youshido project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 11/27/15 2:31 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Config; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Validator\ConfigValidator\ConfigValidator; |
13
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
14
|
|
|
use Youshido\GraphQL\Validator\Exception\ValidationException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Config |
18
|
|
|
* @package Youshido\GraphQL\Config |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractConfig |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $data = []; |
27
|
|
|
|
28
|
|
|
protected $contextObject; |
29
|
|
|
|
30
|
|
|
protected $finalClass = false; |
31
|
|
|
|
32
|
|
|
protected $extraFieldsAllowed = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* TypeConfig constructor. |
36
|
|
|
* @param array $configData |
37
|
|
|
* @param mixed $contextObject |
38
|
|
|
* @param bool $finalClass |
39
|
|
|
* |
40
|
|
|
* @throws ConfigurationException |
41
|
|
|
* @throws ValidationException |
42
|
|
|
*/ |
43
|
104 |
|
public function __construct(array $configData, $contextObject = null, $finalClass = false) |
44
|
|
|
{ |
45
|
104 |
|
if (empty($configData)) { |
46
|
3 |
|
throw new ConfigurationException('Config for Type should be an array'); |
47
|
|
|
} |
48
|
|
|
|
49
|
101 |
|
$this->contextObject = $contextObject; |
50
|
101 |
|
$this->data = $configData; |
51
|
101 |
|
$this->finalClass = $finalClass; |
52
|
|
|
|
53
|
101 |
|
$validator = ConfigValidator::getInstance(); |
54
|
|
|
|
55
|
101 |
|
if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) { |
56
|
20 |
|
throw new ConfigurationException('Config is not valid for ' . ($contextObject ? get_class($contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false))); |
57
|
|
|
} |
58
|
|
|
|
59
|
81 |
|
$this->build(); |
60
|
81 |
|
} |
61
|
|
|
|
62
|
101 |
|
public function getContextRules() |
63
|
|
|
{ |
64
|
101 |
|
$rules = $this->getRules(); |
65
|
101 |
|
if ($this->finalClass) { |
66
|
71 |
|
foreach ($rules as $name => $info) { |
67
|
71 |
|
if (!empty($info['final'])) { |
68
|
71 |
|
$rules[$name]['required'] = true; |
69
|
71 |
|
} |
70
|
71 |
|
} |
71
|
71 |
|
} |
72
|
|
|
|
73
|
101 |
|
return $rules; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
abstract public function getRules(); |
77
|
|
|
|
78
|
54 |
|
public function getName() |
79
|
|
|
{ |
80
|
54 |
|
return $this->get('name'); |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function getType() |
84
|
1 |
|
{ |
85
|
3 |
|
return $this->get('type'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return null|callable |
90
|
|
|
*/ |
91
|
23 |
|
public function getResolveFunction() |
92
|
|
|
{ |
93
|
23 |
|
return $this->get('resolve', null); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
1 |
|
public function getResolveString() |
100
|
|
|
{ |
101
|
1 |
|
return $this->get('resolveString', null); |
102
|
|
|
} |
103
|
|
|
|
104
|
49 |
|
protected function build() |
105
|
|
|
{ |
106
|
49 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $key |
110
|
|
|
* @param null $defaultValue |
111
|
|
|
* @return mixed|null|callable |
112
|
|
|
*/ |
113
|
68 |
|
public function get($key, $defaultValue = null) |
114
|
|
|
{ |
115
|
68 |
|
return array_key_exists($key, $this->data) ? $this->data[$key] : $defaultValue; |
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
public function set($key, $value) |
119
|
|
|
{ |
120
|
9 |
|
$this->data[$key] = $value; |
121
|
|
|
|
122
|
9 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function __call($method, $arguments) |
126
|
|
|
{ |
127
|
2 |
|
if (substr($method, 0, 3) == 'get') { |
128
|
1 |
|
$propertyName = lcfirst(substr($method, 3)); |
129
|
2 |
View Code Duplication |
} elseif (substr($method, 0, 3) == 'set') { |
|
|
|
|
130
|
1 |
|
$propertyName = lcfirst(substr($method, 3)); |
131
|
1 |
|
$this->set($propertyName, $arguments[0]); |
132
|
|
|
|
133
|
1 |
|
return $this; |
134
|
2 |
|
} elseif (substr($method, 0, 2) == 'is') { |
135
|
1 |
|
$propertyName = lcfirst(substr($method, 2)); |
136
|
1 |
|
} else { |
137
|
1 |
|
throw new \Exception('Call to undefined method ' . $method); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $this->get($propertyName); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.