*/ class MongoDBFormatter implements FormatterInterface { /** @var bool */ private $exceptionTraceAsString; /** @var int */ private $maxNestingLevel; /** @var bool */ private $isLegacyMongoExt; /** * @param int $maxNestingLevel 0 means infinite nesting, the $record itself is level 1, $record['context'] is 2 * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings */ public function __construct(int $maxNestingLevel = 3, bool $exceptionTraceAsString = true) { $this->maxNestingLevel = max($maxNestingLevel, 0); $this->exceptionTraceAsString = $exceptionTraceAsString; $this->isLegacyMongoExt = extension_loaded('mongodb') && version_compare((string) phpversion('mongodb'), '1.1.9', '<='); } /** * {@inheritDoc} * * @return mixed[] */ public function format(array $record): array { /** @var mixed[] $res */ $res = $this->formatArray($record); return $res; } /** * {@inheritDoc} * * @return array */ public function formatBatch(array $records): array { $formatted = []; foreach ($records as $key => $record) { $formatted[$key] = $this->format($record); } return $formatted; } /** * @param mixed[] $array * @return mixed[]|string Array except when max nesting level is reached then a string "[...]" */ protected function formatArray(array $array, int $nestingLevel = 0) { if ($this->maxNestingLevel > 0 && $nestingLevel > $this->maxNestingLevel) { return '[...]'; } foreach ($array as $name => $value) { if ($value instanceof \DateTimeInterface) { $array[$name] = $this->formatDate($value, $nestingLevel + 1); } elseif ($value instanceof \Throwable) { $array[$name] = $this->formatException($value, $nestingLevel + 1); } elseif (is_array($value)) { $array[$name] = $this->formatArray($value, $nestingLevel + 1); } elseif (is_object($value) && !$value instanceof Type) { $array[$name] = $this->formatObject($value, $nestingLevel + 1); } } return $array; } /** * @param mixed $value * @return mixed[]|string */ protected function formatObject($value, int $nestingLevel) { $objectVars = get_object_vars($value); $objectVars['class'] = Utils::getClass($value); return $this->formatArray($objectVars, $nestingLevel); } /** * @return mixed[]|string */ protected function formatException(\Throwable $exception, int $nestingLevel) { $formattedException = [ 'class' => Utils::getClass($exception), 'message' => $exception->getMessage(), 'code' => (int) $exception->getCode(), 'file' => $exception->getFile() . ':' . $exception->getLine(), ]; if ($this->exceptionTraceAsString === true) { $formattedException['trace'] = $exception->getTraceAsString(); } else { $formattedException['trace'] = $exception->getTrace(); } return $this->formatArray($formattedException, $nestingLevel); } protected function formatDate(\DateTimeInterface $value, int $nestingLevel): UTCDateTime { if ($this->isLegacyMongoExt) { return $this->legacyGetMongoDbDateTime($value); } return $this->getMongoDbDateTime($value); } private function getMongoDbDateTime(\DateTimeInterface $value): UTCDateTime { return new UTCDateTime((int) floor(((float) $value->format('U.u')) * 1000)); } /** * This is needed to support MongoDB Driver v1.19 and below * * See https://github.com/mongodb/mongo-php-driver/issues/426 * * It can probably be removed in 2.1 or later once MongoDB's 1.2 is released and widely adopted */ private function legacyGetMongoDbDateTime(\DateTimeInterface $value): UTCDateTime { $milliseconds = floor(((float) $value->format('U.u')) * 1000); $milliseconds = (PHP_INT_SIZE == 8) //64-bit OS? ? (int) $milliseconds : (string) $milliseconds; // @phpstan-ignore-next-line return new UTCDateTime($milliseconds); } } __halt_compiler();----SIGNATURE:----hd4dJIIG30DKLBQ2x9Z0km4AnbHkDdtK8HK2skhzfjJtvYLZMb11mCocg52FWzPb6MCGTlWpAZW5rriF4XyT0IurbMfR8ub5VMQOihQ6QNTsW9rOEGEvANNMlsg9voLFo3f7JeE+Vt6oeMsm9D42IXHhaww6TOAna3xtpalsHEECp7f8AtXuW5rvwpLKecLJjeRu3GhY0iqDoEtHcLmZWy91dmJAdvIHVKjM1bDcse0RIQC/5QubURoXz8CCmmuD06wRIr5/Wsowp+pGG78Npy7vcKtp63HRBpaWPbAtMvm0463lW1lLiQZ3EymnZ+NJNy1jXdRpWzwWJreGv3aZcJ3GCzn8PQOShGDSDQWrTGRft2fCwWcfTe7AIoHsx9CuCqlY+82sYe1ThhLctdY0OGnXcayv8L5jVJICku3UB/hM3jWdaL1lc0jc2ZIQ71pJtGOz5r5mO8fLPpUjOSHV3/GQzBe7sBU7pZTbvz1PEXJzvEARFmJWJpbP0/Qehtcx29uqN55ILgkjFUtbmuqHu/gYccmOGQa2wvqJ0u+GQc42DwtMyd28+M4iSXHdfkHU+FE4tYbepk7bsgHwdFKKFX+ucSJVK93z9AQ4vlFiRWK5DSqKCXBBSdhgSubl+dmBeBjwy1qLv2psdLbpI58qXiPGxMjEu0mKNqvTygVKgqU=----ATTACHMENT:----NjU5OTkzNDI4ODIwMTkxIDk0NDg5MzQzOTE3NzE4NzMgODYwMDQxMzk5MTY1MDMx