369 lines
16 KiB
PHP
369 lines
16 KiB
PHP
|
<?php
|
||
|
namespace App\Supports\Traits;
|
||
|
|
||
|
use App\Services\TmdbClient;
|
||
|
use App\Supports\Traits\Helpers;
|
||
|
use Carbon\Carbon;
|
||
|
use Carbon\CarbonInterval;
|
||
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||
|
use Illuminate\Pagination\Paginator;
|
||
|
use Illuminate\Support\Str;
|
||
|
use Illuminate\Support\Facades\Cache;
|
||
|
|
||
|
trait CleanItems {
|
||
|
|
||
|
use Helpers;
|
||
|
|
||
|
protected function getPageLabel(?string $label)
|
||
|
{
|
||
|
switch ($label) {
|
||
|
case 'pagination.previous':
|
||
|
return 'Previous';
|
||
|
break;
|
||
|
case 'pagination.next':
|
||
|
return 'Next';
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
return $label;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
protected function getPageUrl(?string $url, $route_name = 'movies')
|
||
|
{
|
||
|
if (isset($url) && preg_match('~page=(\d+)~', $url, $matches)) {
|
||
|
[,$page] = $matches;
|
||
|
return route($route_name, compact('page'));
|
||
|
}
|
||
|
|
||
|
return $url;
|
||
|
|
||
|
}
|
||
|
|
||
|
public function formatTmdbMovie($data):array
|
||
|
{
|
||
|
$movie = [];
|
||
|
$movie['id'] = self::encodeId($data['id']);
|
||
|
$movie['real_id'] = $data['id'];
|
||
|
// dd($data);
|
||
|
// dump($data);
|
||
|
|
||
|
|
||
|
$movie['title'] = $data['title'];
|
||
|
$movie['slug'] = Str::slug($data['title']);
|
||
|
$movie['overview'] = $data['overview'];
|
||
|
$movie['tagline'] = $data['tagline'];
|
||
|
$movie['vote_average'] = round($data['vote_average'], 1);
|
||
|
$movie['vote_count'] = $data['vote_count'];
|
||
|
$movie['poster'] = $this->getImageUrl($data['poster_path'], 'w500', 300, 450);
|
||
|
$movie['backdrop'] = $this->getImageUrl($data['backdrop_path'], 'w780', null, null, true);
|
||
|
$movie['backdrop_path'] = $data['backdrop_path'];
|
||
|
$movie['release_date'] = $this->formatReleaseDate($data['release_date']);
|
||
|
$movie['year'] = Carbon::parse($data['release_date'])->format('Y');
|
||
|
$movie['genres'] = $this->formatGenres($data['genres']);
|
||
|
$movie['cast'] = $this->getCast($data['credits']['cast'] ?? [], 8);
|
||
|
$movie['crew']['directors'] = $this->getCrew($data['credits']['crew'] ?? [], 8, true);
|
||
|
$movie['crew']['producers'] = $this->getMovieProdcer($data['credits']['crew'] ?? [], 2);
|
||
|
$movie['runtime'] = CarbonInterval::make($data['runtime'], 'minute')->cascade()->forHumans(['short' => true]);
|
||
|
$movie['duration'] = CarbonInterval::make($data['runtime'], 'minute')->totalSeconds;
|
||
|
$movie['countries'] = collect($data['production_countries'])->implode('name', ', ');
|
||
|
$movie['companies'] = collect($data['production_companies'])->implode('name', ', ');
|
||
|
$movie['trailer'] = $data['videos']['results'][0]['key'] ?? false;
|
||
|
if($movie['trailer']) $movie['trailer'] = 'https://www.youtube-nocookie.com/embed/' . $movie['trailer'];
|
||
|
$movie['similar'] = $this->formatTmdbResponse($data['similar'] ?? [], false, [], 8)['data'] ?? [];
|
||
|
$movie['keywords'] = $data['keywords']['keywords'] ?? [];
|
||
|
$movie['share'] = self::getShareLinks(route('movie', ['id' => $movie['id'], 'slug' => $movie['slug']]), $movie['title'], Str::limit($movie['overview'], 100), $movie['poster']);
|
||
|
$movie['languages'] = self::getSpokenLanguages($data['spoken_languages']);
|
||
|
// dump($movie);
|
||
|
return $movie;
|
||
|
|
||
|
}
|
||
|
public function formatTmdbShow($data):array
|
||
|
{
|
||
|
// dd($data);
|
||
|
$show = [];
|
||
|
$show['id'] = self::encodeId($data['id']);
|
||
|
$show['real_id'] = $data['id'];
|
||
|
$show['title'] = $data['name'];
|
||
|
$show['slug'] = Str::slug($data['name']);
|
||
|
$show['overview'] = $data['overview'];
|
||
|
$show['last_episode'] = ['season' => $data['last_episode_to_air']['season_number'] ?? 1, 'episode' => $data['last_episode_to_air']['episode_number'] ?? 1, 'name' => sprintf('S%sE%s : %s', $data['last_episode_to_air']['season_number'] ?? 1, $data['last_episode_to_air']['episode_number'] ?? 1, $data['last_episode_to_air']['name'] ?? 'Episode 1')];
|
||
|
$show['tagline'] = $data['tagline'];
|
||
|
$show['vote_average'] = round($data['vote_average'], 1);
|
||
|
$show['vote_count'] = $data['vote_count'];
|
||
|
$show['number_of_episodes'] = $data['number_of_episodes'];
|
||
|
$show['number_of_seasons'] = $data['number_of_seasons'];
|
||
|
$show['poster'] = $this->getImageUrl($data['poster_path'], 'w500', 230, 345);
|
||
|
$show['backdrop'] = $this->getImageUrl($data['backdrop_path'], 'w1280', 1280, 720);
|
||
|
$show['first_air_date'] = $this->formatReleaseDate($data['first_air_date']);
|
||
|
$show['genres'] = $this->formatGenres($data['genres']);
|
||
|
$show['cast'] = $this->getCast($data['credits']['cast'] ?? [], 8);
|
||
|
$show['creators'] = $this->getCreators($data['created_by'] ?? [], 8);
|
||
|
$show['crew'] = $this->getCrew($data['credits']['crew'] ?? [], 8);
|
||
|
$show['runtime'] = isset($data['episode_run_time'][0]) ? CarbonInterval::make($data['episode_run_time'][0], 'minute')->cascade()->forHumans(['short' => true]) : false;
|
||
|
$show['duration'] = isset($data['episode_run_time'][0]) ? CarbonInterval::make($data['episode_run_time'][0], 'minute')->totalSeconds : false;
|
||
|
$show['countries'] = collect($data['production_countries'])->implode('name', ', ');
|
||
|
$show['companies'] = collect($data['production_companies'])->implode('name', ', ');
|
||
|
$show['trailer'] = $data['videos']['results'][0]['key'] ?? false;
|
||
|
$show['seasons'] = collect($data['seasons'])->where('season_number', '>', 0)->map(function($season){
|
||
|
$season['poster'] = $this->getImageUrl($season['poster_path'], 'w500', 240, 360);
|
||
|
unset($season['poster_path']);
|
||
|
return $season;
|
||
|
})->values()->all();
|
||
|
$show['similar'] = $this->formatTmdbResponse($data['recommendations'], false, [], 6)['data'] ?? [];
|
||
|
$show['keywords'] = $data['keywords']['results'] ?? [];
|
||
|
// dd($show);
|
||
|
return $show;
|
||
|
|
||
|
}
|
||
|
public function formatTmdbSeason($data, $showData):array
|
||
|
{
|
||
|
// dd($data);
|
||
|
$show = $data;
|
||
|
$show['label'] = 'S'.str_pad($data['season_number'], 2, '0', STR_PAD_LEFT);
|
||
|
$show['overview'] = $data['overview'] !== '' ? $data['overview'] : $showData['overview'];
|
||
|
$show['cast'] = $this->getCast($show['credits']['cast'] ?? $showData['credits']['cast'] ?? [], 8);
|
||
|
$show['crew'] = $this->getCrew($show['credits']['crew'] ?? $showData['credits']['crew'] ?? [], 8);
|
||
|
$show['air_date'] = Helpers::formatReleaseDate($data['air_date']);
|
||
|
$show['poster'] = $this->getImageUrl($data['poster_path'], 'w500', 230, 345);
|
||
|
$show['episodes'] = collect($show['episodes'])->map(function($episode){
|
||
|
$episode['air_date'] = Helpers::formatReleaseDate($episode['air_date']);
|
||
|
$episode['label'] = /* 'S'.str_pad($episode['season_number'], 2, '0', STR_PAD_LEFT). 'E'.*/str_pad($episode['episode_number'], 2, '0', STR_PAD_LEFT);
|
||
|
$episode['backdrop'] = $this->getImageUrl($episode['still_path'], 'w1280', 1280, 720);
|
||
|
unset($episode['still_path']);
|
||
|
return $episode;
|
||
|
});
|
||
|
$show['keywords'] = $data['keywords']['results'] ?? [];
|
||
|
|
||
|
unset($show['credits']);
|
||
|
|
||
|
return $show;
|
||
|
}
|
||
|
public function formatTmdbEpisode($data):array
|
||
|
{
|
||
|
// dd($data);
|
||
|
$episode = $data;
|
||
|
$episode['label'] = 'S'.str_pad($episode['season_number'], 2, '0', STR_PAD_LEFT).'E'.str_pad($episode['episode_number'], 2, '0', STR_PAD_LEFT);
|
||
|
$episode['backdrop'] = $this->getImageUrl($episode['still_path'], 'w1280', 1280, 720);
|
||
|
|
||
|
$episode['cast'] = $this->getCast($episode['credits']['cast'] ?? [], 8);
|
||
|
$episode['crew'] = $this->getCrew($episode['credits']['crew'] ?? [], 8);
|
||
|
$episode['air_date'] = Helpers::formatReleaseDate($episode['air_date']);
|
||
|
$episode['vote_average'] = round($episode['vote_average']/2, 1);
|
||
|
$episode['label'] = 'S'.str_pad($episode['season_number'], 2, '0', STR_PAD_LEFT).'E'.str_pad($episode['episode_number'], 2, '0', STR_PAD_LEFT);
|
||
|
|
||
|
unset($episode['still_path']);
|
||
|
|
||
|
unset($episode['credits']);
|
||
|
|
||
|
return $episode;
|
||
|
|
||
|
}
|
||
|
|
||
|
protected static function setItemTypeMovie($item)
|
||
|
{
|
||
|
$item['type'] = 'movie';
|
||
|
return $item;
|
||
|
}
|
||
|
protected static function setItemTypeShow($item)
|
||
|
{
|
||
|
$item['type'] = 'show';
|
||
|
return $item;
|
||
|
}
|
||
|
|
||
|
public static function encodeForInertia($data):string
|
||
|
{
|
||
|
$data = base64_encode(json_encode($data));
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function getHomeHeroSliderData($movies, TmdbClient $tmdb)
|
||
|
{
|
||
|
return Cache::remember('home_hero_slider_nyxy', 3600 * 6, function()use($movies, $tmdb){
|
||
|
$movies = (array)$movies;
|
||
|
// dd($movies['data']);
|
||
|
|
||
|
$movies = collect($movies['data'])->map(fn($item) => self::setItemTypeMovie((array)$item))->map(function($movie) use($tmdb){
|
||
|
$movie = (array)$movie;
|
||
|
return $tmdb->getMovie($movie['id']);
|
||
|
// dd($this->formatTmdbMovie($movie));
|
||
|
// return $this->formatTmdbMovie($movie);
|
||
|
})
|
||
|
->map(function($item){
|
||
|
$item['overview'] = Str::limit($item['overview'], 250);
|
||
|
return $item;
|
||
|
});
|
||
|
// dd($movies);
|
||
|
return $movies->all();
|
||
|
});
|
||
|
|
||
|
// $shows = collect($shows['data'])->map(fn($item) => self::setItemTypeShow($item));
|
||
|
// dd($movies, $shows);
|
||
|
}
|
||
|
|
||
|
public function formatApiResponse(Object $data, $with_pagination = true, $route = [], $limit = false): array
|
||
|
{
|
||
|
// dd($data->data);
|
||
|
// dd($route);
|
||
|
|
||
|
$response = [];
|
||
|
$results = [];
|
||
|
if (!empty($data->data)) {
|
||
|
$items = collect($data->data);
|
||
|
$route_name = isset($items->first()->title) ? 'movies':'shows';
|
||
|
|
||
|
|
||
|
|
||
|
$results = $items->map(function($item, $key){
|
||
|
return [
|
||
|
'id' => self::encodeId($item->id),
|
||
|
// 'real_id' => $item->id,
|
||
|
'title' => $item->title ?? $item->name,
|
||
|
'views' => $item->views,
|
||
|
'vote_average' => round($item->vote_average, 1),
|
||
|
'overview' => $item->overview ?? $item->overview,
|
||
|
'image' => self::getImageUrl($item->poster_path, 'w500', 300, 450),
|
||
|
'backdrop' => self::getImageUrl($item->backdrop_path, 'w780', null, null, true),
|
||
|
// 'link' => self::getItemUrl($item),
|
||
|
'slug' => Str::slug($item->title ?? $item->name),
|
||
|
'year' => Carbon::parse($item->release_date ?? '')->format('Y'),
|
||
|
'route' => isset($item->title) ? 'movie' : 'show',
|
||
|
// 'count' => $key + 1
|
||
|
];
|
||
|
|
||
|
})->when($limit, fn($collection) => $collection->take($limit))->all();
|
||
|
}
|
||
|
// dd($results);
|
||
|
|
||
|
$response['data'] =/* base64_encode(json_encode( */$results/* )) */;
|
||
|
|
||
|
// $paginator = new LengthAwarePaginator($data->data, $data->total, 30, $data->current_page, [
|
||
|
// 'path' => Paginator::resolveCurrentPath()
|
||
|
// ]);
|
||
|
// dd($data->links);
|
||
|
if ($with_pagination) {
|
||
|
$response['pagination'] = new LengthAwarePaginator($response['data'], $data->total, $data->per_page, $data->current_page, [
|
||
|
'path' => request()->url()
|
||
|
]);
|
||
|
// dd($response['pagination']->links());
|
||
|
// $response['pagination'] = [
|
||
|
// 'current_page' => $data->current_page,
|
||
|
// 'per_page' => $data->per_page,
|
||
|
// 'from' => $data->from,
|
||
|
// 'to' => $data->to,
|
||
|
// 'total' => $data->total,
|
||
|
// 'last_page' => $data->last_page,
|
||
|
|
||
|
// ];
|
||
|
// $links = [];
|
||
|
// $pages = range($data->current_page - 5, $data->current_page + 5);
|
||
|
// foreach ($pages as $page) {
|
||
|
// if ($page > 0 && $page != $data->current_page) {
|
||
|
// $links[] = [
|
||
|
// 'page' => $page,
|
||
|
// 'link' => route($route['name'], ['page' => $page, ...$route['params'] ?? []])
|
||
|
// ];
|
||
|
// }
|
||
|
// }
|
||
|
// $response['pagination']['links'] = $links;
|
||
|
}
|
||
|
|
||
|
// dd($response);
|
||
|
return $response;
|
||
|
}
|
||
|
public function formatTmdbSearchResponse(array $data): array
|
||
|
{
|
||
|
// dd($data->data);
|
||
|
// dd($data);
|
||
|
|
||
|
$response = [];
|
||
|
$results = [];
|
||
|
if (!empty($data['results'])) {
|
||
|
$items = collect($data['results'])->filter(fn($item) => in_array($item['media_type'], ['movie', 'tv']))->map(fn($item) =>(Object)$item);
|
||
|
$results = $items->map(function($item) {
|
||
|
return [
|
||
|
'id' => self::encodeId($item->id),
|
||
|
'title' => $item->title ?? $item->name,
|
||
|
// 'overview' => $item->overview ?? $item->overview,
|
||
|
'image' => self::getImageUrl($item->poster_path, 'w500', 230, 345),
|
||
|
// 'backdrop' => self::getImageUrl($item->backdrop_path, 'original', 1920, 1080),
|
||
|
// 'image_low' => self::getImageUrl($item->poster_path, 'w500', 20, 30), first_air_date
|
||
|
'link' => self::getItemUrl($item),
|
||
|
'slug' => Str::slug($item->title ?? $item->name),
|
||
|
'year' => isset($item->release_date) ? Carbon::parse($item->release_date ?? '')->format('Y') : Carbon::parse($item->first_air_date ?? '')->format('Y'),
|
||
|
'type' => match($item->media_type) {'movie' => 'movie', 'tv' => 'show'},
|
||
|
];
|
||
|
})->groupBy('type')->map(fn($typeItems) => $typeItems->all())->all();
|
||
|
}
|
||
|
// dd($results);
|
||
|
return $results;
|
||
|
}
|
||
|
public function formatTmdbResponse(array $data, $with_pagination = false, $route = [], $limit = false): array
|
||
|
{
|
||
|
// dd($data->data);
|
||
|
// dd($data);
|
||
|
|
||
|
$response = [];
|
||
|
$results = [];
|
||
|
if (!empty($data['results'])) {
|
||
|
$items = collect($data['results'])->map(fn($item) =>(Object)$item);
|
||
|
$type = isset($items->first()->title) ? 'movie':'show';
|
||
|
|
||
|
|
||
|
$results = $items->map(function($item) use($type){
|
||
|
// dd($item);
|
||
|
return [
|
||
|
'id' => self::encodeId($item->id),
|
||
|
'title' => $item->title ?? $item->name,
|
||
|
'views' => $item->vote_count,
|
||
|
'vote_average' => round($item->vote_average, 1),
|
||
|
// 'overview' => $item->overview ?? $item->overview,
|
||
|
'image' => self::getImageUrl($item->poster_path, 'w185', null, null),
|
||
|
'backdrop' => self::getImageUrl($item->backdrop_path, 'w780', null, null, true),
|
||
|
// 'link' => self::getItemUrl($item),
|
||
|
'slug' => Str::slug($item->title ?? $item->name),
|
||
|
'year' => Carbon::parse($item->release_date ?? $item->first_air_date ?? '')->format('Y'),
|
||
|
'date' => $item->release_date ?? $item->first_air_date ?? Carbon::now()->format('Y-m-d'),
|
||
|
'route' => isset($item->title) ? 'movie' : 'show',
|
||
|
'type' => $type,
|
||
|
];
|
||
|
})->when($limit, fn($collection) => $collection->take($limit))->all();
|
||
|
}
|
||
|
// dd($results);
|
||
|
|
||
|
$response['data'] =/* base64_encode(json_encode( */$results/* )) */;
|
||
|
|
||
|
// $paginator = new LengthAwarePaginator($data->data, $data->total, 30, $data->current_page, [
|
||
|
// 'path' => Paginator::resolveCurrentPath()
|
||
|
// ]);
|
||
|
// dd($data->links);
|
||
|
if ($with_pagination) {
|
||
|
// dd($data);
|
||
|
$response['pagination'] = [
|
||
|
'current_page' => $data['page'],
|
||
|
'per_page' => 20,
|
||
|
|
||
|
'total' => $data['total_results'],
|
||
|
'last_page' => $data['total_pages'],
|
||
|
|
||
|
];
|
||
|
$links = [];
|
||
|
$pages = range($data['page'] - 5, $data['page'] + 5);
|
||
|
foreach ($pages as $page) {
|
||
|
if ($page > 0 && $page != $data['page']) {
|
||
|
$links[] = [
|
||
|
'page' => $page,
|
||
|
'link' => route($route['name'], ['page' => $page, ...$route['params'] ?? []])
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
$response['pagination']['links'] = $links;
|
||
|
}
|
||
|
|
||
|
// dd($response);
|
||
|
return $response;
|
||
|
}
|
||
|
}
|