| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Console\Format; |
| 4: | |
| 5: | use Salient\Contract\Console\Format\MessageFormatInterface as Format; |
| 6: | use Salient\Contract\Core\Immutable; |
| 7: | use Salient\Core\Concern\ImmutableTrait; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class MessageFormats implements Immutable |
| 13: | { |
| 14: | use ImmutableTrait; |
| 15: | |
| 16: | |
| 17: | private array $Formats = []; |
| 18: | private NullMessageFormat $FallbackFormat; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | public function __construct() |
| 24: | { |
| 25: | $this->FallbackFormat = new NullMessageFormat(); |
| 26: | } |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public function withFormat($level, $type, Format $format) |
| 37: | { |
| 38: | $formats = $this->Formats; |
| 39: | $levels = (array) $level; |
| 40: | $types = (array) $type; |
| 41: | foreach ($levels as $level) { |
| 42: | foreach ($types as $type) { |
| 43: | $formats[$level][$type] = $format; |
| 44: | } |
| 45: | } |
| 46: | return $this->with('Formats', $formats); |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | public function getFormat(int $level, int $type): Format |
| 56: | { |
| 57: | return $this->Formats[$level][$type] ?? $this->FallbackFormat; |
| 58: | } |
| 59: | } |
| 60: | |