From 330bc9024d4474b51ff129f3aadd2ea158f17451 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 27 May 2017 10:37:42 -0700 Subject: [PATCH] fix parsing for hReview thanks to the new backcompat in php-mf2 0.3.2 --- lib/XRay/Formats/Mf2.php | 31 +++++++++++++++++++++++++++ tests/ParseTest.php | 15 ++++++------- tests/data/source.example.com/hReview | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/XRay/Formats/Mf2.php b/lib/XRay/Formats/Mf2.php index 9bf6605..c25d9a3 100644 --- a/lib/XRay/Formats/Mf2.php +++ b/lib/XRay/Formats/Mf2.php @@ -32,6 +32,10 @@ class Mf2 { #Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page"); return self::parseAsHProduct($mf2, $item, $http); } + if(in_array('h-item', $item['type'])) { + #Parse::debug("mf2:0: Recognized $url as an h-product it is the only item on the page"); + return self::parseAsHItem($mf2, $item, $http); + } if(in_array('h-feed', $item['type'])) { #Parse::debug("mf2:0: Recognized $url as an h-feed because it is the only item on the page"); return self::parseAsHFeed($mf2, $http); @@ -62,6 +66,8 @@ class Mf2 { return self::parseAsHRecipe($mf2, $item, $http); } elseif(in_array('h-product', $item['type'])) { return self::parseAsHProduct($mf2, $item, $http); + } elseif(in_array('h-item', $item['type'])) { + return self::parseAsHItem($mf2, $item, $http); } else { #Parse::debug('This object was not a recognized type.'); return false; @@ -92,6 +98,8 @@ class Mf2 { return self::parseAsHRecipe($mf2, $item, $http); } elseif(in_array('h-product', $item['type'])) { return self::parseAsHProduct($mf2, $item, $http); + } elseif(in_array('h-item', $item['type'])) { + return self::parseAsHItem($mf2, $item, $http); } } } @@ -135,6 +143,9 @@ class Mf2 { } elseif(in_array('h-product', $item['type'])) { #Parse::debug("mf2:6: $url is falling back to the first h-product on the page"); return self::parseAsHProduct($mf2, $item, $http); + } elseif(in_array('h-item', $item['type'])) { + #Parse::debug("mf2:6: $url is falling back to the first h-item on the page"); + return self::parseAsHItem($mf2, $item, $http); } } @@ -408,6 +419,26 @@ class Mf2 { return $response; } + private static function parseAsHItem($mf2, $item, $http) { + $data = [ + 'type' => 'item' + ]; + + self::collectSingleValues(['name'], ['url'], $item, $data); + + self::collectArrayURLValues(['photo','video','audio'], $item, $data, $refs, $http); + + $response = [ + 'data' => $data + ]; + + if(count($refs)) { + $response['data']['refs'] = $refs; + } + + return $response; + } + private static function parseAsHEvent($mf2, $item, $http) { $data = [ 'type' => 'event' diff --git a/tests/ParseTest.php b/tests/ParseTest.php index 56e3724..8853c6f 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -431,18 +431,15 @@ class ParseTest extends PHPUnit_Framework_TestCase { $data = json_decode($body, true); $this->assertEquals('review', $data['data']['type']); - $this->assertEquals('Review', $data['data']['name']); - # TODO: backcompat of mf1 parser is kind of messed up right now - #$this->assertEquals('Not great', $data['data']['summary']); + $this->assertEquals('Not great', $data['data']['summary']); $this->assertEquals('3', $data['data']['rating']); $this->assertEquals('5', $data['data']['best']); $this->assertEquals('This is the full text of the review', $data['data']['content']['text']); - // $this->assertContains('http://product.example.com/', $data['data']['item']); - // $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']); - // $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']); - // $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']); - // $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']); - + $this->assertContains('http://product.example.com/', $data['data']['item']); + $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']); + $this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']); + $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']); + $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']); } public function testMf2Recipe() { diff --git a/tests/data/source.example.com/hReview b/tests/data/source.example.com/hReview index a3d6c33..2573c7c 100644 --- a/tests/data/source.example.com/hReview +++ b/tests/data/source.example.com/hReview @@ -9,7 +9,7 @@ Connection: keep-alive Review -

Review

+

Review