/** * Refill item field from source. * * @param Item $item * @param string $field * * @return Item */ public function refill(Item $item, $field) { if (!($url = $this->getSourceForFill($item))) { return $item; } // get data preg_match(Filler::REG_ITEM_ID, $url, $match); $path = str_replace('#ID#', $match['id'], Filler::FILL_URL); $body = $this->browser->get($path); switch ($field) { case self::FIELD_DATE_END: if ($body['released_on']) { $item->setDateEnd(new \DateTime($body['released_on'])); } break; case self::FIELD_DATE_PREMIERE: $item->setDatePremiere(new \DateTime($body['aired_on'])); break; case self::FIELD_DURATION: $item->setDuration($body['duration']); break; case self::FIELD_EPISODES_NUMBER: $ep_num = $body['episodes_aired'] ? $body['episodes_aired'] : $body['episodes']; $item->setEpisodesNumber($ep_num . ($body['ongoing'] ? '+' : '')); break; case self::FIELD_GENRES: $new_item = $this->filler->setGenres(new Item(), $body); foreach ($new_item->getGenres() as $new_genre) { $item->addGenre($new_genre); } break; case self::FIELD_IMAGES: $this->filler->setImages($item, $body); break; case self::FIELD_NAMES: $new_item = $this->filler->setNames(new Item(), $body); // set main name in top of names list $names = $new_item->getNames()->toArray(); array_unshift($names, (new Name())->setName($new_item->getName())); foreach ($names as $new_name) { $item->addName($new_name); } break; case self::FIELD_SOURCES: $new_item = $this->filler->setSources(new Item(), $body); foreach ($new_item->getSources() as $new_source) { $item->addSource($new_source); } break; case self::FIELD_STUDIO: $this->filler->setStudio($item, $body); break; case self::FIELD_SUMMARY: $item->setSummary($body['description']); break; } return $item; }
/** * @param AddNewItem $event */ public function onAddNewItem(AddNewItem $event) { $item = $event->getItem(); if (!$event->getFillers()->contains($this->filler) && ($url = $this->refiller->getSourceForFill($item))) { // get data preg_match(Filler::REG_ITEM_ID, $url, $match); $path = str_replace('#ID#', $match['id'], Filler::FILL_URL); $body = $this->browser->get($path); // fill item if (!$item->getDateEnd() && $body['released_on']) { $item->setDateEnd(new \DateTime($body['released_on'])); } if (!$item->getDatePremiere()) { $item->setDatePremiere(new \DateTime($body['aired_on'])); } if (!$item->getDuration()) { $item->setDuration($body['duration']); } if (!$item->getEpisodesNumber()) { $ep_num = $body['episodes_aired'] ? $body['episodes_aired'] : $body['episodes']; $item->setEpisodesNumber($ep_num . ($body['ongoing'] ? '+' : '')); } if (!$item->getSummary()) { $item->setSummary($body['description']); } // set complex data if (!$item->getStudio()) { $this->filler->setStudio($item, $body); } if (!$item->getType()) { $this->filler->setType($item, $body); } if (!$item->getCover()) { $this->filler->setCover($item, $body); } $this->filler->setGenres($item, $body); $this->filler->setSources($item, $body); $this->filler->setNames($item, $body); $event->addFiller($this->filler); // resend event $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event); $event->stopPropagation(); } }
/** * Get widget item * * @param array $item * * @return \AnimeDb\Bundle\CatalogBundle\Entity\Widget\Item */ public function getWidgetItem(array $item) { $entity = new ItemWidget(); $entity->setName($this->getItemName($item)); $entity->setLink($this->browser->getHost() . $item['url']); $entity->setCover($this->browser->getHost() . $item['image']['original']); $catalog_item = $this->getCatalogItem($item); if ($catalog_item instanceof Item) { $entity->setItem($catalog_item); } elseif ($this->filler instanceof Filler) { $entity->setLinkForFill($this->filler->getLinkForFill($entity->getLink())); } return $entity; }