/** * Search source by name * * Return structure * <code> * [ * \AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item * ] * </code> * * @param array $data * * @return array */ public function search(array $data) { $name = iconv('utf-8', 'cp1251', $data['name']); $url = str_replace('#NAME#', urlencode($name), self::SEARH_URL); $url = str_replace('#SECTOR#', isset($data['type']) ? $data['type'] : self::DEFAULT_SECTOR, $url); // get list from xpath $dom = $this->browser->getDom($url); $xpath = new \DOMXPath($dom); // if for request is found only one result is produced forwarding $refresh = $xpath->query('//meta[@http-equiv="Refresh"]/@content'); if ($refresh->length) { list(, $url) = explode('url=', $refresh->item(0)->nodeValue, 2); // add http if need if ($url[0] == '/') { $url = $this->browser->getHost() . $url; } $name = iconv('cp1251', 'utf-8', $name); if (!preg_match('/id=(?<id>\\d+)/', $url, $mat) || !($type = $this->filler->getItemType($url))) { throw new NotFoundHttpException('Incorrect URL for found item'); } return [new ItemSearch($name, $this->getLinkForFill($url), $this->filler->getCoverUrl($mat['id'], $type), '')]; } $rows = $xpath->query(self::XPATH_FOR_LIST); $list = []; foreach ($rows as $el) { $link = $xpath->query('a', $el); // has link on source if ($link->length && ($href = $link->item(0)->getAttribute('href')) && ($name = $link->item(0)->nodeValue) && preg_match('/id=(?<id>\\d+)/', $href, $mat) && ($type = $this->filler->getItemType($href))) { $list[] = new ItemSearch(str_replace(["\r\n", "\n"], ' ', $name), $this->getLinkForFill($this->browser->getHost() . '/' . $href), $this->filler->getCoverUrl($mat['id'], $type), trim(str_replace($name, '', $el->nodeValue)), $this->browser->getHost() . '/' . $href); } } return $list; }
/** * Get source for fill * * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item * * @return string */ public function getSourceForFill(Item $item) { /* @var $source \AnimeDb\Bundle\CatalogBundle\Entity\Source */ foreach ($item->getSources() as $source) { if (strpos($source->getUrl(), $this->browser->getHost()) === 0) { return $source->getUrl(); } } return ''; }
/** * Is supported URL * * @param string $url * * @return boolean */ public function isSupportedUrl($url) { return strpos($url, $this->browser->getHost()) === 0; }