/**
  * 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;
 }
 /**
  * @param AddNewItem $event
  */
 public function onAddNewItem(AddNewItem $event)
 {
     $item = $event->getItem();
     if (!$event->getFillers()->contains($this->filler) && ($url = $this->refiller->getSourceForFill($item)) && preg_match(Filler::REG_ITEM_ID, $url, $match)) {
         try {
             // get data
             $body = $this->browser->get('anime', ['aid' => $match['id']]);
         } catch (\Exception $e) {
             return;
         }
         // fill item
         if (!$item->getDateEnd()) {
             $item->setDateEnd(new \DateTime($body->filter('enddate')->text()));
         }
         if (!$item->getDatePremiere()) {
             $item->setDatePremiere(new \DateTime($body->filter('startdate')->text()));
         }
         if (!$item->getEpisodes()) {
             $this->filler->setEpisodes($item, $body);
         }
         if (!$item->getEpisodesNumber()) {
             $item->setEpisodesNumber($body->filter('episodecount')->text());
         }
         if (!$item->getSummary()) {
             $item->setSummary($this->cleaner->clean($body->filter('description')->text()));
         }
         if (!$item->getType()) {
             $this->filler->setType($item, $body);
         }
         if (!$item->getCover()) {
             $this->filler->setCover($item, $body, $match['id']);
         }
         $this->filler->setGenres($item, $body);
         // copy main and other names
         $new_item = $this->filler->setNames(new Item(), $body);
         // set main name in top of names list
         $new_names = $new_item->getNames()->toArray();
         array_unshift($new_names, (new Name())->setName($new_item->getName()));
         /* @var $new_name Name */
         foreach ($new_names as $new_name) {
             $item->addName($new_name->setItem(null));
         }
         $event->addFiller($this->filler);
         // resend event
         $this->dispatcher->dispatch(StoreEvents::ADD_NEW_ITEM, clone $event);
         $event->stopPropagation();
     }
 }
 /**
  * Refill item field from source.
  *
  * @param Item $item
  * @param string $field
  *
  * @return Item
  */
 public function refill(Item $item, $field)
 {
     $url = $this->getSourceForFill($item);
     if (!$url || !preg_match(Filler::REG_ITEM_ID, $url, $match)) {
         return $item;
     }
     // get data
     $body = $this->browser->get('anime', ['aid' => $match['id']]);
     switch ($field) {
         case self::FIELD_DATE_END:
             $item->setDateEnd(new \DateTime($body->filter('enddate')->text()));
             break;
         case self::FIELD_DATE_PREMIERE:
             $item->setDatePremiere(new \DateTime($body->filter('startdate')->text()));
             break;
         case self::FIELD_EPISODES:
             $this->filler->setEpisodes($item, $body);
             break;
         case self::FIELD_EPISODES_NUMBER:
             $item->setEpisodesNumber($body->filter('episodecount')->text());
             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_NAMES:
             $new_item = $this->filler->setNames(new Item(), $body);
             // 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:
             if ($url = $body->filter('url')->text()) {
                 $item->addSource((new Source())->setUrl($url));
             }
             break;
         case self::FIELD_SUMMARY:
             $item->setSummary($this->cleaner->clean($body->filter('description')->text()));
             break;
     }
     return $item;
 }