Skip to content

Commit fbc4336

Browse files
committed
display errors with indenting
1 parent 79eac4b commit fbc4336

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

json-result.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</h1>
2020

2121
<?php if($error): ?>
22-
<div class="alert alert-danger" role="alert"><?php echo htmlspecialchars($error); ?></div>
22+
<div class="alert alert-danger" role="alert" style="white-space: pre-wrap;"><?php echo htmlspecialchars($error); ?></div>
2323
<?php else: ?>
2424
<div class="alert alert-success" role="alert">Valid Microformats JSON!</div>
2525
<?php endif ?>

json-validator.php

+28-24
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,94 @@
11
<?php
22

3-
function is_valid_mf2_list($input) {
3+
function is_valid_mf2_list($input, $level=0) {
4+
$indent = str_repeat(" ",$level);
5+
46
// Input to this function must be an array
57
if(!is_object($input))
6-
return [false, 'Input was not an object'];
8+
return [false, $indent.'Input was not an object'];
79

810
if(!isset($input->items))
9-
return [false, 'Input is missing a top-level "items" property'];
11+
return [false, $indent.'Input is missing a top-level "items" property'];
1012

1113
if(!is_array($input->items))
12-
return [false, 'The "items" property must be an array'];
14+
return [false, $indent.'The "items" property must be an array'];
1315

1416
// Every item must be valid
1517
foreach($input->items as $i=>$item) {
16-
list($valid, $error) = is_valid_mf2_object($item);
18+
list($valid, $error) = is_valid_mf2_object($item, $level+1);
1719
if(!$valid) {
18-
return [false, 'Item '.$i.' was invalid: '.$error];
20+
return [false, $indent.'Item '.$i.' was invalid:'."\n".$error];
1921
}
2022
}
2123

2224
return [true, null];
2325
}
2426

25-
function is_valid_mf2_object($input) {
27+
function is_valid_mf2_object($input, $level=0) {
28+
$indent = str_repeat(" ",$level);
29+
2630
// Input to this function must be an array
2731
if(!is_object($input))
28-
return [false, 'Input was not an object'];
32+
return [false, $indent.'Input was not an object'];
2933

3034
// Keys type and properties are required at a minimum and must be arrays
3135
if(!isset($input->type))
32-
return [false, 'Item is missing the "type" property'];
36+
return [false, $indent.'Item is missing the "type" property'];
3337

3438
if(!is_array($input->type))
35-
return [false, 'The "type" property is not an array'];
39+
return [false, $indent.'The "type" property is not an array'];
3640

3741
if(!isset($input->properties))
38-
return [false, 'Item is missing the "properties" property'];
42+
return [false, $indent.'Item is missing the "properties" property'];
3943

4044
if(!is_object($input->properties))
41-
return [false, 'The "properties" property is not an object'];
45+
return [false, $indent.'The "properties" property is not an object'];
4246

4347
// Every value of type must be a string beginning with h-
4448
foreach($input->type as $type) {
4549
if(!is_string($type) || substr($type, 0, 2) != 'h-')
46-
return [false, 'Every type must be an h-* value, got: "'.$type.'"'];
50+
return [false, $indent.'Every type must be an h-* value, got: "'.$type.'"'];
4751
}
4852

4953
foreach($input->properties as $key=>$property) {
5054
// Every property must be an array
5155
if(!is_array($property))
52-
return [false, 'One of the values of "'.$key.'" is not an array'];
56+
return [false, $indent.'One of the values of "'.$key.'" is not an array'];
5357

5458
// If a value of a property is not a string, it must be a valid mf2 object
5559
foreach($property as $k=>$val) {
5660
if(is_object($val)) {
5761
// Try to detect e- parsed objects
5862
if(property_exists($val, 'value') && !property_exists($val, 'html'))
59-
return [false, 'One of the values of '.$key.' is missing the "html" property'];
63+
return [false, $indent.'One of the values of '.$key.' is missing the "html" property'];
6064

6165
if(property_exists($val, 'html') && !property_exists($val, 'value'))
62-
return [false, 'One of the values of '.$key.' is missing the "value" property'];
66+
return [false, $indent.'One of the values of '.$key.' is missing the "value" property'];
6367

6468
// Otherwise this must be a nested object
65-
list($valid, $error) = is_valid_mf2_object($val);
69+
list($valid, $error) = is_valid_mf2_object($val, $level+1);
6670
if(!$valid)
67-
return [false, 'One of the values of "'.$key.'" is not a valid mf2 object: '.$error];
71+
return [false, $indent.'One of the values of "'.$key.'" is not a valid mf2 object:'."\n".$error];
6872

6973
} else if(!is_string($val)) {
7074
if(is_numeric($val))
71-
return [false, 'One of the values of "'.$key.'" is a number instead of a string'];
75+
return [false, $indent.'One of the values of "'.$key.'" is a number instead of a string'];
7276

73-
list($valid, $error) = is_valid_mf2_object($val);
77+
list($valid, $error) = is_valid_mf2_object($val, $level+1);
7478
if($error)
75-
return [false, 'One of the values of "'.$key.'" is not a valid mf2 object'];
79+
return [false, $indent.'One of the values of "'.$key.'" is not a valid mf2 object'."\n"];
7680
}
7781
}
7882
}
7983

8084
if(isset($input->children)) {
8185
if(!is_array($input->children))
82-
return [false, 'The "children" property must be an array'];
86+
return [false, $indent.'The "children" property must be an array'];
8387

8488
foreach($input->children as $child) {
85-
list($valid, $error) = is_valid_mf2_object($child);
89+
list($valid, $error) = is_valid_mf2_object($child, $level+1);
8690
if(!$valid) {
87-
return [false, 'One of the child objects was not valid: '.$error];
91+
return [false, $indent.'One of the child objects was not valid:'."\n".$error."\n"];
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)