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

293 lines
11 KiB
PHP

<?php
namespace App\Supports\Traits;
use Carbon\Carbon;
use Hashids\Hashids;
use Illuminate\Support\Str;
use League\CommonMark\Util\UrlEncoder;
use stdClass;
trait Helpers
{
public static function getImageUrl(?string $path = null, string $size = 'original', ?int $w = 300, ?int $h = 450, $backdrop_placeholder = false): string|null
{
if (is_null($path)) {
return null;
if ($backdrop_placeholder)
return config('app.env') === 'production' ? "https://i1.wp.com/".Str::replace('https://', '', asset('images/lazy_one.webp')) : asset('images/lazy_one.webp');
return config('app.env') === 'production' ? "https://i1.wp.com/".Str::replace('https://', '', asset('images/lazy.webp')) : asset('images/lazy_one.webp');
}
// return "https://cdn.statically.io/img/image.tmdb.org/f=auto,q=80,w={$w},h={$h}/t/p/{$size}{$path}";
if (is_null($w) && is_null($h))
return "https://i1.wp.com/image.tmdb.org/t/p/{$size}{$path}?ssl=1";
return "https://i1.wp.com/image.tmdb.org/t/p/{$size}{$path}?ssl=1&resize={$w},{$h}";
// return "https://image.tmdb.org/t/p/{$size}{$path}";
}
public static function getImageFullUrl(?string $url = null, int $w = 300, int $h = 450): string
{
if (is_null($url)) {
return asset('images/placeholder.webp');
}
return "https://i1.wp.com/".Str::of($url)->replace('https://', '')."?ssl=1&resize={$w},{$h}";
// return "https://image.tmdb.org/t/p/{$size}{$path}";
}
protected static function getHashIds():Hashids
{
return new Hashids('', 8, 'abcdefghijklmnopqrstuvwxyz');
}
public static function encodeId($id):int
{
return $id;
return self::getHashIds()->encode($id);
}
public static function decodeId($id):int
{
return $id;
return self::getHashIds()->decode($id)[0] ?? abort(404);
}
public static function getItemUrl(Object $item): string
{
if (isset($item->title)) {
return Str::slug($item->title);
}
if (isset($item->name)) {
return Str::slug($item->name);
}
}
public static function formatReleaseDate(?string $release_date): string
{
return Carbon::parse($release_date)->format('M d, Y');
}
public static function formatDateYear(?string $release_date): string
{
return Carbon::parse($release_date)->format('Y');
}
public static function formatGenres(array $genres, $type = 'movie'): array
{
return collect($genres)->map(fn ($genre) => [
'name' => $genre['name'],
'id' => $genre['id'],
'slug' => Str::slug($genre['name']),
])->all();
}
public static function getCast(array $actors, int $limit = 5): array
{
return collect($actors)->map(fn ($actor) => [
'id' => $actor['id'],
'name' => $actor['name'],
'slug' => str()->slug($actor['name']) !== '' ? str()->slug($actor['name']) : sprintf('a%sn', $actor['name']),
'as' => $actor['character'],
'image' => self::getImageUrl($actor['profile_path'], 'w500', 200, 300),
])->take($limit)->all();
}
public static function getCreators(array $actors, int $limit = 5): array
{
return collect($actors)->map(fn ($actor) => [
'id' => $actor['id'],
'name' => $actor['name'],
// 'as' => $actor['character'],
'image' => self::getImageUrl($actor['profile_path'], 'w500', 200, 300),
])->take($limit)->all();
}
public static function getCrew(array $actors, int $limit = 5, $is_movie = false): array
{
return collect($actors)
->when($is_movie, fn($col) => $col->filter(fn($director) => in_array($director['job'], ['Director'])))
->when(!$is_movie, fn($col) => $col->filter(fn($director) => in_array($director['job'], ['Producer', 'Executive Producer', 'Creator', 'Writer', 'Director'])))
->map(fn ($actor) => [
'id' => $actor['id'],
'name' => $actor['name'],
'as' => $actor['job'],
'image' => self::getImageUrl($actor['profile_path'], 'w500', 200, 300),
])->take($limit)->all();
}
public static function getSpokenLanguages(array $spoken_languages): array
{
return collect($spoken_languages)
->map(fn($language) => $language['english_name'])
->all();
}
public static function getMovieProdcer(array $actors, int $limit = 5): array
{
return collect($actors)
->filter(fn($director) => in_array($director['job'], ['Executive Producer']))
->map(fn ($actor) => [
'id' => $actor['id'],
'name' => $actor['name'],
'as' => $actor['job'],
'image' => self::getImageUrl($actor['profile_path'], 'w500', 200, 300),
])->take($limit)->all();
}
public static function getMovieGenreBySlug($slug):stdClass|null
{
return self::getMovieGenreList()->where('slug', $slug)->first();
}
public static function getShowGenreBySlug($slug):stdClass|null
{
return self::getShowGenreList()->where('slug', $slug)->first();
}
public static function getShowGenreList()
{
$genres = [
['id' => '10759', 'name' => 'Action & Adventure'],
['id' => '16', 'name' => 'Animation'],
['id' => '35', 'name' => 'Comedy'],
['id' => '80', 'name' => 'Crime'],
['id' => '99', 'name' => 'Documentary'],
['id' => '18', 'name' => 'Drama'],
['id' => '10751', 'name' => 'Family'],
['id' => '10762', 'name' => 'Kids'],
['id' => '9648', 'name' => 'Mystery'],
['id' => '10763', 'name' => 'News'],
['id' => '10764', 'name' => 'Reality'],
['id' => '10765', 'name' => 'Sci-Fi & Fantasy'],
['id' => '10766', 'name' => 'Soap'],
['id' => '10767', 'name' => 'Talk'],
['id' => '10768', 'name' => 'War & Politics'],
['id' => '37', 'name' => 'Western'],
];
return collect($genres)->map(function ($genre) {
$genre = (object)$genre;
$genre->slug = Str::slug($genre->name);
return $genre;
});
}
public static function getMovieGenreList()
{
$genres = [
['id' => '28', 'name' => 'Action'],
['id' => '12', 'name' => 'Adventure'],
['id' => '16', 'name' => 'Animation'],
['id' => '35', 'name' => 'Comedy'],
['id' => '80', 'name' => 'Crime'],
['id' => '99', 'name' => 'Documentary'],
['id' => '18', 'name' => 'Drama'],
['id' => '10751', 'name' => 'Family'],
['id' => '14', 'name' => 'Fantasy'],
['id' => '36', 'name' => 'History'],
['id' => '27', 'name' => 'Horror'],
['id' => '10402', 'name' => 'Music'],
['id' => '9648', 'name' => 'Mystery'],
['id' => '10749', 'name' => 'Romance'],
['id' => '878', 'name' => 'Science Fiction'],
['id' => '10770', 'name' => 'TV Movie'],
['id' => '53', 'name' => 'Thriller'],
['id' => '10752', 'name' => 'War'],
['id' => '37', 'name' => 'Western'],
];
return collect($genres)->map(function ($genre) {
$genre = (object)$genre;
$genre->slug = Str::slug($genre->name);
return $genre;
});
}
public static function getCountriesList()
{
$countries = [
['code' => 'us', 'name' => 'United States', 'slug' => 'united-states'],
['code' => 'gb', 'name' => 'United Kingdom', 'slug' => 'united-kingdom'],
['code' => 'au', 'name' => 'Australia', 'slug' => 'australia'],
['code' => 'ca', 'name' => 'Canada', 'slug' => 'canada'],
['code' => 'ie', 'name' => 'Ireland', 'slug' => 'ireland'],
['code' => 'kr', 'name' => 'South Korea', 'slug' => 'south-korea'],
['code' => 'my', 'name' => 'Malaysia', 'slug' => 'malaysia'],
['code' => 'id', 'name' => 'Indonesia', 'slug' => 'indonesia'],
['code' => 'tr', 'name' => 'Turkey', 'slug' => 'turkey'],
['code' => 'ar', 'name' => 'Argentina', 'slug' => 'argentina'],
['code' => 'kh', 'name' => 'Cambodia', 'slug' => 'cambodia'],
['code' => 'th', 'name' => 'Thailand', 'slug' => 'thailand'],
['code' => 'br', 'name' => 'Brazil', 'slug' => 'brazil'],
['code' => 'mx', 'name' => 'Mexico', 'slug' => 'mexico'],
['code' => 'jp', 'name' => 'Japan', 'slug' => 'japan'],
];
return collect($countries)->map(fn($country) => (Object)$country);
}
public static function getCountryBySlug($slug):stdClass|null
{
return self::getCountriesList()->where('slug', $slug)->first();
}
public static function getShareLinks(string $url, string $title, string $description, string $image):array
{
return [
[
'name' => 'Facebook',
'url' => sprintf('https://www.facebook.com/sharer/sharer.php?u=%s', urlencode($url)),
'icon' => 'la-facebook-f'
],
[
'name' => 'Linkedin',
'url' => sprintf('http://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=%s', urlencode($url), urlencode($title), urlencode($description)),
'icon' => 'la-linkedin-in'
],
[
'name' => 'Twitter',
'url' => sprintf('https://twitter.com/intent/tweet?text=%s', urlencode($title .'%0a'. $url)),
'icon' => 'la-twitter'
],
[
'name' => 'Pinterest',
'url' => sprintf('https://pinterest.com/pin/create/button/?url=%s&description=%s&media=%s',urlencode($url), urlencode($title), urlencode($image)),
'icon' => 'la-pinterest'
],
];
}
public static function filterTmdbPersonData(array $data):array
{
$movies = collect($data['movie_credits']['cast'] ?? [])->where('adult', false)->map(function($movie){
return [
'id' => $movie['id'],
'title' => $movie['title'],
'poster_path' => $movie['poster_path'],
'date' => $movie['release_date'],
'type' => 'movie'
];
});
$shows = collect($data['tv_credits']['cast'] ?? [])->where('adult', false)->map(function($show){
return [
'id' => $show['id'],
'title' => $show['name'],
'poster_path' => $show['poster_path'],
'date' => $show['first_air_date'],
'type' => 'show'
];
});
return [
'id' => $data['id'],
'name' => $data['name'],
'titles' => $movies->merge($shows)
->whereNotNull('poster_path')
->sortByDesc('date')
->unique(fn($item) => $item['type'].$item['id'])
->values()->all(),
];
}
}