comparison 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
comparison
equal deleted inserted replaced
1:0b0e5f3b1e83 2:5311817fb629
39 */ 39 */
40 public function addLinks(DOMNodeList $links, $uri) 40 public function addLinks(DOMNodeList $links, $uri)
41 { 41 {
42 foreach ($links as $link) { 42 foreach ($links as $link) {
43 if (strtolower($link->getAttribute('rel')) !== 'alternate' 43 if (strtolower($link->getAttribute('rel')) !== 'alternate'
44 || !$link->getAttribute('type') || !$link->getAttribute('href')) { 44 || ! $link->getAttribute('type') || ! $link->getAttribute('href')) {
45 continue; 45 continue;
46 } 46 }
47 if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') { 47 if (! isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
48 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); 48 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
49 } elseif (!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') { 49 } elseif (! isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
50 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); 50 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
51 } elseif (!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') { 51 } elseif (! isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
52 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); 52 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri);
53 } 53 }
54 $this[] = new static([ 54 $this[] = new static([
55 'rel' => 'alternate', 55 'rel' => 'alternate',
56 'type' => $link->getAttribute('type'), 56 'type' => $link->getAttribute('type'),
57 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri), 57 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri),
58 'title' => $link->getAttribute('title'),
58 ]); 59 ]);
59 } 60 }
60 } 61 }
61 62
62 /** 63 /**
63 * Attempt to turn a relative URI into an absolute URI 64 * Attempt to turn a relative URI into an absolute URI
65 *
66 * @param string $link
67 * @param string $uri OPTIONAL
68 * @return string|null absolutised link or null if invalid
64 */ 69 */
65 protected function absolutiseUri($link, $uri = null) 70 protected function absolutiseUri($link, $uri = null)
66 { 71 {
67 $linkUri = Uri::factory($link); 72 $linkUri = Uri::factory($link);
68 if (!$linkUri->isAbsolute() or !$linkUri->isValid()) { 73 if ($linkUri->isAbsolute()) {
69 if ($uri !== null) { 74 // invalid absolute link can not be recovered
70 $uri = Uri::factory($uri); 75 return $linkUri->isValid() ? $link : null;
76 }
71 77
72 if ($link[0] !== '/') { 78 $scheme = 'http';
73 $link = $uri->getPath() . '/' . $link; 79 if ($uri !== null) {
74 } 80 $uri = Uri::factory($uri);
81 $scheme = $uri->getScheme() ?: $scheme;
82 }
75 83
76 $link = sprintf( 84 if ($linkUri->getHost()) {
77 '%s://%s/%s', 85 $link = $this->resolveSchemeRelativeUri($link, $scheme);
78 ($uri->getScheme() ?: 'http'), 86 } elseif ($uri !== null) {
79 $uri->getHost(), 87 $link = $this->resolveRelativeUri($link, $scheme, $uri->getHost(), $uri->getPath());
80 $this->canonicalizePath($link) 88 }
81 );
82 89
83 if (!Uri::factory($link)->isValid()) { 90 if (! Uri::factory($link)->isValid()) {
84 $link = null; 91 return null;
85 }
86 }
87 } 92 }
93
88 return $link; 94 return $link;
95 }
96
97 /**
98 * Resolves scheme relative link to absolute
99 *
100 * @param string $link
101 * @param string $scheme
102 * @return string
103 */
104 private function resolveSchemeRelativeUri($link, $scheme)
105 {
106 $link = ltrim($link, '/');
107 return sprintf('%s://%s', $scheme, $link);
108 }
109
110 /**
111 * Resolves relative link to absolute
112 *
113 * @param string $link
114 * @param string $scheme
115 * @param string $host
116 * @param string $uriPath
117 * @return string
118 */
119 private function resolveRelativeUri($link, $scheme, $host, $uriPath)
120 {
121 if ($link[0] !== '/') {
122 $link = $uriPath . '/' . $link;
123 }
124 return sprintf(
125 '%s://%s/%s',
126 $scheme,
127 $host,
128 $this->canonicalizePath($link)
129 );
89 } 130 }
90 131
91 /** 132 /**
92 * Canonicalize relative path 133 * Canonicalize relative path
93 */ 134 */
115 * @param string $offset 156 * @param string $offset
116 * @return mixed 157 * @return mixed
117 */ 158 */
118 public function offsetGet($offset) 159 public function offsetGet($offset)
119 { 160 {
120 if ($offset == 'feed' && !$this->offsetExists('feed')) { 161 if ($offset == 'feed' && ! $this->offsetExists('feed')) {
121 if (!$this->offsetExists('href')) { 162 if (! $this->offsetExists('href')) {
122 return; 163 return;
123 } 164 }
124 $feed = Reader::import($this->offsetGet('href')); 165 $feed = Reader::import($this->offsetGet('href'));
125 $this->offsetSet('feed', $feed); 166 $this->offsetSet('feed', $feed);
126 return $feed; 167 return $feed;