client = $client; } public function search(string $keyword): tableData { $this->query = ['q' => trim($keyword)]; $this->searchUrl = $this->baseUrl; $this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks(); if ($this->hasErrors()) { return tableData::create()->setEror($this->getErrors()); } return tableData::create($this->getTableTitles(), $this->getRows()); } public function getItems() { $data = $this->getResponse(); $this->rows = $this->transformResp($data); return $this; } protected function getDownloadUrl(array $item): string { extract($item); return sprintf("https://slider.kz/download/%s/%s/%s/%s.mp3?extra=null", $id, $duration, $url, urlencode($tit_art)); } private function transformResp($data): array { $items = []; if (count($data) < 1 || $this->hasErrors()) { return []; } foreach ($data as $item) { if (empty($item)) { continue; } $items[] = array("title" => $item["tit_art"], "data-link" => $this->getDownloadUrl($item), "duration" => Helper::formatInterval($item["duration"])); } unset($data); return $items; } public function getResponse(): array { try { $response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]); $resp = $response->toArray(); if (isset($resp['audios'])) { return array_values($resp["audios"])[0]; } } catch (ExceptionInterface $e) { $this->errors[] = $e->getMessage(); } return []; } public static function getLabel(): string { return 'music'; } }