1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Laminas\Log\Formatter;
use DateTime;
use function array_merge;
use function is_array;
use function is_object;
use function method_exists;
use function str_replace;
class ErrorHandler extends Simple
{
public const DEFAULT_FORMAT = '%timestamp% %priorityName% (%priority%) %message% (errno %extra[errno]%) '
. 'in %extra[file]% on line %extra[line]%';
/**
* This method formats the event for the PHP Error Handler.
*
* @param array $event
* @return string
*/
public function format($event)
{
$output = $this->format;
if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) {
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}
foreach ($this->buildReplacementsFromArray($event) as $name => $value) {
$output = str_replace("%$name%", (string) $value, $output);
}
return $output;
}
/**
* Flatten the multi-dimensional $event array into a single dimensional
* array
*
* @param array $event
* @param string $key
* @return array
*/
protected function buildReplacementsFromArray($event, $key = null)
{
$result = [];
foreach ($event as $index => $value) {
$nextIndex = $key === null ? $index : $key . '[' . $index . ']';
if ($value === null) {
continue;
}
if (! is_array($value)) {
if ($key === null) {
$result[$nextIndex] = $value;
} else {
if (! is_object($value) || method_exists($value, "__toString")) {
$result[$nextIndex] = $value;
}
}
} else {
$result = array_merge($result, $this->buildReplacementsFromArray($value, $nextIndex));
}
}
return $result;
}
}