Пример #1
0
 /**
  * Fill head data
  *
  * @param \AnimeDb\Bundle\CatalogBundle\Entity\Item $item
  * @param \DOMXPath $xpath
  * @param \DOMElement $head
  *
  * @return \AnimeDb\Bundle\CatalogBundle\Entity\Item
  */
 private function fillHeadData(Item $item, \DOMXPath $xpath, \DOMElement $head)
 {
     /* @var $data \DOMElement */
     $data = $xpath->query('font', $head)->item(0);
     $length = $data->childNodes->length;
     for ($i = 0; $i < $length; $i++) {
         if ($data->childNodes->item($i)->nodeName == 'b') {
             switch ($data->childNodes->item($i)->nodeValue) {
                 // set country
                 case 'Производство':
                     $j = 1;
                     do {
                         if ($data->childNodes->item($i + $j)->nodeName == 'a') {
                             $country_name = trim($data->childNodes->item($i + $j)->nodeValue);
                             if ($country_name && ($country = $this->getCountryByName($country_name))) {
                                 $item->setCountry($country);
                             }
                             break;
                         }
                         $j++;
                     } while ($data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     break;
                     // add genre
                 // add genre
                 case 'Жанр':
                     $j = 2;
                     do {
                         if ($data->childNodes->item($i + $j)->nodeName == 'a' && ($genre = $this->getGenreByName($data->childNodes->item($i + $j)->nodeValue))) {
                             $item->addGenre($genre);
                         }
                         $j++;
                     } while ($data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     break;
                     // set type and add file info
                 // set type and add file info
                 case 'Тип':
                     $type = $data->childNodes->item($i + 1)->nodeValue;
                     if (preg_match('/(?<type>[\\w\\s]+)(?: \\((?:(?<episodes_number>>?\\d+) эп.)?(?<file_info>.*)\\))?(, (?<duration>\\d{1,3}) мин\\.)?$/u', $type, $match)) {
                         // add type
                         if ($type = $this->getTypeByName(trim($match['type']))) {
                             $item->setType($type);
                         }
                         // add duration
                         if (!empty($match['duration'])) {
                             $item->setDuration((int) $match['duration']);
                         }
                         // add number of episodes
                         if (!empty($match['episodes_number'])) {
                             if ($match['episodes_number'][0] == '>') {
                                 $item->setEpisodesNumber(substr($match['episodes_number'], 1) . '+');
                             } else {
                                 $item->setEpisodesNumber((int) $match['episodes_number']);
                             }
                         } elseif ($item->getType()->getId() != 'tv') {
                             // everything except the TV series consist of a single episode
                             $item->setEpisodesNumber(1);
                         }
                         // add file info
                         if (!empty($match['file_info'])) {
                             $file_info = $item->getFileInfo();
                             $item->setFileInfo(($file_info ? $file_info . "\n" : '') . trim($match['file_info']));
                         }
                     }
                     $i++;
                     break;
                     // set date premiere and date end if exists
                 // set date premiere and date end if exists
                 case 'Премьера':
                 case 'Выпуск':
                     $j = 1;
                     $date = '';
                     do {
                         $date .= $data->childNodes->item($i + $j)->nodeValue;
                         $j++;
                     } while ($length > $i + $j && $data->childNodes->item($i + $j)->nodeName != 'br');
                     $i += $j;
                     $reg = '/(?<start>(?:(?:\\d{2})|(?:\\?\\?)).\\d{2}.\\d{4})' . '(?:.*(?<end>(?:(?:\\d{2})|(?:\\?\\?)).\\d{2}.\\d{4}))?/';
                     if (preg_match($reg, $date, $match)) {
                         $item->setDatePremiere(new \DateTime(str_replace('??', '01', $match['start'])));
                         if (isset($match['end'])) {
                             $item->setDateEnd(new \DateTime($match['end']));
                         }
                     }
                     break;
                 case 'Хронометраж':
                     if (preg_match('/(?<duration>\\d+)/', $data->childNodes->item($i + 1)->nodeValue, $match)) {
                         $item->setDuration((int) $match['duration']);
                     }
                     break;
                 case 'Кол-во серий':
                     $number = trim($data->childNodes->item($i + 1)->nodeValue, ' :');
                     if (strpos($number, '>') !== false) {
                         $number = str_replace('>', '', $number) . '+';
                     }
                     $item->setEpisodesNumber($number);
                     break;
             }
         }
     }
 }
Пример #2
0
 /**
  * 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;
 }