Example #1
0
 /**
  * @param Item $item
  *
  * @return Source
  */
 public function setItem(Item $item = null)
 {
     if ($this->item !== $item) {
         // romove link on this item for old item
         if ($this->item instanceof Item) {
             $tmp = $this->item;
             $this->item = null;
             $tmp->removeSource($this);
         }
         $this->item = $item;
         // add link on this item
         if ($item instanceof Item) {
             $this->item->addSource($this);
         }
     }
     return $this;
 }
Example #2
0
 /**
  * Fill item from source.
  *
  * @param array $data
  *
  * @return Item|null
  */
 public function fill(array $data)
 {
     if (empty($data['url']) || !is_string($data['url']) || strpos($data['url'], $this->browser->getHost()) !== 0 || !preg_match(self::REG_ITEM_ID, $data['url'], $match)) {
         return null;
     }
     $body = $this->browser->get('anime', ['aid' => $match['id']]);
     $item = new Item();
     $item->setEpisodesNumber($body->filter('episodecount')->text());
     $item->setDatePremiere(new \DateTime($body->filter('startdate')->text()));
     $item->setDateEnd(new \DateTime($body->filter('enddate')->text()));
     $item->setSummary($this->cleaner->clean($body->filter('description')->text()));
     // set main source
     $source = new Source();
     $source->setUrl($data['url']);
     $item->addSource($source);
     // add url to offsite
     $source = new Source();
     $source->setUrl($body->filter('url')->text());
     $item->addSource($source);
     // set complex data
     $this->setCover($item, $body, $match['id']);
     $this->setNames($item, $body);
     $this->setEpisodes($item, $body);
     $this->setType($item, $body);
     $this->setGenres($item, $body);
     return $item;
 }
 /**
  * Fill item from source
  *
  * @param array $date
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item|null
  */
 public function fill(array $data)
 {
     if (empty($data['url']) || !is_string($data['url']) || strpos($data['url'], $this->browser->getHost()) !== 0) {
         return null;
     }
     $dom = $this->browser->getDom(substr($data['url'], strlen($this->browser->getHost())));
     if (!$dom instanceof \DOMDocument) {
         return null;
     }
     $xpath = new \DOMXPath($dom);
     $nodes = $xpath->query(self::XPATH_FOR_FILL);
     // get item type
     if (!($type = $this->getItemType($data['url']))) {
         return null;
     }
     /* @var $body \DOMElement */
     if (!($body = $nodes->item(4))) {
         throw new \LogicException('Incorrect data structure at source');
     }
     // item id from source
     $id = 0;
     if (preg_match('/id=(?<id>\\d+)/', $data['url'], $mat)) {
         $id = (int) $mat['id'];
     }
     $item = new Item();
     // add source link on world-art
     $item->addSource((new Source())->setUrl($data['url']));
     // add other source links
     /* @var $links \DOMNodeList */
     $links = $xpath->query('a', $nodes->item(1));
     for ($i = 0; $i < $links->length; $i++) {
         $link = $this->getAttrAsArray($links->item($i));
         if (strpos($link['href'], 'http://') !== false && strpos($link['href'], $this->browser->getHost()) === false) {
             $item->addSource((new Source())->setUrl($link['href']));
         }
     }
     // add cover
     $this->setCover($item, $id, $type);
     // fill item studio
     if ($studio = $this->getStudio($xpath, $body)) {
         $item->setStudio($studio);
     }
     // fill main data
     if ($type == self::ITEM_TYPE_ANIMATION) {
         $head = $xpath->query('table[3]/tr[2]/td[3]', $body);
         if (!$head->length) {
             $head = $xpath->query('table[2]/tr[1]/td[3]', $body);
         }
     } else {
         $head = $xpath->query('table[3]/tr[1]/td[3]', $body);
     }
     if ($head->length) {
         switch ($type) {
             case self::ITEM_TYPE_ANIMATION:
                 $this->fillAnimationNames($item, $xpath, $head->item(0));
                 break;
             default:
                 $this->fillCinemaNames($item, $xpath, $head->item(0));
         }
     }
     $this->fillHeadData($item, $xpath, $head->item(0));
     // fill body data
     $this->fillBodyData($item, $xpath, $body, $id, !empty($data['frames']), $type);
     return $item;
 }
 /**
  * Refill item field from search result.
  *
  * @param Item $item
  * @param string $field
  * @param array $data
  *
  * @return Item
  */
 public function refillFromSearchResult(Item $item, $field, array $data)
 {
     if (!empty($data['url'])) {
         $source = new Source();
         $source->setUrl($data['url']);
         $item->addSource($source);
         $item = $this->refill($item, $field);
     }
     return $item;
 }
 /**
  * @param Item $item
  * @param array $body
  *
  * @return Item
  */
 public function setSources(Item $item, $body)
 {
     $sources = ['ani_db_id' => self::ANI_DB_URL, 'world_art_id' => self::WORLD_ART_URL, 'myanimelist_id' => self::MY_ANIME_LIST_URL];
     foreach ($sources as $key => $url) {
         if (!empty($body[$key])) {
             $source = new Source();
             $source->setUrl(str_replace('#ID#', $body[$key], $url));
             $item->addSource($source);
         }
     }
     return $item;
 }
 /**
  * Fill item
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $new_item
  * @param string $field
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 protected function fillItem(Item $item, Item $new_item, $field)
 {
     switch ($field) {
         case self::FIELD_COUNTRY:
             $item->setCountry($new_item->getCountry());
             break;
         case self::FIELD_DATE_END:
             $item->setDateEnd($new_item->getDateEnd());
             break;
         case self::FIELD_DATE_PREMIERE:
             $item->setDatePremiere($new_item->getDatePremiere());
             break;
         case self::FIELD_DURATION:
             $item->setDuration($new_item->getDuration());
             break;
         case self::FIELD_EPISODES:
             $item->setEpisodes($new_item->getEpisodes());
             break;
         case self::FIELD_EPISODES_NUMBER:
             $item->setEpisodesNumber($new_item->getEpisodesNumber());
             break;
         case self::FIELD_FILE_INFO:
             $item->setFileInfo($new_item->getFileInfo());
             break;
         case self::FIELD_GENRES:
             /* @var $new_genre \AnimeDb\Bundle\CatalogBundle\Entity\Genre */
             foreach ($new_item->getGenres() as $new_genre) {
                 $item->addGenre($new_genre);
             }
             break;
         case self::FIELD_NAMES:
             // set main name in top of names list
             $new_names = $new_item->getNames()->toArray();
             array_unshift($new_names, (new Name())->setName($new_item->getName()));
             foreach ($new_names as $new_name) {
                 $item->addName($new_name);
             }
             break;
         case self::FIELD_SOURCES:
             foreach ($new_item->getSources() as $new_source) {
                 $item->addSource($new_source);
             }
             break;
         case self::FIELD_STUDIO:
             $item->setStudio($new_item->getStudio());
             break;
         case self::FIELD_SUMMARY:
             $item->setSummary($new_item->getSummary());
             break;
     }
     return $item;
 }