ScalarFormatter.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. /**
  12. * Formats data into an associative array of scalar values.
  13. * Objects and arrays will be JSON encoded.
  14. *
  15. * @author Andrew Lawson <adlawson@gmail.com>
  16. */
  17. class ScalarFormatter extends NormalizerFormatter
  18. {
  19. /**
  20. * {@inheritdoc}
  21. *
  22. * @phpstan-return scalar[] $record
  23. */
  24. public function format(array $record): array
  25. {
  26. foreach ($record as $key => $value) {
  27. $record[$key] = $this->normalizeValue($value);
  28. }
  29. return $record;
  30. }
  31. /**
  32. * @param mixed $value
  33. * @return string|int|bool|null
  34. */
  35. protected function normalizeValue($value)
  36. {
  37. $normalized = $this->normalize($value);
  38. if (is_array($normalized)) {
  39. return $this->toJson($normalized, true);
  40. }
  41. return $normalized;
  42. }
  43. }