ScalarFormatter.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  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. public function format(array $record)
  23. {
  24. foreach ($record as $key => $value) {
  25. $record[$key] = $this->normalizeValue($value);
  26. }
  27. return $record;
  28. }
  29. /**
  30. * @param mixed $value
  31. * @return mixed
  32. */
  33. protected function normalizeValue($value)
  34. {
  35. $normalized = $this->normalize($value);
  36. if (is_array($normalized) || is_object($normalized)) {
  37. return $this->toJson($normalized, true);
  38. }
  39. return $normalized;
  40. }
  41. }