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