123movies-seo/app/Supports/Traits/CleanItems.php

396 lines
18 KiB
PHP
Raw Normal View History

2024-08-24 23:08:42 +03:00
<?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';
case 'pagination.next':
return 'Next';
default:
return $label;
}
}
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'];
2024-08-25 18:55:02 +03:00
$movie['similar'] = $this->formatTmdbResponse($data['recommendations'] ?? [], false, [], 8)['data'] ?? [];
2024-08-24 23:08:42 +03:00
$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'];
2024-08-26 22:05:12 +03:00
$show['poster'] = $this->getImageUrl($data['poster_path'], 'w500', 300, 450);
2024-08-24 23:08:42 +03:00
$show['backdrop'] = $this->getImageUrl($data['backdrop_path'], 'w1280', 1280, 720);
2024-08-26 22:05:12 +03:00
$show['backdrop_path'] = $data['backdrop_path'];
2024-08-24 23:08:42 +03:00
$show['first_air_date'] = $this->formatReleaseDate($data['first_air_date']);
2024-08-26 22:05:12 +03:00
$show['year'] = $this->formatDateYear($data['first_air_date']);
2024-08-24 23:08:42 +03:00
$show['genres'] = $this->formatGenres($data['genres']);
$show['cast'] = $this->getCast($data['credits']['cast'] ?? [], 8);
2024-08-26 22:05:12 +03:00
$show['crew']['creators'] = $this->getCreators($data['created_by'] ?? [], 8);
$show['crew']['producers'] = $this->getCrew($data['credits']['crew'] ?? [], 8);
2024-08-24 23:08:42 +03:00
$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){
2024-08-27 18:46:07 +03:00
$season['poster'] = $this->getImageUrl($season['poster_path'], 'w500', 120, 180);
2024-08-24 23:08:42 +03:00
unset($season['poster_path']);
return $season;
})->values()->all();
2024-08-26 22:05:12 +03:00
$show['similar'] = $this->formatTmdbResponse($data['recommendations'], false, [], 8)['data'] ?? [];
2024-08-24 23:08:42 +03:00
$show['keywords'] = $data['keywords']['results'] ?? [];
2024-08-26 22:05:12 +03:00
$show['languages'] = self::getSpokenLanguages($data['spoken_languages']);
2024-08-26 22:11:33 +03:00
$show['share'] = self::getShareLinks(route('movie', ['id' => $show['id'], 'slug' => $show['slug']]), $show['title'], Str::limit($show['overview'], 100), $show['poster']);
2024-08-24 23:08:42 +03:00
// 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);
2024-08-27 18:46:07 +03:00
$show['crew']['producers'] = $this->getCrew($show['credits']['crew'] ?? $showData['credits']['crew'] ?? [], 8);
2024-08-30 19:49:30 +03:00
$show['air_date'] = self::formatReleaseDate($data['air_date']);
2024-08-24 23:08:42 +03:00
$show['poster'] = $this->getImageUrl($data['poster_path'], 'w500', 230, 345);
$show['episodes'] = collect($show['episodes'])->map(function($episode){
2024-08-30 19:49:30 +03:00
$episode['air_date'] = self::formatReleaseDate($episode['air_date']);
2024-08-24 23:08:42 +03:00
$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);
2024-08-30 19:49:30 +03:00
$episode['air_date'] = self::formatReleaseDate($episode['air_date']);
$episode['vote_average'] = round($episode['vote_average'], 1);
2024-08-24 23:08:42 +03:00
$episode['label'] = 'S'.str_pad($episode['season_number'], 2, '0', STR_PAD_LEFT).'E'.str_pad($episode['episode_number'], 2, '0', STR_PAD_LEFT);
2024-08-30 19:49:30 +03:00
// unset($episode['still_path']);
2024-08-24 23:08:42 +03:00
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)
{
2024-08-25 16:11:19 +03:00
return Cache::remember('home_hero_slider', 3600 * 6, function()use($movies, $tmdb){
2024-08-24 23:08:42 +03:00
$movies = (array)$movies;
$movies = collect($movies['data'])->map(fn($item) => self::setItemTypeMovie((array)$item))->map(function($movie) use($tmdb){
$movie = (array)$movie;
return $tmdb->getMovie($movie['id']);
})
->map(function($item){
$item['overview'] = Str::limit($item['overview'], 250);
return $item;
});
return $movies->all();
});
}
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',
2024-08-30 16:12:40 +03:00
'type' => isset($item->title) ? 'movie' : 'show',
2024-08-24 23:08:42 +03:00
// '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'},
];
2024-08-27 18:46:07 +03:00
})/* ->map(fn($typeItems) => $typeItems->all()) */->all();
2024-08-24 23:08:42 +03:00
}
2024-08-27 18:46:07 +03:00
$response['data'] = $results;
$response['pagination'] = new LengthAwarePaginator($response['data'], $data['total_results'], 20, $data['page'], [
'path' => request()->url()
]);
return $response;
2024-08-24 23:08:42 +03:00
}
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,
2024-08-25 18:55:02 +03:00
'image' => self::getImageUrl($item->poster_path, 'w500', 300, 450),
2024-08-24 23:08:42 +03:00
'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'] ?? []])
];
}
}
2024-08-29 05:49:54 +03:00
2024-08-24 23:08:42 +03:00
$response['pagination']['links'] = $links;
}
// dd($response);
return $response;
}
2024-08-29 05:49:54 +03:00
public function formatTmdbPersonResponse(array $data, $page = 1, $limit = false):array
{
$response = [];
$results = collect([]);
$response['name'] = $data['name'];
$results = collect($data['titles'])->map(function($item) {
$item = (Object)$item;
return [
'id' => self::encodeId($item->id),
'title' => $item->title,
'image' => self::getImageUrl($item->poster_path, 'w300', null, null),
'slug' => Str::slug($item->title) === '' ? sprintf('f%sl', $item->id) : Str::slug($item->title),
'year' => Carbon::parse($item->date)->format('Y'),
'date' => $item->date ?? $item->first_air_date ?? Carbon::now()->format('Y-m-d'),
'type' => $item->type,
'route' => $item->type,
];
})->when($limit, fn($collection) => $collection->take($limit));//->sortByDesc('popularity');
$response['data'] = $results;
$response['pagination'] = new LengthAwarePaginator($results->forPage($page, 20), count($results), 20, $page, [
'path' => request()->url()
]);
return $response;
}
2024-08-24 23:08:42 +03:00
}