1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MonologFactory\Config; |
6
|
|
|
|
7
|
|
|
use Monolog\Formatter\FormatterInterface; |
8
|
|
|
|
9
|
|
|
class HandlerConfig extends ParametrizedObjectConfig |
10
|
|
|
{ |
11
|
|
|
public const PROCESSORS = 'processors'; |
12
|
|
|
public const FORMATTER = 'formatter'; |
13
|
|
|
|
14
|
|
|
/** @var callable[]|ProcessorConfig[] */ |
15
|
|
|
protected $processors; |
16
|
|
|
|
17
|
|
|
/** @var FormatterInterface|FormatterConfig|null */ |
18
|
|
|
protected $formatter; |
19
|
|
|
|
20
|
13 |
|
protected function __construct( |
21
|
|
|
string $name, |
22
|
|
|
array $parameters, |
23
|
|
|
array $processors, |
24
|
|
|
$formatter = null |
25
|
|
|
) { |
26
|
13 |
|
parent::__construct($name, $parameters); |
27
|
13 |
|
$this->processors = $processors; |
28
|
13 |
|
$this->formatter = $formatter; |
29
|
13 |
|
} |
30
|
|
|
|
31
|
14 |
|
public static function fromArray(array $config) |
32
|
|
|
{ |
33
|
14 |
|
$config = self::filter($config); |
34
|
|
|
|
35
|
13 |
|
return new static( |
36
|
13 |
|
$config[self::NAME], |
37
|
13 |
|
$config[self::PARAMETERS], |
38
|
|
|
array_map(function ($processor) { |
39
|
1 |
|
return is_array($processor) ? ProcessorConfig::fromArray($processor) : $processor; |
40
|
13 |
|
}, $config[self::PROCESSORS]), |
41
|
13 |
|
is_array($config[self::FORMATTER]) |
42
|
2 |
|
? FormatterConfig::fromArray($config[self::FORMATTER]) |
43
|
13 |
|
: $config[self::FORMATTER] |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
14 |
|
protected static function defaults(): array |
48
|
|
|
{ |
49
|
14 |
|
return array_merge(parent::defaults(), [ |
50
|
14 |
|
self::PROCESSORS => [], |
51
|
14 |
|
self::FORMATTER => null, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
14 |
|
protected static function validate(array $config): void |
56
|
|
|
{ |
57
|
14 |
|
parent::validate($config); |
58
|
14 |
|
ConfigAssertion::isArray($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array"); |
59
|
14 |
|
ConfigAssertion::allIsArrayOrCallable($config[self::PROCESSORS], "'" . self::PROCESSORS . "' must be an array of callables or configuration arrays"); |
60
|
14 |
|
ConfigAssertion::nullOrIsArrayOrInstanceOf($config[self::FORMATTER], FormatterInterface::class, "'" . self::FORMATTER . "' must be Formatter instance or configuration array"); |
61
|
13 |
|
} |
62
|
|
|
|
63
|
13 |
|
public function getProcessors(): array |
64
|
|
|
{ |
65
|
13 |
|
return $this->processors; |
66
|
|
|
} |
67
|
|
|
|
68
|
13 |
|
public function getFormatter() |
69
|
|
|
{ |
70
|
13 |
|
return $this->formatter; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|