Chris@17: provider = $provider; Chris@17: $this->schemes = array_map('mb_strtolower', $schemes); Chris@17: Chris@17: $this->formats = $formats = array_map('mb_strtolower', $formats); Chris@17: // Assert that only the supported formats are present. Chris@17: assert(array_diff($formats, ['json', 'xml']) == []); Chris@17: Chris@17: // Use the first provided format to build the endpoint URL. If no formats Chris@17: // are provided, default to JSON. Chris@17: $this->url = str_replace('{format}', reset($this->formats) ?: 'json', $url); Chris@17: Chris@17: if (!UrlHelper::isValid($this->url, TRUE) || !UrlHelper::isExternal($this->url)) { Chris@17: throw new \InvalidArgumentException('oEmbed endpoint must have a valid external URL'); Chris@17: } Chris@17: Chris@17: $this->supportsDiscovery = (bool) $supports_discovery; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns the endpoint URL. Chris@17: * Chris@17: * The URL will be built with the first available format. If the endpoint Chris@17: * does not provide any formats, JSON will be used. Chris@17: * Chris@17: * @return string Chris@17: * The endpoint URL. Chris@17: */ Chris@17: public function getUrl() { Chris@17: return $this->url; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns the provider this endpoint belongs to. Chris@17: * Chris@17: * @return \Drupal\media\OEmbed\Provider Chris@17: * The provider object. Chris@17: */ Chris@17: public function getProvider() { Chris@17: return $this->provider; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns list of URL schemes supported by the provider. Chris@17: * Chris@17: * @return string[] Chris@17: * List of schemes. Chris@17: */ Chris@17: public function getSchemes() { Chris@17: return $this->schemes; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns list of supported formats. Chris@17: * Chris@17: * @return string[] Chris@17: * List of formats. Chris@17: */ Chris@17: public function getFormats() { Chris@17: return $this->formats; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns whether the provider supports oEmbed discovery. Chris@17: * Chris@17: * @return bool Chris@17: * Returns TRUE if the provides discovery, otherwise FALSE. Chris@17: */ Chris@17: public function supportsDiscovery() { Chris@17: return $this->supportsDiscovery; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tries to match a URL against the endpoint schemes. Chris@17: * Chris@17: * @param string $url Chris@17: * Media item URL. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the URL matches against the endpoint schemes, otherwise FALSE. Chris@17: */ Chris@17: public function matchUrl($url) { Chris@17: foreach ($this->getSchemes() as $scheme) { Chris@17: // Convert scheme into a valid regular expression. Chris@17: $regexp = str_replace(['.', '*'], ['\.', '.*'], $scheme); Chris@17: if (preg_match("|^$regexp$|", $url)) { Chris@17: return TRUE; Chris@17: } Chris@17: } Chris@17: return FALSE; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Builds and returns the endpoint URL. Chris@17: * Chris@17: * @param string $url Chris@17: * The canonical media URL. Chris@17: * Chris@17: * @return string Chris@17: * URL of the oEmbed endpoint. Chris@17: */ Chris@17: public function buildResourceUrl($url) { Chris@17: $query = ['url' => $url]; Chris@17: return $this->getUrl() . '?' . UrlHelper::buildQuery($query); Chris@17: } Chris@17: Chris@17: }