/** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); $builder->get('name')->setRequired(false); // get genres $list = $this->browser->get('/genres'); $genres = []; while ($genre = array_shift($list)) { if ($this->locale == 'ru') { $genres[$genre['id']] = $genre['russian']; } else { $genres[$genre['id']] = $genre['name']; } } $builder->add('genre', 'choice', ['choices' => $genres, 'required' => false])->add('type', 'choice', ['choices' => array_combine($this->types, $this->types), 'required' => false])->add('season', 'text', ['required' => false, 'label' => 'Year of the premier', 'help' => 'You can select the period of the years indicated by a dash: 2002-2004']); }
/** * 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(); } }
/** * @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; }
/** * @param array $data * * @return ItemSearch[] */ public function search(array $data) { $path = str_replace('#NAME#', urlencode($data['name']), self::SEARH_URL); $path = str_replace('#LIMIT#', self::DEFAULT_LIMIT, $path); $path = str_replace('#GENRE#', isset($data['genre']) ? $data['genre'] : '', $path); $path = str_replace('#TYPE#', isset($data['type']) ? $data['type'] : '', $path); $path = str_replace('#SEASON#', isset($data['season']) ? str_replace('-', '_', $data['season']) : '', $path); $body = (array) $this->browser->get($path); // build list foreach ($body as $key => $item) { // set a name based on the locale if ($this->locale == 'ru' && $item['russian']) { $name = $item['russian']; $description = $item['name']; } else { $name = $item['name']; $description = $item['russian']; } $body[$key] = new ItemSearch($name, $this->getLinkForFill($this->browser->getHost() . $item['url']), $this->browser->getHost() . $item['image']['original'], $description, $this->browser->getHost() . $item['url']); } return $body; }
public function testGet() { $data = ['test' => 123]; $this->buildDialogue('baz', false, $data); $this->assertEquals($data, $this->browser->get('baz')); }
/** * Get item info by id * * @param integer $id * * @return array */ public function getItem($id) { return $this->browser->get(str_replace('#ID#', $id, self::PATH_ITEM_INFO)); }