|
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
|
112 |
|
public function __construct(array $configData, $contextObject = null, $finalClass = false) |
|
44
|
|
|
{ |
|
45
|
112 |
|
if (empty($configData)) { |
|
46
|
3 |
|
throw new ConfigurationException('Config for Type should be an array'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
109 |
|
$this->contextObject = $contextObject; |
|
50
|
109 |
|
$this->data = $configData; |
|
51
|
109 |
|
$this->finalClass = $finalClass; |
|
52
|
|
|
|
|
53
|
|
|
// $this->validate(); |
|
|
|
|
|
|
54
|
109 |
|
$this->build(); |
|
55
|
109 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function validate() |
|
58
|
|
|
{ |
|
59
|
|
|
$validator = ConfigValidator::getInstance(); |
|
60
|
|
|
|
|
61
|
|
|
if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) { |
|
62
|
|
|
throw new ConfigurationException('Config is not valid for ' . ($this->contextObject ? get_class($this->contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false))); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getContextRules() |
|
67
|
|
|
{ |
|
68
|
1 |
|
$rules = $this->getRules(); |
|
69
|
1 |
View Code Duplication |
if ($this->finalClass) { |
|
|
|
|
|
|
70
|
1 |
|
foreach ($rules as $name => $info) { |
|
71
|
1 |
|
if (!empty($info['final'])) { |
|
72
|
1 |
|
$rules[$name]['required'] = true; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return $rules; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
abstract public function getRules(); |
|
81
|
|
|
|
|
82
|
58 |
|
public function getName() |
|
83
|
|
|
{ |
|
84
|
58 |
|
return $this->get('name'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
public function getType() |
|
88
|
|
|
{ |
|
89
|
3 |
|
return $this->get('type'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
52 |
|
public function getData() |
|
93
|
|
|
{ |
|
94
|
52 |
|
return $this->data; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
20 |
|
public function getContextObject() |
|
98
|
|
|
{ |
|
99
|
20 |
|
return $this->contextObject; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
52 |
|
public function isFinalClass() |
|
103
|
|
|
{ |
|
104
|
52 |
|
return $this->finalClass; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
52 |
|
public function isExtraFieldsAllowed() |
|
108
|
|
|
{ |
|
109
|
52 |
|
return $this->extraFieldsAllowed; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return null|callable |
|
115
|
|
|
*/ |
|
116
|
26 |
|
public function getResolveFunction() |
|
117
|
|
|
{ |
|
118
|
26 |
|
return $this->get('resolve', null); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
67 |
|
protected function build() |
|
122
|
|
|
{ |
|
123
|
67 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $key |
|
127
|
|
|
* @param null $defaultValue |
|
128
|
|
|
* @return mixed|null|callable |
|
129
|
|
|
*/ |
|
130
|
76 |
|
public function get($key, $defaultValue = null) |
|
131
|
|
|
{ |
|
132
|
76 |
|
return array_key_exists($key, $this->data) ? $this->data[$key] : $defaultValue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
9 |
|
public function set($key, $value) |
|
136
|
|
|
{ |
|
137
|
9 |
|
$this->data[$key] = $value; |
|
138
|
|
|
|
|
139
|
9 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public function __call($method, $arguments) |
|
143
|
|
|
{ |
|
144
|
2 |
|
if (substr($method, 0, 3) == 'get') { |
|
145
|
1 |
|
$propertyName = lcfirst(substr($method, 3)); |
|
146
|
2 |
View Code Duplication |
} elseif (substr($method, 0, 3) == 'set') { |
|
|
|
|
|
|
147
|
1 |
|
$propertyName = lcfirst(substr($method, 3)); |
|
148
|
1 |
|
$this->set($propertyName, $arguments[0]); |
|
149
|
|
|
|
|
150
|
1 |
|
return $this; |
|
151
|
2 |
|
} elseif (substr($method, 0, 2) == 'is') { |
|
152
|
1 |
|
$propertyName = lcfirst(substr($method, 2)); |
|
153
|
|
|
} else { |
|
154
|
1 |
|
throw new \Exception('Call to undefined method ' . $method); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
return $this->get($propertyName); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.