/** * Get clear uri. * * @static * * @param \Zend\Uri\Uri $uri * * @return boolean|\Zend\Uri\Uri False if uri is not auhorized */ public static function clearUri(Uri $uri) { if ($uri->getScheme() !== 'http') { return false; } $uri->setHost('www.vimeo.com'); // clear $uri->setPort(0); $uri->setUserInfo(''); $uri->setQuery(''); $uri->setFragment(''); return $uri; }
/** * Get clear uri. * * @static * * @param \Zend\Uri\Uri $uri * * @return boolean|\Zend\Uri\Uri False if uri is not auhorized */ public static function clearUri(Uri $uri) { if ($uri->getScheme() !== 'http') { return false; } $query = $uri->getQueryAsArray(); if (empty($query['v'])) { return false; } $uri->setQuery(array('v' => $query['v'])); $uri->setHost('www.youtube.com'); $uri->setPath('/watch'); // clear $uri->setPort(0); $uri->setUserInfo(''); $uri->setFragment(''); return $uri; }
/** * Extract a single review into a review instance * @param DomElement $node * @return Review */ public function extractReview(DomElement $node) { $review = new Review(); $doc = new DomDocument(); $html = $node->ownerDocument->saveHtml($node); $doc->loadHtml('<?xml encoding="utf-8" ?>' . $html); $xpath = new DOMXpath($doc); /** * Author Username * // member_info/ * /username/span */ $nodes = $xpath->query("//div[contains(@class, 'member_info')]/*/div[contains(@class, 'username')]/span"); if ($nodes->length === 1) { $review->setAuthor(trim($nodes->item(0)->nodeValue)); } /** * Author Location */ $nodes = $xpath->query("//div[contains(@class, 'member_info')]/div[contains(@class, 'location')]"); if ($nodes->length === 1) { $review->setAuthorLocation(trim($nodes->item(0)->nodeValue)); } /** * Review Permalink */ $nodes = $xpath->query("//div[contains(@class, 'quote')]/a"); if ($nodes->length === 1) { // URL linked to from the title is an absolute path without host/scheme // Also, URL contains a fragment that should be removed $path = trim($nodes->item(0)->getAttribute('href')); $url = new Uri($this->getOptions()->getUrl()); $new = sprintf('%s://%s/%s', $url->getScheme(), $url->getHost(), ltrim($path, '/')); $url = new Uri($new); $url->setFragment(null); $review->setUrl((string) $url); } /** * Title Quote */ $nodes = $xpath->query("//div[contains(@class, 'quote')]/a/span[contains(@class, 'noQuotes')]"); if ($nodes->length === 1) { $review->setTitle(trim($nodes->item(0)->nodeValue)); } /** * Rating as an alt tag in an image */ $nodes = $xpath->query("//div[contains(@class, 'rating')]/span[contains(@class, 'rate')]/img"); if ($nodes->length === 1) { $img = $nodes->item(0); $alt = $img->getAttribute('alt'); if (preg_match('/([0-9\\.]+)\\s[\\w]+\\s([0-9\\.]+)/', $alt, $match)) { $review->setStarRating((double) $match[1]); $review->setMaxStarRating((double) $match[2]); } } /** * Rating Date */ $nodes = $xpath->query("//div[contains(@class, 'rating')]/span[contains(@class, 'ratingDate')]"); if ($nodes->length === 1) { $span = $nodes->item(0); if ($span->hasAttribute('title')) { $dateString = $span->getAttribute('title'); $date = DateTime::createFromFormat('d F Y', $dateString); if (false !== $date) { $review->setDate($date); } } else { if (preg_match('/([0-9]{1,2}\\s[\\w]+\\s[0-9]{4})/i', trim($span->nodeValue), $match)) { $date = DateTime::createFromFormat('d F Y', $match[1]); if (false !== $date) { $review->setDate($date); } } } } /** * Review Excerpt */ $nodes = $xpath->query("//div[contains(@class, 'entry')]/p[contains(@class, 'partial_entry')]"); if ($nodes->length === 1) { // Use first child node->nodeValue to skip links for 'more...' $review->setExcerpt(trim($nodes->item(0)->childNodes->item(0)->nodeValue)); } return $review; }
/** * Get clear uri. * * @static * * @param \Zend\Uri\Uri $uri * * @return boolean|\Zend\Uri\Uri False if uri is not auhorized */ public static function clearUri(Uri $uri) { if ($uri->getScheme() !== 'http') { return false; } $paths = explode('/', $uri->getPath()); $pathsCount = count($paths); if ($pathsCount < 2) { return false; } $type = $paths[$pathsCount - 2]; if (!in_array($type, self::$typesAuthorized)) { return false; } $uri->setHost('www.deezer.com'); // clear $uri->setPort(0); $uri->setUserInfo(''); $uri->setQuery(''); $uri->setFragment(''); return $uri; }
/** * Set the URI fragment part * * @param string $fragment * @return \Zend\Uri\Uri * @throws Exception\InvalidUriPartException If the schema definition * does not have this part */ public function setFragment($fragment) { $this->uri->setFragment($fragment); return $this; }