123movies-seo/app/Supports/SchemaBuilder.php

322 lines
15 KiB
PHP

<?php
namespace App\Supports;
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;
use Spatie\SchemaOrg\Schema;
use Symfony\Component\VarExporter\Internal\Values;
class SchemaBuilder
{
// use Helpers;
public static function getMovieSchema(array $movie): array
{
$schema = [];
$movie = (object)$movie;
$movieSchema = Schema::movie()
->name($movie->title)
->description($movie->title . ' - ' . $movie->overview)
->url(route('movie', ['id' => $movie->id, 'slug' => $movie->slug]))
->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
->datePublished(Carbon::parse($movie->release_date)->format('Y-m-d'))
->genre(collect($movie->genres)->pluck('name')->all())
->image($movie->poster);
if (isset($movie->vote_average) && $movie->vote_count) {
$rating = Schema::aggregateRating()
->bestRating(10)
->worstRating(1)
->ratingValue($movie->vote_average)
->ratingCount($movie->vote_count)
->ratingExplanation('Community Rating');
$movieSchema->aggregateRating($rating);
}
$actors = collect($movie->cast)->take(5)->map(function ($actor) {
return Schema::person()->name($actor['name'])->image($actor['image']);
})->values()->all();
$movieSchema->actors($actors);
$directors = collect($movie->crew['directors'])->filter(fn ($director) => Str::of($director['as'])->contains('director', true))->map(function ($director) {
// dd($director);
return Schema::person()->name($director['name'])->image($director['image']);
})->values()->all();
$movieSchema->directors($directors);
$schema[] = json_encode($movieSchema->toArray(), JSON_UNESCAPED_UNICODE);
// if (isset($movie->trailer)) {
// $videoSchema = Schema::videoObject()
// ->name($movie->title)
// ->description($movie->overview)
// ->thumbnailUrl($movie->poster)
// ->uploadDate(Carbon::parse($movie->release_date)->toIso8601String())
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
// ->embedUrl($movie->trailer);
// $schema[] = json_encode($videoSchema->toArray(), JSON_UNESCAPED_UNICODE);
// }
if (isset($movie->similar) && !empty($movie->similar)) {
$carousel = Schema::itemList();
foreach ($movie->similar as $key => $similarMovie) {
// dd($similarMovie);
if (!isset($similarMovie['slug']) || $similarMovie['slug'] === '') continue;
$listItems[] = Schema::listItem()
->position($key + 1)
->item(
Schema::movie()
->url(route('movie', ['id' => $movie->id, 'slug' => $similarMovie['slug']]))
->name($similarMovie['title'])
->image($similarMovie['image'])
->datePublished($similarMovie['date'])
);
}
$carousel->itemListElement(
$listItems
);
$schema[] = json_encode($carousel->toArray(), JSON_UNESCAPED_UNICODE);
}
return $schema;
}
public static function getShowSchema(array $movie): array
{
$schema = [];
$movie = (object)$movie;
$movieSchema = Schema::tVSeries()
->name($movie->title)
->description($movie->title . ' - ' . $movie->overview)
->url(route('show', ['id' => $movie->id, 'slug' => $movie->slug]))
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
->datePublished(Carbon::parse($movie->first_air_date)->format('Y-m-d'))
->numberOfEpisodes($movie->number_of_episodes)
->numberOfSeasons($movie->number_of_seasons)
->genre(collect($movie->genres)->pluck('name')->all())
->image($movie->poster);
if (isset($movie->vote_average) && $movie->vote_count) {
$rating = Schema::aggregateRating()
->bestRating(10)
->worstRating(1)
->ratingValue($movie->vote_average)
->ratingCount($movie->vote_count)
->ratingExplanation('Community Rating');
$movieSchema->aggregateRating($rating);
}
$actors = collect($movie->cast)->take(5)->map(function ($actor) {
return Schema::person()->name($actor['name'])->image($actor['image']);
})->values()->all();
$movieSchema->actor($actors);
$directors = collect($movie->crew['producers'])->filter(fn ($director) => Str::of($director['as'])->contains('producer', true))->map(function ($director) {
return Schema::person()->name($director['name'])->image($director['image']);
})->values()->all();
$movieSchema->producer($directors);
$schema[] = json_encode($movieSchema->toArray(), JSON_UNESCAPED_UNICODE);
// if (isset($movie->trailer)) {
// $videoSchema = Schema::videoObject()
// ->name($movie->title)
// ->description($movie->overview)
// ->thumbnailUrl($movie->poster)
// ->uploadDate(Carbon::parse($movie->first_air_date)->toIso8601String())
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
// ->embedUrl($movie->trailer);
// $schema[] = json_encode($videoSchema->toArray(), JSON_UNESCAPED_UNICODE);
// }
if (isset($movie->similar) && !empty($movie->similar)) {
$carousel = Schema::itemList();
$listItems = [];
foreach ($movie->similar as $key => $similarMovie) {
// dd($similarMovie);
$listItems[] = Schema::listItem()
->position($key + 1)
->item(
Schema::tVSeries()
->url(route('show', ['id' => $movie->id, 'slug' => $similarMovie['slug']]))
->name($similarMovie['title'])
->image($similarMovie['image'])
->datePublished($similarMovie['date'])
);
}
$carousel->itemListElement(
$listItems
);
$schema[] = json_encode($carousel->toArray(), JSON_UNESCAPED_UNICODE);
}
return $schema;
}
public static function getShowSeasonSchema($show, $season): array
{
$schema = [];
$movie = (object)$show;
$movieSchema = Schema::tVSeason()
->name($season['name'])
->description($season['overview'])
->url(route('show.season', ['id' => $movie->id, 'slug' => $movie->slug, 'season' => $season['season_number']]))
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
->datePublished(Carbon::parse($season['air_date'])->format('Y-m-d'))
->numberOfEpisodes($movie->number_of_episodes)
->genre(collect($movie->genres)->pluck('name')->all())
->image($movie->poster)
->seasonNumber($season['season_number'])
->partOfTVSeries(Schema::tVSeries()->name($movie->title)->url((route('show', ['id' => $movie->id, 'slug' => $movie->slug]))));
if (isset($movie->vote_average) && $movie->vote_count) {
$rating = Schema::aggregateRating()
->bestRating(10)
->worstRating(1)
->ratingValue($movie->vote_average)
->ratingCount($movie->vote_count)
->ratingExplanation('Community Rating');
$movieSchema->aggregateRating($rating);
}
if (!empty($season['episodes'])) {
$showEpisodes = [];
array_map(function ($ep) use (&$showEpisodes) {
$showEpisodes[] = Schema::episode()
->name($ep['name'])
->description($ep['overview'])
->episodeNumber($ep['episode_number']);
}, $season['episodes']->all());
$movieSchema->episode($showEpisodes);
}
$actors = collect($season['cast'])->take(5)->map(function ($actor) {
return Schema::person()->name($actor['name'])->image($actor['image']);
})->values()->all();
$movieSchema->actor($actors);
$directors = collect($season['crew'])->filter(fn ($director) => Str::of($director['as'])->contains('producer', true))->map(function ($director) {
return Schema::person()->name($director['name'])->image($director['image']);
})->values()->all();
$movieSchema->producer($directors);
$schema[] = json_encode($movieSchema->toArray(), JSON_UNESCAPED_UNICODE);
// if (isset($movie->trailer)) {
// $videoSchema = Schema::videoObject()
// ->name($movie->title)
// ->description($movie->overview)
// ->thumbnailUrl($movie->poster)
// ->uploadDate(Carbon::parse($movie->first_air_date)->toIso8601String())
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
// ->embedUrl($movie->trailer);
// $schema[] = json_encode($videoSchema->toArray(), JSON_UNESCAPED_UNICODE);
// }
if (isset($movie->similar) && !empty($movie->similar)) {
$carousel = Schema::itemList();
$listItems = [];
foreach ($movie->similar as $key => $similarMovie) {
// dd($similarMovie);
$listItems[] = Schema::listItem()
->position($key + 1)
->item(
Schema::tVSeries()
->url(route('show', ['id' => $movie->id, 'slug' => $similarMovie['slug']]))
->name($similarMovie['title'])
->image($similarMovie['image'])
->datePublished($similarMovie['date'])
);
}
$carousel->itemListElement(
$listItems
);
$schema[] = json_encode($carousel->toArray(), JSON_UNESCAPED_UNICODE);
}
return $schema;
}
public static function getShowEpisodeSchema($show, $season, $episode): array
{
$schema = [];
$movie = (object)$show;
$movieSchema = Schema::tVEpisode()
->name($episode['name'])
->description($episode['overview'])
->url(route('show.episode', ['id' => $movie->id, 'slug' => $movie->slug, 'season' => $episode['season_number'], 'episode' => $episode['episode_number']]))
->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
->datePublished(Carbon::parse($episode['air_date'])->format('Y-m-d'))
// ->numberOfEpisodes($movie->number_of_episodes)
->genre(collect($movie->genres)->pluck('name')->all())
->image($episode['backdrop'])
->episodeNumber($episode['episode_number'])
->partOfSeason(
Schema::tVSeason()
->name($season['name'])
->seasonNumber($episode['season_number'])
->url(route('show.season', ['id' => $movie->id, 'slug' => $movie->slug, 'season' => $season['season_number']]))
)
->partOfTVSeries(Schema::tVSeries()->name($movie->title)->url((route('show', ['id' => $movie->id, 'slug' => $movie->slug]))));
if (isset($episode['vote_average']) && $episode['vote_count']) {
$rating = Schema::aggregateRating()
->bestRating(10)
->worstRating(1)
->ratingValue($episode['vote_average'])
->ratingCount($episode['vote_count'])
->ratingExplanation('Community Rating');
$movieSchema->aggregateRating($rating);
}
// if (!empty($season['episodes'])) {
// $showEpisodes = [];
// array_map(function($ep) use(&$showEpisodes){
// $showEpisodes[] = Schema::episode()
// ->name($ep['name'])
// ->description($ep['overview'])
// ->episodeNumber($ep['episode_number']);
// },$season['episodes']->all());
// $movieSchema->episode($showEpisodes);
// }
// $actors = collect($episode['cast'])->take(5)->map(function($actor){
// return Schema::person()->name($actor['name'])->image($actor['image']);
// })->values()->all();
// $movieSchema->actors($actors);
// $directors = collect($episode['crew'])->filter(fn($director) => Str::of($director['as'])->contains('producer', true))->map(function($director){
// return Schema::person()->name($director['name'])->image($director['image']);
// })->values()->all();
// $movieSchema->producers($directors);
$schema[] = json_encode($movieSchema->toArray(), JSON_UNESCAPED_UNICODE);
// if (isset($episode['trailer'])) {
// $videoSchema = Schema::videoObject()
// ->name($movie->title)
// ->description($movie->overview)
// ->thumbnailUrl($movie->poster)
// ->uploadDate(Carbon::parse($movie->first_air_date)->toIso8601String())
// ->duration('PT' . Str::of($movie->runtime)->replace(' ', '')->upper())
// ->embedUrl($movie->trailer);
// $schema[] = json_encode($videoSchema->toArray(), JSON_UNESCAPED_UNICODE);
// }
if (isset($movie->similar) && !empty($movie->similar)) {
$carousel = Schema::itemList();
$listItems = [];
foreach ($movie->similar as $key => $similarMovie) {
// dd($similarMovie);
$listItems[] = Schema::listItem()
->position($key + 1)
->item(
Schema::tVSeries()
->url(route('show', ['id' => $movie->id, 'slug' => $similarMovie['slug']]))
->name($similarMovie['title'])
->image($similarMovie['image'])
->datePublished($similarMovie['date'])
);
}
$carousel->itemListElement(
$listItems
);
$schema[] = json_encode($carousel->toArray(), JSON_UNESCAPED_UNICODE);
}
return $schema;
}
}