Skip to content

Commit f083a69

Browse files
committed
Use stdClass when a microformat has no properties
Fixes microformats#171. Test based on the issue included.
1 parent e8da04f commit f083a69

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Mf2/Parser.php

+5
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,11 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
11221122
// Make sure things are unique and in alphabetical order
11231123
$mfTypes = array_unique($mfTypes);
11241124
sort($mfTypes);
1125+
1126+
// Properties should be an object when JSON serialised
1127+
if (empty($return) and $this->jsonMode) {
1128+
$return = new stdClass();
1129+
}
11251130

11261131
// Phew. Return the final result.
11271132
$parsed = array(

tests/Mf2/CombinedMicroformatsTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -417,5 +417,17 @@ public function testMf2DoesNotParseRelTag() {
417417
$this->assertArrayNotHasKey('category', $output['items'][1]['properties']);
418418
}
419419

420+
/**
421+
* JSON-mode should return an empty stdClass when a microformat has no properties.
422+
* @see https://github.com/indieweb/php-mf2/issues/171
423+
*/
424+
public function testEmptyPropertiesObjectInJSONMode() {
425+
$input = '<div class="h-feed"><div class="h-entry"></div></div>';
426+
$parser = new Parser($input, null, true);
427+
$output = $parser->parse();
428+
$this->assertInstanceOf(\stdClass::class, $output['items'][0]['properties']);
429+
$this->assertSame('{}', json_encode($output['items'][0]['properties']));
430+
}
431+
420432
}
421433

0 commit comments

Comments
 (0)