Skip to content

Commit ae49928

Browse files
authored
Merge pull request #172 from Zegnat/properties-json-object
Use stdClass when a microformat has no properties
2 parents e8da04f + 00b10e5 commit ae49928

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Mf2/Parser.php

Lines changed: 5 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,5 +417,23 @@ 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+
// Try in JSON-mode: expect the raw PHP to be an stdClass instance that serializes to an empty object.
427+
$parser = new Parser($input, null, true);
428+
$output = $parser->parse();
429+
$this->assertInstanceOf('\stdClass', $output['items'][0]['properties']);
430+
$this->assertSame('{}', json_encode($output['items'][0]['properties']));
431+
// Repeat in non-JSON-mode: expect the raw PHP to be an array. Check that its serialization is not what we need for mf2 JSON.
432+
$parser = new Parser($input, null, false);
433+
$output = $parser->parse();
434+
$this->assertInternalType('array', $output['items'][0]['properties']);
435+
$this->assertSame('[]', json_encode($output['items'][0]['properties']));
436+
}
437+
420438
}
421439

0 commit comments

Comments
 (0)