|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -function is_valid_mf2_list($input) { |
| 3 | +function is_valid_mf2_list($input, $level=0) { |
| 4 | + $indent = str_repeat(" ",$level); |
| 5 | + |
4 | 6 | // Input to this function must be an array
|
5 | 7 | if(!is_object($input))
|
6 |
| - return [false, 'Input was not an object']; |
| 8 | + return [false, $indent.'Input was not an object']; |
7 | 9 |
|
8 | 10 | 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']; |
10 | 12 |
|
11 | 13 | 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']; |
13 | 15 |
|
14 | 16 | // Every item must be valid
|
15 | 17 | 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); |
17 | 19 | if(!$valid) {
|
18 |
| - return [false, 'Item '.$i.' was invalid: '.$error]; |
| 20 | + return [false, $indent.'Item '.$i.' was invalid:'."\n".$error]; |
19 | 21 | }
|
20 | 22 | }
|
21 | 23 |
|
22 | 24 | return [true, null];
|
23 | 25 | }
|
24 | 26 |
|
25 |
| -function is_valid_mf2_object($input) { |
| 27 | +function is_valid_mf2_object($input, $level=0) { |
| 28 | + $indent = str_repeat(" ",$level); |
| 29 | + |
26 | 30 | // Input to this function must be an array
|
27 | 31 | if(!is_object($input))
|
28 |
| - return [false, 'Input was not an object']; |
| 32 | + return [false, $indent.'Input was not an object']; |
29 | 33 |
|
30 | 34 | // Keys type and properties are required at a minimum and must be arrays
|
31 | 35 | if(!isset($input->type))
|
32 |
| - return [false, 'Item is missing the "type" property']; |
| 36 | + return [false, $indent.'Item is missing the "type" property']; |
33 | 37 |
|
34 | 38 | 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']; |
36 | 40 |
|
37 | 41 | if(!isset($input->properties))
|
38 |
| - return [false, 'Item is missing the "properties" property']; |
| 42 | + return [false, $indent.'Item is missing the "properties" property']; |
39 | 43 |
|
40 | 44 | 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']; |
42 | 46 |
|
43 | 47 | // Every value of type must be a string beginning with h-
|
44 | 48 | foreach($input->type as $type) {
|
45 | 49 | 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.'"']; |
47 | 51 | }
|
48 | 52 |
|
49 | 53 | foreach($input->properties as $key=>$property) {
|
50 | 54 | // Every property must be an array
|
51 | 55 | 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']; |
53 | 57 |
|
54 | 58 | // If a value of a property is not a string, it must be a valid mf2 object
|
55 | 59 | foreach($property as $k=>$val) {
|
56 | 60 | if(is_object($val)) {
|
57 | 61 | // Try to detect e- parsed objects
|
58 | 62 | 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']; |
60 | 64 |
|
61 | 65 | 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']; |
63 | 67 |
|
64 | 68 | // 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); |
66 | 70 | 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]; |
68 | 72 |
|
69 | 73 | } else if(!is_string($val)) {
|
70 | 74 | 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']; |
72 | 76 |
|
73 |
| - list($valid, $error) = is_valid_mf2_object($val); |
| 77 | + list($valid, $error) = is_valid_mf2_object($val, $level+1); |
74 | 78 | 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"]; |
76 | 80 | }
|
77 | 81 | }
|
78 | 82 | }
|
79 | 83 |
|
80 | 84 | if(isset($input->children)) {
|
81 | 85 | 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']; |
83 | 87 |
|
84 | 88 | 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); |
86 | 90 | 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"]; |
88 | 92 | }
|
89 | 93 | }
|
90 | 94 | }
|
|
0 commit comments