From 2156c6bfe120534252488ffbb391b9c3f20a5344 Mon Sep 17 00:00:00 2001 From: pushrbx Date: Thu, 15 Dec 2022 14:47:09 +0000 Subject: [PATCH] added serialization to model factories --- app/CarbonDateRange.php | 61 ++++++++++++++++++++++++ app/Providers/SerializerFactory.php | 44 +++++++++++++++-- database/factories/AnimeFactory.php | 13 ++--- database/factories/CharacterFactory.php | 5 +- database/factories/GenreFactory.php | 5 +- database/factories/JikanModelFactory.php | 24 ++++++++++ database/factories/MangaFactory.php | 12 ++--- database/factories/PersonFactory.php | 5 +- 8 files changed, 142 insertions(+), 27 deletions(-) create mode 100644 app/CarbonDateRange.php create mode 100644 database/factories/JikanModelFactory.php diff --git a/app/CarbonDateRange.php b/app/CarbonDateRange.php new file mode 100644 index 0000000..71cb63c --- /dev/null +++ b/app/CarbonDateRange.php @@ -0,0 +1,61 @@ +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()); + } +} diff --git a/app/Providers/SerializerFactory.php b/app/Providers/SerializerFactory.php index a7842f0..558eaa1 100644 --- a/app/Providers/SerializerFactory.php +++ b/app/Providers/SerializerFactory.php @@ -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; diff --git a/database/factories/AnimeFactory.php b/database/factories/AnimeFactory.php index e163547..404f903 100644 --- a/database/factories/AnimeFactory.php +++ b/database/factories/AnimeFactory.php @@ -1,12 +1,13 @@ 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), diff --git a/database/factories/CharacterFactory.php b/database/factories/CharacterFactory.php index 7f3ff95..ecc5b0e 100644 --- a/database/factories/CharacterFactory.php +++ b/database/factories/CharacterFactory.php @@ -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"); diff --git a/database/factories/GenreFactory.php b/database/factories/GenreFactory.php index 5f0141e..e893ee3 100644 --- a/database/factories/GenreFactory.php +++ b/database/factories/GenreFactory.php @@ -1,16 +1,15 @@ createMalId(); $name = $this->getRandomGenreName(); diff --git a/database/factories/JikanModelFactory.php b/database/factories/JikanModelFactory.php new file mode 100644 index 0000000..7992224 --- /dev/null +++ b/database/factories/JikanModelFactory.php @@ -0,0 +1,24 @@ +toArray($this->definitionInternal()); + } + + protected abstract function definitionInternal(): array; +} diff --git a/database/factories/MangaFactory.php b/database/factories/MangaFactory.php index 04e97b2..0cc67b7 100644 --- a/database/factories/MangaFactory.php +++ b/database/factories/MangaFactory.php @@ -1,17 +1,18 @@ 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(), diff --git a/database/factories/PersonFactory.php b/database/factories/PersonFactory.php index 65b926f..35409bb 100644 --- a/database/factories/PersonFactory.php +++ b/database/factories/PersonFactory.php @@ -1,12 +1,11 @@ createMalId(); $name = $this->faker->name();