diff options
Diffstat (limited to 'Digitigrade/Job.php')
| -rw-r--r-- | Digitigrade/Job.php | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/Digitigrade/Job.php b/Digitigrade/Job.php index 146e4b2..d460b7e 100644 --- a/Digitigrade/Job.php +++ b/Digitigrade/Job.php @@ -2,15 +2,28 @@ namespace Digitigrade; abstract class Job implements \JsonSerializable { + private static \JsonMapper $mapper; + public int $remainingTries; public int $doneTries = 0; + public static function __initStatic() { + self::$mapper = new \JsonMapper(); + // to parse DateTime(Immutable)s properly + self::$mapper->bStrictObjectTypeChecking = false; + self::$mapper->classMap[\DateTimeInterface::class] = \DateTimeImmutable::class; + } + public function jsonSerialize(): array { $array = ['jobSubtype' => static::class]; foreach ((new \ReflectionObject($this))->getProperties() as $prop) { assert($prop instanceof \ReflectionProperty); // for intellisense $value = $prop->getValue($this); - $array[$prop->getName()] = is_object($value) ? json_encode($value) : $value; + if ($value instanceof \DateTimeInterface) { + $array[$prop->getName()] = $value->format('c'); + } else { + $array[$prop->getName()] = $value; + } } return $array; } @@ -20,7 +33,7 @@ abstract class Job implements \JsonSerializable { if (!property_exists($obj, 'jobSubtype')) { return null; } - return (new \JsonMapper())->map($obj, $obj->jobSubtype); + return self::$mapper->map($obj, $obj->jobSubtype); } /** @@ -36,4 +49,6 @@ abstract class Job implements \JsonSerializable { public function submit() { JobQueue::getInstance()->submitNormal($this); } -}
\ No newline at end of file +} + +Job::__initStatic();
\ No newline at end of file |
