diff --git a/lib/Feed.php b/lib/Feed.php index ad0af3d..3b47411 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -104,26 +104,15 @@ class Feed { } else { $f->titleContentHash = hash('sha256', $f->title.$content); } - // If there is an Atom id element use it as the id. - $id = (string)$f->xml->children('http://www.w3.org/2005/Atom')->id; - if ($id !== '') { - $f->id = hash('sha256', $id); - continue; - } - // If there is a guid element use it as the id. - $id = (string)$f->xml->guid; - if ($id !== '') { - $f->id = hash('sha256', $id); - continue; - } - // If there is a Dublin Core identifier use it. - $id = (string)$f->xml->children('http://purl.org/dc/elements/1.1/')->identifier; - if ($id !== '') { - $f->id = hash('sha256', $id); - continue; - } - // If there aren't any of those there is no id. $f->id = null; + // prefer an Atom ID as the item's ID + $id = (string) $f->xml->children('http://www.w3.org/2005/Atom')->id; + // otherwise use the RSS2 guid element + if(!strlen($id)) $id = (string) $f->xml->guid; + // otherwise use the Dublin Core identifier element + if(!strlen($id)) $id = (string) $f->xml->children('http://purl.org/dc/elements/1.1/')->identifier; + // otherwise there is no ID; if there is one, hash it + if(strlen($id)) $f->id = hash('sha256', $id); // PicoFeed also doesn't gather up categories, so we do this as well $f->categories = []; diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index 4f062af..71326e1 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -124,6 +124,10 @@ class TestFeed extends \PHPUnit\Framework\TestCase { "Bodybuilders", "Men", ]; + $this->assertSame([], $f->data->items[0]->categories); + $this->assertSame([], $f->data->items[1]->categories); + $this->assertSame([], $f->data->items[3]->categories); + $this->assertSame([], $f->data->items[4]->categories); $this->assertSame($categories, $f->data->items[5]->categories); }