示例#1
0
 /**
  * @param Item $item
  *
  * @return Image
  */
 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->removeImage($this);
         }
         $this->item = $item;
         // add link on this item
         if ($item instanceof Item) {
             $this->item->addImage($this);
         }
     }
     return $this;
 }
 /**
  * Fill body data
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \DOMXPath $xpath
  * @param \DOMElement $body
  * @param integer $id
  * @param boolean $frames
  * @param string $type
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 private function fillBodyData(Item $item, \DOMXPath $xpath, \DOMElement $body, $id, $frames, $type)
 {
     for ($i = 0; $i < $body->childNodes->length; $i++) {
         if ($value = trim($body->childNodes->item($i)->nodeValue)) {
             switch ($value) {
                 // get summary
                 case 'Краткое содержание:':
                     $summary = $xpath->query('tr/td/p[1]', $body->childNodes->item($i + 2));
                     if ($summary->length) {
                         $item->setSummary($this->getNodeValueAsText($summary->item(0)));
                     }
                     $i += 2;
                     break;
                     // get episodes
                 // get episodes
                 case 'Эпизоды:':
                     if (!trim($body->childNodes->item($i + 1)->nodeValue)) {
                         // simple list
                         $item->setEpisodes($this->getNodeValueAsText($body->childNodes->item($i + 2)));
                         $i += 2;
                     } else {
                         // episodes in table
                         $rows = $xpath->query('tr/td[2]', $body->childNodes->item($i + 1));
                         $episodes = '';
                         for ($j = 1; $j < $rows->length; $j++) {
                             $episode = $xpath->query('font', $rows->item($j));
                             $episodes .= $j . '. ' . $episode->item(0)->nodeValue;
                             if ($rows->length > 1) {
                                 $episodes .= ' (' . $episode->item(1)->nodeValue . ')';
                             }
                             $episodes .= "\n";
                         }
                         $item->setEpisodes($episodes);
                         $i++;
                     }
                     break;
                     // get date premiere
                 // get date premiere
                 case 'Даты премьер и релизов':
                     $rows = $xpath->query('tr/td/table/tr/td[3]', $body->childNodes->item($i + 1));
                     foreach ($rows as $row) {
                         if (preg_match('/\\d{4}\\.\\d{2}\\.\\d{2}/', $row->nodeValue, $match)) {
                             $date = new \DateTime(str_replace('.', '-', $match[0]));
                             if (!$item->getDatePremiere() || $item->getDatePremiere() > $date) {
                                 $item->setDatePremiere($date);
                             }
                         }
                     }
                     break;
                 default:
                     // get frames
                     if ((strpos($value, 'кадры из аниме') !== false || strpos($value, 'Кадры из фильма') !== false) && $id && $frames) {
                         foreach ($this->getFrames($id, $type) as $frame) {
                             $item->addImage($frame);
                         }
                     }
             }
         }
     }
 }
 /**
  * @param Item $item
  * @param array $body
  *
  * @return Item
  */
 public function setImages(Item $item, $body)
 {
     $images = $this->browser->get(str_replace('#ID#', $body['id'], self::FILL_IMAGES_URL));
     foreach ($images as $image) {
         if ($path = parse_url($image['original'], PHP_URL_PATH)) {
             $image = new Image();
             $image->setSource(self::NAME . '/' . $body['id'] . '/' . pathinfo($path, PATHINFO_BASENAME));
             if ($this->uploadImage($this->browser->getHost() . $image['original'], $image)) {
                 $item->addImage($image);
             }
         }
     }
     return $item;
 }