ScalarFormatter.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. use Monolog\LogRecord;
  12. /**
  13. * Formats data into an associative array of scalar (+ null) values.
  14. * Objects and arrays will be JSON encoded.
  15. *
  16. * @author Andrew Lawson <adlawson@gmail.com>
  17. */
  18. class ScalarFormatter extends NormalizerFormatter
  19. {
  20. /**
  21. * @inheritDoc
  22. *
  23. * @phpstan-return array<string, scalar|null> $record
  24. */
  25. public function format(LogRecord $record): array
  26. {
  27. $result = [];
  28. foreach ($record->toArray() as $key => $value) {
  29. $result[$key] = $this->toScalar($value);
  30. }
  31. return $result;
  32. }
  33. protected function toScalar(mixed $value): string|int|float|bool|null
  34. {
  35. $normalized = $this->normalize($value);
  36. if (is_array($normalized)) {
  37. return $this->toJson($normalized, true);
  38. }
  39. return $normalized;
  40. }
  41. }