11
11
12
12
use JsonSchema \Uri \UriRetriever ;
13
13
use JsonSchema \Validator ;
14
+ use JsonSchema \Entity \JsonPointer ;
14
15
15
16
/**
16
17
* The Base Constraints, all Validators should extend this class
@@ -80,10 +81,11 @@ public function setUriRetriever(UriRetriever $uriRetriever)
80
81
/**
81
82
* {@inheritDoc}
82
83
*/
83
- public function addError ($ path , $ message , $ constraint ='' , array $ more =null )
84
+ public function addError (JsonPointer $ path = null , $ message , $ constraint ='' , array $ more =null )
84
85
{
85
86
$ error = array (
86
- 'property ' => $ path ,
87
+ 'property ' => $ this ->convertJsonPointerIntoPropertyPath ($ path ?: new JsonPointer ('' )),
88
+ 'pointer ' => ltrim (strval ($ path ?: new JsonPointer ('' )), '# ' ),
87
89
'message ' => $ message ,
88
90
'constraint ' => $ constraint ,
89
91
);
@@ -132,37 +134,32 @@ public function reset()
132
134
/**
133
135
* Bubble down the path
134
136
*
135
- * @param string $path Current path
136
- * @param mixed $i What to append to the path
137
+ * @param JsonPointer|null $path Current path
138
+ * @param mixed $i What to append to the path
137
139
*
138
- * @return string
140
+ * @return JsonPointer;
139
141
*/
140
- protected function incrementPath ($ path , $ i )
142
+ protected function incrementPath (JsonPointer $ path = null , $ i )
141
143
{
142
- if ($ path !== '' ) {
143
- if (is_int ($ i )) {
144
- $ path .= '[ ' . $ i . '] ' ;
145
- } elseif ($ i == '' ) {
146
- $ path .= '' ;
147
- } else {
148
- $ path .= '. ' . $ i ;
149
- }
150
- } else {
151
- $ path = $ i ;
152
- }
153
-
144
+ $ path = $ path ?: new JsonPointer ('' );
145
+ $ path = $ path ->withPropertyPaths (
146
+ array_merge (
147
+ $ path ->getPropertyPaths (),
148
+ array_filter (array ($ i ), 'strlen ' )
149
+ )
150
+ );
154
151
return $ path ;
155
152
}
156
153
157
154
/**
158
155
* Validates an array
159
156
*
160
- * @param mixed $value
161
- * @param mixed $schema
162
- * @param mixed $path
163
- * @param mixed $i
157
+ * @param mixed $value
158
+ * @param mixed $schema
159
+ * @param JsonPointer|null $path
160
+ * @param mixed $i
164
161
*/
165
- protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
162
+ protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
166
163
{
167
164
$ validator = $ this ->getFactory ()->createInstanceFor ('collection ' );
168
165
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -173,13 +170,13 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
173
170
/**
174
171
* Validates an object
175
172
*
176
- * @param mixed $value
177
- * @param mixed $schema
178
- * @param mixed $path
179
- * @param mixed $i
180
- * @param mixed $patternProperties
173
+ * @param mixed $value
174
+ * @param mixed $schema
175
+ * @param JsonPointer|null $path
176
+ * @param mixed $i
177
+ * @param mixed $patternProperties
181
178
*/
182
- protected function checkObject ($ value , $ schema = null , $ path = null , $ i = null , $ patternProperties = null )
179
+ protected function checkObject ($ value , $ schema = null , JsonPointer $ path = null , $ i = null , $ patternProperties = null )
183
180
{
184
181
$ validator = $ this ->getFactory ()->createInstanceFor ('object ' );
185
182
$ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
@@ -190,12 +187,12 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
190
187
/**
191
188
* Validates the type of a property
192
189
*
193
- * @param mixed $value
194
- * @param mixed $schema
195
- * @param mixed $path
196
- * @param mixed $i
190
+ * @param mixed $value
191
+ * @param mixed $schema
192
+ * @param JsonPointer|null $path
193
+ * @param mixed $i
197
194
*/
198
- protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
195
+ protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
199
196
{
200
197
$ validator = $ this ->getFactory ()->createInstanceFor ('type ' );
201
198
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -206,12 +203,12 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
206
203
/**
207
204
* Checks a undefined element
208
205
*
209
- * @param mixed $value
210
- * @param mixed $schema
211
- * @param mixed $path
212
- * @param mixed $i
206
+ * @param mixed $value
207
+ * @param mixed $schema
208
+ * @param JsonPointer|null $path
209
+ * @param mixed $i
213
210
*/
214
- protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
211
+ protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
215
212
{
216
213
$ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
217
214
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -222,12 +219,12 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
222
219
/**
223
220
* Checks a string element
224
221
*
225
- * @param mixed $value
226
- * @param mixed $schema
227
- * @param mixed $path
228
- * @param mixed $i
222
+ * @param mixed $value
223
+ * @param mixed $schema
224
+ * @param JsonPointer|null $path
225
+ * @param mixed $i
229
226
*/
230
- protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
227
+ protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
231
228
{
232
229
$ validator = $ this ->getFactory ()->createInstanceFor ('string ' );
233
230
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -238,12 +235,12 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
238
235
/**
239
236
* Checks a number element
240
237
*
241
- * @param mixed $value
242
- * @param mixed $schema
243
- * @param mixed $path
244
- * @param mixed $i
238
+ * @param mixed $value
239
+ * @param mixed $schema
240
+ * @param JsonPointer $path
241
+ * @param mixed $i
245
242
*/
246
- protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
243
+ protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
247
244
{
248
245
$ validator = $ this ->getFactory ()->createInstanceFor ('number ' );
249
246
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -254,20 +251,28 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
254
251
/**
255
252
* Checks a enum element
256
253
*
257
- * @param mixed $value
258
- * @param mixed $schema
259
- * @param mixed $path
260
- * @param mixed $i
254
+ * @param mixed $value
255
+ * @param mixed $schema
256
+ * @param JsonPointer|null $path
257
+ * @param mixed $i
261
258
*/
262
- protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
259
+ protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
263
260
{
264
261
$ validator = $ this ->getFactory ()->createInstanceFor ('enum ' );
265
262
$ validator ->check ($ value , $ schema , $ path , $ i );
266
263
267
264
$ this ->addErrors ($ validator ->getErrors ());
268
265
}
269
266
270
- protected function checkFormat ($ value , $ schema = null , $ path = null , $ i = null )
267
+ /**
268
+ * Checks format of an element
269
+ *
270
+ * @param mixed $value
271
+ * @param mixed $schema
272
+ * @param JsonPointer|null $path
273
+ * @param mixed $i
274
+ */
275
+ protected function checkFormat ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
271
276
{
272
277
$ validator = $ this ->getFactory ()->createInstanceFor ('format ' );
273
278
$ validator ->check ($ value , $ schema , $ path , $ i );
@@ -298,4 +303,19 @@ protected function getTypeCheck()
298
303
{
299
304
return $ this ->getFactory ()->getTypeCheck ();
300
305
}
306
+
307
+ /**
308
+ * @param JsonPointer $pointer
309
+ * @return string property path
310
+ */
311
+ protected function convertJsonPointerIntoPropertyPath (JsonPointer $ pointer )
312
+ {
313
+ $ result = array_map (
314
+ function ($ path ) {
315
+ return sprintf (is_numeric ($ path ) ? '[%d] ' : '.%s ' , $ path );
316
+ },
317
+ $ pointer ->getPropertyPaths ()
318
+ );
319
+ return trim (implode ('' , $ result ), '. ' );
320
+ }
301
321
}
0 commit comments