1111
1212use JsonSchema \Uri \UriRetriever ;
1313use JsonSchema \Validator ;
14+ use JsonSchema \Entity \JsonPointer ;
1415
1516/**
1617 * The Base Constraints, all Validators should extend this class
@@ -80,10 +81,11 @@ public function setUriRetriever(UriRetriever $uriRetriever)
8081 /**
8182 * {@inheritDoc}
8283 */
83- public function addError ($ path , $ message , $ constraint ='' , array $ more =null )
84+ public function addError (JsonPointer $ path = null , $ message , $ constraint ='' , array $ more =null )
8485 {
8586 $ error = array (
86- 'property ' => $ path ,
87+ 'property ' => $ this ->convertJsonPointerIntoPropertyPath ($ path ?: new JsonPointer ('' )),
88+ 'pointer ' => ltrim (strval ($ path ?: new JsonPointer ('' )), '# ' ),
8789 'message ' => $ message ,
8890 'constraint ' => $ constraint ,
8991 );
@@ -132,37 +134,32 @@ public function reset()
132134 /**
133135 * Bubble down the path
134136 *
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
137139 *
138- * @return string
140+ * @return JsonPointer;
139141 */
140- protected function incrementPath ($ path , $ i )
142+ protected function incrementPath (JsonPointer $ path = null , $ i )
141143 {
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+ );
154151 return $ path ;
155152 }
156153
157154 /**
158155 * Validates an array
159156 *
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
164161 */
165- protected function checkArray ($ value , $ schema = null , $ path = null , $ i = null )
162+ protected function checkArray ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
166163 {
167164 $ validator = $ this ->getFactory ()->createInstanceFor ('collection ' );
168165 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -173,13 +170,13 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
173170 /**
174171 * Validates an object
175172 *
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
181178 */
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 )
183180 {
184181 $ validator = $ this ->getFactory ()->createInstanceFor ('object ' );
185182 $ validator ->check ($ value , $ schema , $ path , $ i , $ patternProperties );
@@ -190,12 +187,12 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
190187 /**
191188 * Validates the type of a property
192189 *
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
197194 */
198- protected function checkType ($ value , $ schema = null , $ path = null , $ i = null )
195+ protected function checkType ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
199196 {
200197 $ validator = $ this ->getFactory ()->createInstanceFor ('type ' );
201198 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -206,12 +203,12 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
206203 /**
207204 * Checks a undefined element
208205 *
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
213210 */
214- protected function checkUndefined ($ value , $ schema = null , $ path = null , $ i = null )
211+ protected function checkUndefined ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
215212 {
216213 $ validator = $ this ->getFactory ()->createInstanceFor ('undefined ' );
217214 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -222,12 +219,12 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
222219 /**
223220 * Checks a string element
224221 *
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
229226 */
230- protected function checkString ($ value , $ schema = null , $ path = null , $ i = null )
227+ protected function checkString ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
231228 {
232229 $ validator = $ this ->getFactory ()->createInstanceFor ('string ' );
233230 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -238,12 +235,12 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
238235 /**
239236 * Checks a number element
240237 *
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
245242 */
246- protected function checkNumber ($ value , $ schema = null , $ path = null , $ i = null )
243+ protected function checkNumber ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
247244 {
248245 $ validator = $ this ->getFactory ()->createInstanceFor ('number ' );
249246 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -254,20 +251,28 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
254251 /**
255252 * Checks a enum element
256253 *
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
261258 */
262- protected function checkEnum ($ value , $ schema = null , $ path = null , $ i = null )
259+ protected function checkEnum ($ value , $ schema = null , JsonPointer $ path = null , $ i = null )
263260 {
264261 $ validator = $ this ->getFactory ()->createInstanceFor ('enum ' );
265262 $ validator ->check ($ value , $ schema , $ path , $ i );
266263
267264 $ this ->addErrors ($ validator ->getErrors ());
268265 }
269266
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 )
271276 {
272277 $ validator = $ this ->getFactory ()->createInstanceFor ('format ' );
273278 $ validator ->check ($ value , $ schema , $ path , $ i );
@@ -298,4 +303,19 @@ protected function getTypeCheck()
298303 {
299304 return $ this ->getFactory ()->getTypeCheck ();
300305 }
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+ }
301321}
0 commit comments