diff --git a/lib/Parser/XML/Construct.php b/lib/Parser/XML/Construct.php index f0cebb4..fe609fc 100644 --- a/lib/Parser/XML/Construct.php +++ b/lib/Parser/XML/Construct.php @@ -232,7 +232,7 @@ abstract class Construct { return $out; } - /** Returns the first Atom link URL which matches the desired relation, with nearest desired media type, or no media type if none match */ + /** Returns the first Atom link URL which matches the desired relation, with nearest desired media type */ protected function fetchAtomRelation(string $rel = "", array $mediaTypes = [], \DOMNode $context = null): ?Url { // tidy ther list of media types; this orders them worst (0)to best (highest index) and then creates a hashtable $mediaTypes = array_flip(array_reverse(array_values(array_unique(array_map(function(string $t) { @@ -245,16 +245,14 @@ abstract class Construct { $result = array_reduce($rels, function($best, $cur) use ($mediaTypes) { $t = trim($cur->getAttribute("type")); // absence of media type is acceptable if no better match yet exists - if (!strlen($t)) { - if (!$best) { - return [$cur, -1]; // any match will rank higher than -1 - } + if (!strlen($t) && (!$best || $best[1] < -1)) { + return [$cur, -1]; // any preferred type will rank higher than -1 } $t = $this->parseMediaType($t); if ($t) { - $rank = $mediaTypes[$t] ?? null; - if (!is_null($rank) && (!$best || $rank > $best[1])) { - // if the media type is acceptable and there is currently no candidate or the candidate ranks lower, use the current link + $rank = $mediaTypes[$t] ?? -2; // even no type will rank higher than a non-preferred type + if (!$best || $rank > $best[1]) { + // if there is currently no candidate or the candidate ranks lower, use the current link return [$cur, $rank]; } } diff --git a/tests/cases/XML/feed-atom.yaml b/tests/cases/XML/feed-atom.yaml index b5c9a13..28b5201 100644 --- a/tests/cases/XML/feed-atom.yaml +++ b/tests/cases/XML/feed-atom.yaml @@ -129,7 +129,7 @@ Feed link 6: # HTML is even more preferred version: '1.0' link: 'http://example.com/' -Feed link 7: # No type is better than an unacceptable type +Feed link 7: # No type is better than a non-preferred type input: >