/** * {@inheritDoc} */ public function extractAttributes($html, array $options) { $Document = $this->_document($html); $options = Hash::normalize($options, []); $data = []; foreach ($options as $name => $required) { $tags = $Document->getElementsByTagName($name); $required = Hash::normalize((array) $required, ''); $data[$name] = []; foreach ($tags as $Tag) { if ($Tag->hasAttributes()) { $attributes = $this->_extractAttributesFromTag($Tag, $required); if (!empty($attributes)) { $data[$name][] = $attributes; } } } } return $data; }
/** * Fetches embed information from the given endpoint. * * @param string $endpoint Endpoint to fetch informations from. * @param string $format Response format. * @return Media Embed informations. */ protected function _embedEndpoint($endpoint, $format) { $response = $this->_Http->get($endpoint); switch ($format) { case self::json: $data = Json::parse($response); break; case self::xml: $data = Xml::parse($response); break; default: throw new Exception('Unsupported response format.'); } return new Media(Hash::reindex($data, ['author_name' => 'authorName', 'author_url' => 'authorUrl', 'provider_name' => 'providerName', 'provider_url' => 'providerUrl', 'cache_age' => 'cacheAge', 'thumbnail_url' => 'thumbnailUrl', 'thumbnail_width' => 'thumbnailWidth', 'thumbnail_height' => 'thumbnailHeight'])); }
/** * */ public function testNormalize() { $data = Hash::normalize(['one', 'two' => 'three', 'four'], 'default'); $this->assertEquals(['one' => 'default', 'two' => 'three', 'four' => 'default'], $data); }
/** * Fetches informations about the given URLs. * * @param array $urls An array of URLs to fetch informations from. * @param array $options Custom options to be interpreted by a provider. * @return array An array of informations, indexed by URL. */ public function extractAll(array $urls, array $options = []) { return Hash::combine($urls, function ($url) use($options) { (yield $url => $this->extract($url, $options)); }); }
/** * {@inheritDoc} */ protected function _embed($url, array $options) { return new Media(Hash::reindex($this->_extractInformations($url), ['og:type' => 'type', 'og:title' => 'title', 'og:description' => 'description', 'og:site_name' => 'providerName', 'og:image' => 'thumbnailUrl', 'og:image:url' => 'thumbnailUrl', 'og:image:width' => 'width', 'og:image:height' => 'height', 'og:video:width' => 'width', 'og:video:height' => 'height', 'og:url' => 'url'])); }
/** * Builds a media from the given meta tags. * * @param array $metas Meta tags. * @return Media Media. */ protected function _media(array $metas) { $metas = Hash::combine($metas, function ($Meta) { (yield $Meta->get('property') => $Meta->get('content')); }); return new Media($metas); }
/** * {@inheritDoc} */ public function present(Media $Media) { return $Media->setProperties(Hash::reindex($Media->properties(), $this->_mapping)); }