diff --git a/lib/XRay/Formats/HTML.php b/lib/XRay/Formats/HTML.php index 8c39345..9bcebec 100644 --- a/lib/XRay/Formats/HTML.php +++ b/lib/XRay/Formats/HTML.php @@ -96,8 +96,8 @@ class HTML extends Format { if($mf2 && count($mf2['items']) > 0) { $data = Formats\Mf2::parse($mf2, $url, $http, $opts); - $result = array_merge($result, $data); if($data) { + $result = array_merge($result, $data); if($fragment) { $result['info'] = [ 'found_fragment' => $foundFragment diff --git a/lib/XRay/Formats/Mf2.php b/lib/XRay/Formats/Mf2.php index 936091e..636bdc8 100644 --- a/lib/XRay/Formats/Mf2.php +++ b/lib/XRay/Formats/Mf2.php @@ -55,6 +55,10 @@ class Mf2 extends Format { #Parse::debug("mf2:0: Recognized $url as an h-card it is the only item on the page"); return self::parseAsHCard($item, $http, $url, $url); } + if(in_array('h-app', $item['type']) || in_array('h-x-app', $item['type'])) { + #Parse::debug("mf2:0: Recognized $url as an h-feed because it is the only item on the page"); + return self::parseAsHApp($mf2, $item, $http, $url); + } 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, $url); @@ -83,6 +87,8 @@ class Mf2 extends Format { return self::parseAsHProduct($mf2, $item, $http, $url); } elseif(in_array('h-item', $item['type'])) { return self::parseAsHItem($mf2, $item, $http, $url); + } elseif(in_array('h-app', $item['type']) || in_array('h-x-app', $item['type'])) { + return self::parseAsHApp($mf2, $item, $http, $url); } elseif(in_array('h-feed', $item['type'])) { return self::parseAsHFeed($mf2, $http, $url); } else { @@ -116,6 +122,8 @@ class Mf2 extends Format { return self::parseAsHProduct($mf2, $item, $http, $url); } elseif(in_array('h-item', $item['type'])) { return self::parseAsHItem($mf2, $item, $http, $url); + } elseif(in_array('h-app', $item['type']) || in_array('h-x-app', $item['type'])) { + return self::parseAsHApp($mf2, $item, $http, $url); } } } @@ -162,6 +170,9 @@ class Mf2 extends Format { } 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, $url); + } elseif(in_array('h-app', $item['type']) || in_array('h-x-app', $item['type'])) { + #Parse::debug("mf2:6: $url is falling back to the first h-item on the page"); + return self::parseAsHApp($mf2, $item, $http, $url); } } @@ -543,6 +554,23 @@ class Mf2 extends Format { return $response; } + private static function parseAsHApp($mf2, $item, $http, $url) { + $data = [ + 'type' => 'app' + ]; + + self::collectSingleValues(['name'], ['url','logo'], $item, $url, $data); + + if(!isset($data['url'])) + $data['url'] = $url; + + $response = [ + 'data' => $data + ]; + + return $response; + } + private static function parseAsHEvent($mf2, $item, $http, $url) { $data = [ 'type' => 'event' diff --git a/tests/ParseTest.php b/tests/ParseTest.php index 991b23d..aa318bc 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -613,4 +613,32 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]); } + public function testHApp() { + $url = 'http://source.example.com/h-app'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + $this->assertEquals('app', $data->data->type); + $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo); + $this->assertEquals('Quill', $data->data->name); + $this->assertEquals($url, $data->data->url); + $this->assertObjectNotHasAttribute('photo', $data->data); + } + + public function testHXApp() { + $url = 'http://source.example.com/h-x-app'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + $this->assertEquals('app', $data->data->type); + $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo); + $this->assertEquals('Quill', $data->data->name); + $this->assertEquals($url, $data->data->url); + $this->assertObjectNotHasAttribute('photo', $data->data); + } + } diff --git a/tests/data/source.example.com/h-app b/tests/data/source.example.com/h-app new file mode 100644 index 0000000..1233eea --- /dev/null +++ b/tests/data/source.example.com/h-app @@ -0,0 +1,14 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + + + + diff --git a/tests/data/source.example.com/h-x-app b/tests/data/source.example.com/h-x-app new file mode 100644 index 0000000..7f4242d --- /dev/null +++ b/tests/data/source.example.com/h-x-app @@ -0,0 +1,15 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + + + + +