1: <?php declare(strict_types=1);
2:
3: namespace Salient\Console\Support;
4:
5: use Salient\Console\Support\ConsoleMessageFormat as MessageFormat;
6: use Salient\Contract\Console\ConsoleMessageType as MessageType;
7: use Salient\Contract\Core\MessageLevel as Level;
8:
9: /**
10: * Maps message levels and types to formats
11: *
12: * If multiple formats are assigned to the same {@see Level} and
13: * {@see MessageType}, the format assigned last takes precedence.
14: */
15: final class ConsoleMessageFormats
16: {
17: /** @var array<Level::*,array<MessageType::*,MessageFormat>> */
18: private array $Formats = [];
19: private MessageFormat $FallbackFormat;
20:
21: public function __construct(?MessageFormat $fallbackFormat = null)
22: {
23: $this->FallbackFormat = $fallbackFormat
24: ?: MessageFormat::getDefaultMessageFormat();
25: }
26:
27: /**
28: * Assign a format to one or more message levels and types
29: *
30: * @param array<Level::*>|Level::* $level
31: * @param array<MessageType::*>|MessageType::* $type
32: * @return $this
33: */
34: public function set($level, $type, MessageFormat $format)
35: {
36: foreach ((array) $level as $level) {
37: foreach ((array) $type as $_type) {
38: $this->Formats[$level][$_type] = $format;
39: }
40: }
41:
42: return $this;
43: }
44:
45: /**
46: * Get the format assigned to a message level and type
47: *
48: * @param Level::* $level
49: * @param MessageType::* $type
50: */
51: public function get($level, $type): MessageFormat
52: {
53: return $this->Formats[$level][$type] ?? $this->FallbackFormat;
54: }
55: }
56: