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