Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-feed/src/Reader/FeedSet.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
5 * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 */ | |
9 | |
10 namespace Zend\Feed\Reader; | |
11 | |
12 use ArrayObject; | |
13 use DOMNodeList; | |
14 use Zend\Feed\Uri; | |
15 | |
16 class FeedSet extends ArrayObject | |
17 { | |
18 public $rss = null; | |
19 | |
20 public $rdf = null; | |
21 | |
22 public $atom = null; | |
23 | |
24 /** | |
25 * Import a DOMNodeList from any document containing a set of links | |
26 * for alternate versions of a document, which will normally refer to | |
27 * RSS/RDF/Atom feeds for the current document. | |
28 * | |
29 * All such links are stored internally, however the first instance of | |
30 * each RSS, RDF or Atom type has its URI stored as a public property | |
31 * as a shortcut where the use case is simply to get a quick feed ref. | |
32 * | |
33 * Note that feeds are not loaded at this point, but will be lazy | |
34 * loaded automatically when each links 'feed' array key is accessed. | |
35 * | |
36 * @param DOMNodeList $links | |
37 * @param string $uri | |
38 * @return void | |
39 */ | |
40 public function addLinks(DOMNodeList $links, $uri) | |
41 { | |
42 foreach ($links as $link) { | |
43 if (strtolower($link->getAttribute('rel')) !== 'alternate' | |
44 || !$link->getAttribute('type') || !$link->getAttribute('href')) { | |
45 continue; | |
46 } | |
47 if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') { | |
48 $this->rss = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); | |
49 } elseif (!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') { | |
50 $this->atom = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); | |
51 } elseif (!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') { | |
52 $this->rdf = $this->absolutiseUri(trim($link->getAttribute('href')), $uri); | |
53 } | |
54 $this[] = new static([ | |
55 'rel' => 'alternate', | |
56 'type' => $link->getAttribute('type'), | |
57 'href' => $this->absolutiseUri(trim($link->getAttribute('href')), $uri), | |
58 ]); | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * Attempt to turn a relative URI into an absolute URI | |
64 */ | |
65 protected function absolutiseUri($link, $uri = null) | |
66 { | |
67 $linkUri = Uri::factory($link); | |
68 if (!$linkUri->isAbsolute() or !$linkUri->isValid()) { | |
69 if ($uri !== null) { | |
70 $uri = Uri::factory($uri); | |
71 | |
72 if ($link[0] !== '/') { | |
73 $link = $uri->getPath() . '/' . $link; | |
74 } | |
75 | |
76 $link = sprintf( | |
77 '%s://%s/%s', | |
78 ($uri->getScheme() ?: 'http'), | |
79 $uri->getHost(), | |
80 $this->canonicalizePath($link) | |
81 ); | |
82 | |
83 if (!Uri::factory($link)->isValid()) { | |
84 $link = null; | |
85 } | |
86 } | |
87 } | |
88 return $link; | |
89 } | |
90 | |
91 /** | |
92 * Canonicalize relative path | |
93 */ | |
94 protected function canonicalizePath($path) | |
95 { | |
96 $parts = array_filter(explode('/', $path)); | |
97 $absolutes = []; | |
98 foreach ($parts as $part) { | |
99 if ('.' == $part) { | |
100 continue; | |
101 } | |
102 if ('..' == $part) { | |
103 array_pop($absolutes); | |
104 } else { | |
105 $absolutes[] = $part; | |
106 } | |
107 } | |
108 return implode('/', $absolutes); | |
109 } | |
110 | |
111 /** | |
112 * Supports lazy loading of feeds using Reader::import() but | |
113 * delegates any other operations to the parent class. | |
114 * | |
115 * @param string $offset | |
116 * @return mixed | |
117 */ | |
118 public function offsetGet($offset) | |
119 { | |
120 if ($offset == 'feed' && !$this->offsetExists('feed')) { | |
121 if (!$this->offsetExists('href')) { | |
122 return; | |
123 } | |
124 $feed = Reader::import($this->offsetGet('href')); | |
125 $this->offsetSet('feed', $feed); | |
126 return $feed; | |
127 } | |
128 return parent::offsetGet($offset); | |
129 } | |
130 } |