added serialization to model factories

This commit is contained in:
pushrbx 2022-12-15 14:47:09 +00:00
parent 34e76580cd
commit 2156c6bfe1
8 changed files with 142 additions and 27 deletions

61
app/CarbonDateRange.php Normal file
View File

@ -0,0 +1,61 @@
<?php
namespace App;
use Illuminate\Support\Carbon;
use Jikan\Model\Common\DateProp;
/**
* Class representing a date range via Carbon objects.
*
* Mainly used for testing.
*/
class CarbonDateRange
{
private ?Carbon $fromObj;
private ?Carbon $untilObj;
public function __construct(?Carbon $from, ?Carbon $to)
{
$this->fromObj = $from;
$this->untilObj = $to;
}
public function __toString()
{
$result = "";
if ($this->untilObj === null && $this->fromObj !== null && $this->fromObj->day == 1 && $this->fromObj->month == 1) {
$result = "{$this->fromObj->year}";
}
else if ($this->untilObj === null && $this->fromObj !== null && $this->fromObj->day == 1) {
$result = $this->fromObj->format("M, Y");
}
else if ($this->untilObj === null && $this->fromObj !== null) {
$result = $this->fromObj->format("M d, Y");
}
else if ($this->untilObj !== null && $this->fromObj !== null) {
$result = "{$this->fromObj->format("M d, Y")} to {$this->untilObj->format("M d, Y")}";
}
return $result;
}
public function getFrom(): ?Carbon
{
return $this->fromObj;
}
public function getUntil(): ?Carbon
{
return $this->untilObj;
}
public function getFromProp(): ?DateProp
{
return DateProp::fromDateTime($this->fromObj?->toDateTimeImmutable());
}
public function getUntilProp(): ?DateProp
{
return DateProp::fromDateTime($this->untilObj?->toDateTimeImmutable());
}
}

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use App\CarbonDateRange;
use Jikan\Model\Common\DateRange;
use Jikan\Model\Common\MalUrl;
use JMS\Serializer\GraphNavigatorInterface;
@ -21,21 +22,28 @@ class SerializerFactory
GraphNavigatorInterface::DIRECTION_SERIALIZATION,
MalUrl::class,
'json',
\Closure::fromCallable('self::convertMalUrl')
self::convertMalUrl(...)
);
$registry->registerHandler(
GraphNavigatorInterface::DIRECTION_SERIALIZATION,
DateRange::class,
'json',
\Closure::fromCallable('self::convertDateRange')
self::convertDateRange(...)
);
$registry->registerHandler(
GraphNavigatorInterface::DIRECTION_SERIALIZATION,
\DateTimeImmutable::class,
'json',
\Closure::fromCallable('self::convertDateTimeImmutable')
self::convertDateTimeImmutable(...)
);
$registry->registerHandler(
GraphNavigatorInterface::DIRECTION_SERIALIZATION,
CarbonDateRange::class,
'json',
self::convertCarbonDateRange(...)
);
}
)
@ -69,6 +77,9 @@ class SerializerFactory
private static function convertDateRange($visitor, DateRange $obj, array $type): array
{
return [
// todo: update the storage method of dates from string to UTCDateTime BSON object.
// 'from' => $obj->getFrom() ? new UTCDateTime($obj->getFrom()->getTimestamp()) : null,
// 'to' => $obj->getUntil() ? new UTCDateTime($obj->getUntil()->getTimestamp()) : null,
'from' => $obj->getFrom() ? $obj->getFrom()->format(DATE_ATOM) : null,
'to' => $obj->getUntil() ? $obj->getUntil()->format(DATE_ATOM) : null,
'prop' => [
@ -87,6 +98,33 @@ class SerializerFactory
];
}
private static function convertCarbonDateRange($visitor, CarbonDateRange $obj, array $type): array
{
$from = $obj->getFrom();
$to = $obj->getUntil();
return [
// todo: update the storage method of dates from string to UTCDateTime BSON object.
// 'from' => $from !== null ? new UTCDateTime($from->getTimestamp()) : null,
// 'to' => $to !== null ? new UTCDateTime($to->getTimestamp()) : null,
'from' => $from?->toAtomString(),
'to' => $to?->toAtomString(),
'prop' => [
'from' => [
'day' => $obj->getFromProp()->getDay(),
'month' => $obj->getFromProp()->getMonth(),
'year' => $obj->getFromProp()->getYear()
],
'to' => [
'day' => $obj->getUntilProp()->getDay(),
'month' => $obj->getUntilProp()->getMonth(),
'year' => $obj->getUntilProp()->getYear()
],
],
'string' => (string)$obj,
];
}
private static function convertDateTimeImmutable($visitor, \DateTimeImmutable $obj, array $type): ?string
{
return $obj ? $obj->format(DATE_ATOM) : null;

View File

@ -1,12 +1,13 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\CarbonDateRange;
use App\Anime;
use App\Testing\JikanDataGenerator;
use MongoDB\BSON\UTCDateTime;
class AnimeFactory extends Factory
class AnimeFactory extends JikanModelFactory
{
use JikanDataGenerator;
@ -18,13 +19,12 @@ class AnimeFactory extends Factory
protected $model = Anime::class;
public function definition(): array
protected function definitionInternal(): array
{
$mal_id = $this->createMalId();
$title = $this->createTitle();
$status = $this->faker->randomElement(["Currently Airing", "Completed", "Upcoming"]);
[$aired_from, $aired_to] = $this->createActiveDateRange($status, "Currently Airing");
$test_base_url = env('APP_URL');
return [
"mal_id" => $mal_id,
@ -44,10 +44,7 @@ class AnimeFactory extends Factory
"episodes" => $this->faker->randomElement([1, 12, 13, 16, 24, 48, 96, 128, 366]),
"status" => $status,
"airing" => $status == "Currently Airing",
"aired" => [
"from" => $aired_from->toAtomString(),
"to" => $aired_to,
],
"aired" => new CarbonDateRange($aired_from, $aired_to),
"duration" => "",
"rating" => $this->faker->randomElement(["R - 17+ (violence & profanity)", "PG"]),
"score" => $this->faker->randomFloat(2, 1.00, 9.99),

View File

@ -3,18 +3,17 @@
namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Character;
use MongoDB\BSON\UTCDateTime;
class CharacterFactory extends Factory
class CharacterFactory extends JikanModelFactory
{
use JikanDataGenerator;
protected $model = Character::class;
public function definition()
protected function definitionInternal()
{
$mal_id = $this->createMalId();
$url = $this->createMalUrl($mal_id, "character");

View File

@ -1,16 +1,15 @@
<?php
namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory;
use MongoDB\BSON\UTCDateTime;
abstract class GenreFactory extends Factory
abstract class GenreFactory extends JikanModelFactory
{
use JikanDataGenerator;
protected string $mediaType = "";
public function definition(): array
protected function definitionInternal(): array
{
$mal_id = $this->createMalId();
$name = $this->getRandomGenreName();

View File

@ -0,0 +1,24 @@
<?php
namespace Database\Factories;
use JMS\Serializer\Serializer;
use \Illuminate\Database\Eloquent\Factories\Factory;
abstract class JikanModelFactory extends Factory
{
/**
* @inheritDoc
*/
public function definition()
{
/**
* @var Serializer $serializer
*/
$serializer = app("SerializerV4");
return $serializer->toArray($this->definitionInternal());
}
protected abstract function definitionInternal(): array;
}

View File

@ -1,17 +1,18 @@
<?php
namespace Database\Factories;
use App\CarbonDateRange;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Manga;
use MongoDB\BSON\UTCDateTime;
class MangaFactory extends Factory
class MangaFactory extends JikanModelFactory
{
use JikanDataGenerator;
protected $model = Manga::class;
public function definition()
protected function definitionInternal(): array
{
$mal_id = $this->createMalId();
$title = $this->createTitle();
@ -36,10 +37,7 @@ class MangaFactory extends Factory
"volumes" => $this->faker->numberBetween(0, 55),
"status" => $status,
"publishing" => $status === "Finished",
"published" => [
"from" => $published_from->toAtomString(),
"to" => $published_to
],
"published" => new CarbonDateRange($published_from, $published_to),
"score" => $this->faker->randomFloat(2, 1.00, 9.99),
"scored_by" => $this->faker->randomDigitNotNull(),
"rank" => $this->faker->randomDigitNotNull(),

View File

@ -1,12 +1,11 @@
<?php
namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Person;
use MongoDB\BSON\UTCDateTime;
class PersonFactory extends Factory
class PersonFactory extends JikanModelFactory
{
use JikanDataGenerator;
@ -17,7 +16,7 @@ class PersonFactory extends Factory
*/
protected $model = Person::class;
public function definition(): array
protected function definitionInternal(): array
{
$mal_id = $this->createMalId();
$name = $this->faker->name();