22
22
use CloudCreativity \JsonApi \Error \ErrorObject ;
23
23
use CloudCreativity \JsonApi \Error \SourceObject ;
24
24
use CloudCreativity \JsonApi \Validator \AbstractValidator ;
25
- use CloudCreativity \JsonApi \Validator \Type \StringValidator ;
25
+ use CloudCreativity \JsonApi \Validator \Helper \AllowedKeysTrait ;
26
+ use CloudCreativity \JsonApi \Validator \Helper \RequiredKeysTrait ;
26
27
use CloudCreativity \JsonApi \Validator \Type \TypeValidator ;
27
28
use CloudCreativity \JsonApi \Contracts \Validator \ValidatorInterface ;
28
29
33
34
class AttributesValidator extends AbstractValidator implements ConfigurableInterface
34
35
{
35
36
37
+ use RequiredKeysTrait,
38
+ AllowedKeysTrait;
39
+
36
40
// Config constants
37
41
const ALLOWED = 'allowed ' ;
38
42
const REQUIRED = 'required ' ;
@@ -66,62 +70,13 @@ class AttributesValidator extends AbstractValidator implements ConfigurableInter
66
70
],
67
71
];
68
72
69
- /**
70
- * @var array|null
71
- * null means any allowed, array means only the supplied keys are allowed.
72
- */
73
- protected $ _allowed ;
74
-
75
- /**
76
- * @var array|null
77
- */
78
- protected $ _required ;
79
-
80
73
/**
81
74
* Validators for use with keys within the attributes.
82
75
*
83
76
* @var array
84
77
*/
85
78
protected $ _validators = [];
86
79
87
- /**
88
- * @param array $keys
89
- * @return $this
90
- */
91
- public function setAllowed (array $ keys )
92
- {
93
- $ this ->_allowed = $ keys ;
94
-
95
- return $ this ;
96
- }
97
-
98
- /**
99
- * @return bool
100
- */
101
- public function isAllowed ($ key )
102
- {
103
- return is_array ($ this ->_allowed ) ? in_array ($ key , $ this ->_allowed ) : true ;
104
- }
105
-
106
- /**
107
- * @param array $keys
108
- * @return $this
109
- */
110
- public function setRequired (array $ keys )
111
- {
112
- $ this ->_required = $ keys ;
113
-
114
- return $ this ;
115
- }
116
-
117
- /**
118
- * @return array
119
- */
120
- public function getRequired ()
121
- {
122
- return (array ) $ this ->_required ;
123
- }
124
-
125
80
/**
126
81
* @param $key
127
82
* @param ValidatorInterface $validator
@@ -179,18 +134,30 @@ public function attr($key, $type = null, array $options = [])
179
134
return $ this ;
180
135
}
181
136
137
+ /**
138
+ * Only allow keys that have validators.
139
+ *
140
+ * @return $this
141
+ */
142
+ public function setRestricted ()
143
+ {
144
+ $ this ->setAllowedKeys (array_keys ($ this ->_validators ));
145
+
146
+ return $ this ;
147
+ }
148
+
182
149
/**
183
150
* @param array $config
184
151
* @return $this
185
152
*/
186
153
public function configure (array $ config )
187
154
{
188
155
if (isset ($ config [static ::ALLOWED ]) && is_array ($ config [static ::ALLOWED ])) {
189
- $ this ->setAllowed ($ config [static ::ALLOWED ]);
156
+ $ this ->setAllowedKeys ($ config [static ::ALLOWED ]);
190
157
}
191
158
192
159
if (isset ($ config [static ::REQUIRED ]) && is_array ($ config [static ::REQUIRED ])) {
193
- $ this ->setRequired ($ config [static ::REQUIRED ]);
160
+ $ this ->setRequiredKeys ($ config [static ::REQUIRED ]);
194
161
}
195
162
196
163
return $ this ;
@@ -213,7 +180,7 @@ protected function validate($value)
213
180
}
214
181
215
182
// Check that required keys exist.
216
- foreach ($ this ->getRequired () as $ key ) {
183
+ foreach ($ this ->getRequiredKeys () as $ key ) {
217
184
218
185
if (!isset ($ value ->{$ key })) {
219
186
$ err = $ this ->error (static ::ERROR_REQUIRED_ATTRIBUTE );
@@ -230,7 +197,7 @@ protected function checkKey($key)
230
197
{
231
198
$ pointer = '/ ' . $ key ;
232
199
233
- if (!$ this ->isAllowed ($ key )) {
200
+ if (!$ this ->isAllowedKey ($ key )) {
234
201
$ this ->error (static ::ERROR_UNRECOGNISED_ATTRIBUTE )
235
202
->setSource ([
236
203
SourceObject::POINTER => $ pointer ,
0 commit comments