diff vendor/zendframework/zend-feed/src/Reader/FeedSet.php @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children a9cd425dd02b
line wrap: on
line diff
--- a/vendor/zendframework/zend-feed/src/Reader/FeedSet.php	Thu Jul 05 15:32:06 2018 +0100
+++ b/vendor/zendframework/zend-feed/src/Reader/FeedSet.php	Tue Jul 10 13:19:18 2018 +0000
@@ -41,54 +41,95 @@
     {
         foreach ($links as $link) {
             if (strtolower($link->getAttribute('rel')) !== 'alternate'
-                || !$link->getAttribute('type') || !$link->getAttribute('href')) {
+                || ! $link->getAttribute('type') || ! $link->getAttribute('href')) {
                 continue;
             }
-            if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
+            if (! isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
                 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
-            } elseif (!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
+            } elseif (! isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
                 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
-            } elseif (!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
+            } elseif (! isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
                 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
             }
             $this[] = new static([
                 'rel' => 'alternate',
                 'type' => $link->getAttribute('type'),
                 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
+                'title' => $link->getAttribute('title'),
             ]);
         }
     }
 
     /**
      *  Attempt to turn a relative URI into an absolute URI
+     *
+     *  @param string $link
+     *  @param string $uri OPTIONAL
+     *  @return string|null absolutised link or null if invalid
      */
     protected function absolutiseUri($link, $uri = null)
     {
         $linkUri = Uri::factory($link);
-        if (!$linkUri->isAbsolute() or !$linkUri->isValid()) {
-            if ($uri !== null) {
-                $uri = Uri::factory($uri);
+        if ($linkUri->isAbsolute()) {
+            // invalid absolute link can not be recovered
+            return $linkUri->isValid() ? $link : null;
+        }
 
-                if ($link[0] !== '/') {
-                    $link = $uri->getPath() . '/' . $link;
-                }
+        $scheme = 'http';
+        if ($uri !== null) {
+            $uri = Uri::factory($uri);
+            $scheme = $uri->getScheme() ?: $scheme;
+        }
 
-                $link   = sprintf(
-                    '%s://%s/%s',
-                    ($uri->getScheme() ?: 'http'),
-                    $uri->getHost(),
-                    $this->canonicalizePath($link)
-                );
+        if ($linkUri->getHost()) {
+            $link = $this->resolveSchemeRelativeUri($link, $scheme);
+        } elseif ($uri !== null) {
+            $link = $this->resolveRelativeUri($link, $scheme, $uri->getHost(), $uri->getPath());
+        }
 
-                if (!Uri::factory($link)->isValid()) {
-                    $link = null;
-                }
-            }
+        if (! Uri::factory($link)->isValid()) {
+            return null;
         }
+
         return $link;
     }
 
     /**
+     * Resolves scheme relative link to absolute
+     *
+     * @param string $link
+     * @param string $scheme
+     * @return string
+     */
+    private function resolveSchemeRelativeUri($link, $scheme)
+    {
+        $link = ltrim($link, '/');
+        return sprintf('%s://%s', $scheme, $link);
+    }
+
+    /**
+     *  Resolves relative link to absolute
+     *
+     *  @param string $link
+     *  @param string $scheme
+     *  @param string $host
+     *  @param string $uriPath
+     *  @return string
+     */
+    private function resolveRelativeUri($link, $scheme, $host, $uriPath)
+    {
+        if ($link[0] !== '/') {
+            $link = $uriPath . '/' . $link;
+        }
+        return sprintf(
+            '%s://%s/%s',
+            $scheme,
+            $host,
+            $this->canonicalizePath($link)
+        );
+    }
+
+    /**
      *  Canonicalize relative path
      */
     protected function canonicalizePath($path)
@@ -117,8 +158,8 @@
      */
     public function offsetGet($offset)
     {
-        if ($offset == 'feed' && !$this->offsetExists('feed')) {
-            if (!$this->offsetExists('href')) {
+        if ($offset == 'feed' && ! $this->offsetExists('feed')) {
+            if (! $this->offsetExists('href')) {
                 return;
             }
             $feed = Reader::import($this->offsetGet('href'));