Skip to content

Commit

Permalink
Use stdClass when a microformat has no properties
Browse files Browse the repository at this point in the history
Fixes microformats#171. Test based on the issue included.
  • Loading branch information
Zegnat committed Apr 28, 2018
1 parent e8da04f commit f083a69
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,11 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
// Make sure things are unique and in alphabetical order
$mfTypes = array_unique($mfTypes);
sort($mfTypes);

// Properties should be an object when JSON serialised
if (empty($return) and $this->jsonMode) {
$return = new stdClass();
}

// Phew. Return the final result.
$parsed = array(
Expand Down
12 changes: 12 additions & 0 deletions tests/Mf2/CombinedMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,17 @@ public function testMf2DoesNotParseRelTag() {
$this->assertArrayNotHasKey('category', $output['items'][1]['properties']);
}

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

}

0 comments on commit f083a69

Please sign in to comment.