2022-09-15 07:54:28 -05:00
|
|
|
<?php
|
|
|
|
|
|
2023-10-08 23:30:41 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-09-15 07:54:28 -05:00
|
|
|
namespace App\Data\DataPipes;
|
|
|
|
|
|
2023-10-08 23:30:41 -05:00
|
|
|
use App\Data\Enums\Type;
|
2022-09-15 07:54:28 -05:00
|
|
|
use App\Data\FileData;
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
use Spatie\LaravelData\DataPipes\DataPipe;
|
|
|
|
|
use Spatie\LaravelData\Lazy;
|
|
|
|
|
use Spatie\LaravelData\Optional;
|
|
|
|
|
use Spatie\LaravelData\Support\DataClass;
|
|
|
|
|
use Spatie\LaravelData\Support\DataConfig;
|
|
|
|
|
use Spatie\LaravelData\Support\DataProperty;
|
|
|
|
|
|
|
|
|
|
class StructureData implements DataPipe
|
|
|
|
|
{
|
|
|
|
|
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
|
|
|
|
|
{
|
|
|
|
|
if (!$properties->has('rawData') || !count($properties->get('rawData'))) {
|
|
|
|
|
return $properties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [];
|
2023-10-08 23:30:41 -05:00
|
|
|
$films = [];
|
2022-09-15 07:54:28 -05:00
|
|
|
foreach ($properties->get('rawData') as $watch) {
|
2023-10-08 23:30:41 -05:00
|
|
|
switch ($watch['type']) {
|
|
|
|
|
case (Type::Episode->value):
|
|
|
|
|
$data = $this->getEp($data, $watch);
|
|
|
|
|
break;
|
|
|
|
|
case (Type::Movie->value):
|
|
|
|
|
$films[] = $this->getFilm($watch);
|
|
|
|
|
break;
|
2022-09-15 07:54:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$flat = [];
|
|
|
|
|
foreach ($data as $show) {
|
|
|
|
|
$show['seasons'] = array_values($show['seasons']);
|
|
|
|
|
$flat[] = $show;
|
|
|
|
|
}
|
2023-10-08 23:30:41 -05:00
|
|
|
$properties['structuredData'] = ['shows' => $flat, 'movies' => $films];
|
2022-09-15 07:54:28 -05:00
|
|
|
|
|
|
|
|
return $properties;
|
|
|
|
|
}
|
2023-10-08 23:30:41 -05:00
|
|
|
|
|
|
|
|
protected function getEp(array $data, array $watch): array
|
|
|
|
|
{
|
|
|
|
|
$showTmdb = $watch['showTmdb'];
|
|
|
|
|
$season = $watch['season'];
|
|
|
|
|
$episode = $watch['episode'];
|
|
|
|
|
$watched = $watch['watched'];
|
|
|
|
|
if (!array_key_exists($showTmdb, $data)) {
|
|
|
|
|
$data[$showTmdb] = [];
|
|
|
|
|
$data[$showTmdb]['ids'] = ['tmdb' => $showTmdb];
|
|
|
|
|
$data[$showTmdb]['seasons'] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!array_key_exists($season, $data[$showTmdb]['seasons'])) {
|
|
|
|
|
$data[$showTmdb]['seasons'][$season] = [];
|
|
|
|
|
$data[$showTmdb]['seasons'][$season]['number'] = $season;
|
|
|
|
|
$data[$showTmdb]['seasons'][$season]['episodes'] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data[$showTmdb]['seasons'][$season]['episodes'][] = [
|
|
|
|
|
'number' => $episode,
|
|
|
|
|
'watched_at' => $watched,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFilm(array $watch): array
|
|
|
|
|
{
|
|
|
|
|
$filmTmdb = $watch['movieTmdb'];
|
|
|
|
|
$filmYear = $watch['movieYear'];
|
|
|
|
|
$title = $watch['showTitle'];
|
|
|
|
|
$watched = $watch['watched'];
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'watched_at' => $watched,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
'year' => $filmYear,
|
|
|
|
|
'ids' => [
|
|
|
|
|
'tmdb' => $filmTmdb,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2022-09-15 07:54:28 -05:00
|
|
|
}
|