|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of graphql-youshido project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 11/28/15 2:25 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Validator\ConfigValidator; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Validator\ConfigValidator\Rules\TypeValidationRule; |
|
13
|
|
|
use Youshido\GraphQL\Validator\ConfigValidator\Rules\ValidationRuleInterface; |
|
14
|
|
|
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait; |
|
15
|
|
|
use Youshido\GraphQL\Validator\Exception\ValidationException; |
|
16
|
|
|
|
|
17
|
|
|
class ConfigValidator implements ConfigValidatorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
use ErrorContainerTrait; |
|
21
|
|
|
|
|
22
|
|
|
protected $rules = []; |
|
23
|
|
|
|
|
24
|
|
|
protected $extraFieldsAllowed = false; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ValidationRuleInterface[] */ |
|
27
|
|
|
protected $validationRules = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ConfigValidator */ |
|
30
|
|
|
protected static $instance; |
|
31
|
|
|
|
|
32
|
|
|
private function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->initializeRules(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return ConfigValidator |
|
39
|
|
|
*/ |
|
40
|
136 |
|
public static function getInstance() |
|
41
|
|
|
{ |
|
42
|
136 |
|
if (empty(self::$instance)) { |
|
43
|
|
|
self::$instance = new self(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
136 |
|
self::$instance->clearErrors(); |
|
47
|
|
|
|
|
48
|
136 |
|
return self::$instance; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
110 |
|
public function validate($data, $rules = [], $extraFieldsAllowed = null) |
|
52
|
|
|
{ |
|
53
|
110 |
|
if ($extraFieldsAllowed !== null) $this->setExtraFieldsAllowed($extraFieldsAllowed); |
|
54
|
|
|
|
|
55
|
110 |
|
$processedFields = []; |
|
56
|
110 |
|
foreach ($rules as $fieldName => $fieldRules) { |
|
57
|
110 |
|
$processedFields[] = $fieldName; |
|
58
|
|
|
|
|
59
|
|
|
/** Custom validation of 'required' property */ |
|
60
|
110 |
|
if (array_key_exists('required', $fieldRules)) { |
|
61
|
97 |
|
unset($fieldRules['required']); |
|
62
|
|
|
|
|
63
|
97 |
|
if (!array_key_exists($fieldName, $data)) { |
|
64
|
12 |
|
$this->addError(new ValidationException(sprintf('Field "%s" is required', $fieldName))); |
|
65
|
|
|
|
|
66
|
12 |
|
continue; |
|
67
|
|
|
} |
|
68
|
110 |
|
} elseif (!array_key_exists($fieldName, $data)) { |
|
69
|
105 |
|
continue; |
|
70
|
|
|
} |
|
71
|
109 |
|
if (!empty($fieldRules['final'])) unset($fieldRules['final']); |
|
72
|
|
|
|
|
73
|
|
|
/** Validation of all other rules*/ |
|
74
|
109 |
|
foreach ($fieldRules as $ruleName => $ruleInfo) { |
|
75
|
109 |
|
if (!array_key_exists($ruleName, $this->validationRules)) { |
|
76
|
1 |
|
$this->addError(new ValidationException(sprintf('Field "%s" has invalid rule "%s"', $fieldName, $ruleInfo))); |
|
77
|
|
|
|
|
78
|
1 |
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
109 |
|
if (!$this->validationRules[$ruleName]->validate($data[$fieldName], $ruleInfo)) { |
|
82
|
13 |
|
$this->addError(new ValidationException(sprintf('Field "%s" expected to be "%s" but got "%s"', $fieldName, $ruleName, gettype($data[$fieldName])))); |
|
83
|
13 |
|
} |
|
84
|
109 |
|
} |
|
85
|
110 |
|
} |
|
86
|
|
|
|
|
87
|
110 |
|
if (!$this->isExtraFieldsAllowed()) { |
|
88
|
3 |
|
foreach (array_keys($data) as $fieldName) { |
|
89
|
3 |
|
if (!in_array($fieldName, $processedFields)) { |
|
90
|
1 |
|
$this->addError(new ValidationException(sprintf('Field "%s" is not expected', $fieldName))); |
|
91
|
1 |
|
} |
|
92
|
3 |
|
} |
|
93
|
3 |
|
} |
|
94
|
|
|
|
|
95
|
110 |
|
return $this->isValid(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function initializeRules() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->validationRules['type'] = new TypeValidationRule($this); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function addRule($name, ValidationRuleInterface $rule) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->validationRules[$name] = $rule; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
110 |
|
public function isValid() |
|
109
|
|
|
{ |
|
110
|
110 |
|
return !$this->hasErrors(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
*/ |
|
117
|
110 |
|
public function isExtraFieldsAllowed() |
|
118
|
|
|
{ |
|
119
|
110 |
|
return $this->extraFieldsAllowed; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param boolean $extraFieldsAllowed |
|
124
|
|
|
* |
|
125
|
|
|
* @return ConfigValidator |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function setExtraFieldsAllowed($extraFieldsAllowed) |
|
128
|
|
|
{ |
|
129
|
1 |
|
$this->extraFieldsAllowed = $extraFieldsAllowed; |
|
130
|
|
|
|
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|