Mercurial > hg > isophonics-drupal-site
comparison core/modules/media/src/OEmbed/Endpoint.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\media\OEmbed; | |
4 | |
5 use Drupal\Component\Utility\UrlHelper; | |
6 | |
7 /** | |
8 * Value object for oEmbed provider endpoints. | |
9 * | |
10 * @internal | |
11 * This class is an internal part of the oEmbed system and should only be | |
12 * instantiated by instances of Drupal\media\OEmbed\Provider. | |
13 */ | |
14 class Endpoint { | |
15 | |
16 /** | |
17 * The endpoint's URL. | |
18 * | |
19 * @var string | |
20 */ | |
21 protected $url; | |
22 | |
23 /** | |
24 * The provider this endpoint belongs to. | |
25 * | |
26 * @var \Drupal\media\OEmbed\Provider | |
27 */ | |
28 protected $provider; | |
29 | |
30 /** | |
31 * List of URL schemes supported by the provider. | |
32 * | |
33 * @var string[] | |
34 */ | |
35 protected $schemes; | |
36 | |
37 /** | |
38 * List of supported formats. Only 'json' and 'xml' are allowed. | |
39 * | |
40 * @var string[] | |
41 * | |
42 * @see https://oembed.com/#section2 | |
43 */ | |
44 protected $formats; | |
45 | |
46 /** | |
47 * Whether the provider supports oEmbed discovery. | |
48 * | |
49 * @var bool | |
50 */ | |
51 protected $supportsDiscovery; | |
52 | |
53 /** | |
54 * Endpoint constructor. | |
55 * | |
56 * @param string $url | |
57 * The endpoint URL. May contain a @code '{format}' @endcode placeholder. | |
58 * @param \Drupal\media\OEmbed\Provider $provider | |
59 * The provider this endpoint belongs to. | |
60 * @param string[] $schemes | |
61 * List of URL schemes supported by the provider. | |
62 * @param string[] $formats | |
63 * List of supported formats. Can be "json", "xml" or both. | |
64 * @param bool $supports_discovery | |
65 * Whether the provider supports oEmbed discovery. | |
66 * | |
67 * @throws \InvalidArgumentException | |
68 * If the endpoint URL is empty. | |
69 */ | |
70 public function __construct($url, Provider $provider, array $schemes = [], array $formats = [], $supports_discovery = FALSE) { | |
71 $this->provider = $provider; | |
72 $this->schemes = array_map('mb_strtolower', $schemes); | |
73 | |
74 $this->formats = $formats = array_map('mb_strtolower', $formats); | |
75 // Assert that only the supported formats are present. | |
76 assert(array_diff($formats, ['json', 'xml']) == []); | |
77 | |
78 // Use the first provided format to build the endpoint URL. If no formats | |
79 // are provided, default to JSON. | |
80 $this->url = str_replace('{format}', reset($this->formats) ?: 'json', $url); | |
81 | |
82 if (!UrlHelper::isValid($this->url, TRUE) || !UrlHelper::isExternal($this->url)) { | |
83 throw new \InvalidArgumentException('oEmbed endpoint must have a valid external URL'); | |
84 } | |
85 | |
86 $this->supportsDiscovery = (bool) $supports_discovery; | |
87 } | |
88 | |
89 /** | |
90 * Returns the endpoint URL. | |
91 * | |
92 * The URL will be built with the first available format. If the endpoint | |
93 * does not provide any formats, JSON will be used. | |
94 * | |
95 * @return string | |
96 * The endpoint URL. | |
97 */ | |
98 public function getUrl() { | |
99 return $this->url; | |
100 } | |
101 | |
102 /** | |
103 * Returns the provider this endpoint belongs to. | |
104 * | |
105 * @return \Drupal\media\OEmbed\Provider | |
106 * The provider object. | |
107 */ | |
108 public function getProvider() { | |
109 return $this->provider; | |
110 } | |
111 | |
112 /** | |
113 * Returns list of URL schemes supported by the provider. | |
114 * | |
115 * @return string[] | |
116 * List of schemes. | |
117 */ | |
118 public function getSchemes() { | |
119 return $this->schemes; | |
120 } | |
121 | |
122 /** | |
123 * Returns list of supported formats. | |
124 * | |
125 * @return string[] | |
126 * List of formats. | |
127 */ | |
128 public function getFormats() { | |
129 return $this->formats; | |
130 } | |
131 | |
132 /** | |
133 * Returns whether the provider supports oEmbed discovery. | |
134 * | |
135 * @return bool | |
136 * Returns TRUE if the provides discovery, otherwise FALSE. | |
137 */ | |
138 public function supportsDiscovery() { | |
139 return $this->supportsDiscovery; | |
140 } | |
141 | |
142 /** | |
143 * Tries to match a URL against the endpoint schemes. | |
144 * | |
145 * @param string $url | |
146 * Media item URL. | |
147 * | |
148 * @return bool | |
149 * TRUE if the URL matches against the endpoint schemes, otherwise FALSE. | |
150 */ | |
151 public function matchUrl($url) { | |
152 foreach ($this->getSchemes() as $scheme) { | |
153 // Convert scheme into a valid regular expression. | |
154 $regexp = str_replace(['.', '*'], ['\.', '.*'], $scheme); | |
155 if (preg_match("|^$regexp$|", $url)) { | |
156 return TRUE; | |
157 } | |
158 } | |
159 return FALSE; | |
160 } | |
161 | |
162 /** | |
163 * Builds and returns the endpoint URL. | |
164 * | |
165 * @param string $url | |
166 * The canonical media URL. | |
167 * | |
168 * @return string | |
169 * URL of the oEmbed endpoint. | |
170 */ | |
171 public function buildResourceUrl($url) { | |
172 $query = ['url' => $url]; | |
173 return $this->getUrl() . '?' . UrlHelper::buildQuery($query); | |
174 } | |
175 | |
176 } |