bluetree-service /
simple-log
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleLog\Message; |
||
| 6 | |||
| 7 | use Psr\Log\InvalidArgumentException; |
||
| 8 | |||
| 9 | class DefaultMessage implements MessageInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $message = ''; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param string|array|object $message |
||
| 18 | * @param array $context |
||
| 19 | * @return $this |
||
| 20 | * @throws InvalidArgumentException |
||
| 21 | */ |
||
| 22 | 22 | public function createMessage($message, array $context): MessageInterface |
|
| 23 | { |
||
| 24 | 22 | $this->buildMessage($message) |
|
| 25 | 19 | ->wrapMessage() |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 26 | 19 | ->buildContext($context); |
|
| 27 | |||
| 28 | 19 | return $this; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param array $context |
||
| 33 | * @return $this |
||
| 34 | */ |
||
| 35 | 23 | protected function buildContext(array $context): MessageInterface |
|
| 36 | { |
||
| 37 | 23 | $replace = []; |
|
| 38 | |||
| 39 | 23 | foreach ($context as $key => $val) { |
|
| 40 | 4 | if (!\is_array($val) && (!\is_object($val) || \method_exists($val, '__toString'))) { |
|
| 41 | 4 | $replace['{' . $key . '}'] = $val; |
|
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | 23 | $this->message = \strtr($this->message, $replace); |
|
| 46 | |||
| 47 | 23 | return $this; |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | 14 | protected function wrapMessage(): MessageInterface |
|
| 54 | { |
||
| 55 | 14 | $this->message = $this->dateTime() |
|
| 56 | 14 | . PHP_EOL |
|
| 57 | 14 | . $this->message |
|
| 58 | 14 | . '-----------------------------------------------------------' |
|
| 59 | 14 | . PHP_EOL; |
|
| 60 | |||
| 61 | 14 | return $this; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * recurrent function to convert array into message |
||
| 66 | * |
||
| 67 | * @param string|array|object $message |
||
| 68 | * @param string $indent |
||
| 69 | * @return $this |
||
| 70 | * @throws InvalidArgumentException |
||
| 71 | */ |
||
| 72 | 22 | protected function buildMessage($message, string $indent = ''): MessageInterface |
|
| 73 | { |
||
| 74 | switch (true) { |
||
| 75 | 22 | case \is_array($message): |
|
| 76 | 6 | $this->buildArrayMessage($message, $indent); |
|
| 77 | 6 | break; |
|
| 78 | |||
| 79 | 16 | case \method_exists($message, '__toString'): |
|
| 80 | 16 | case \is_string($message): |
|
| 81 | 13 | $this->message = $message . PHP_EOL; |
|
| 82 | 13 | break; |
|
| 83 | |||
| 84 | default: |
||
| 85 | 3 | throw new InvalidArgumentException( |
|
| 86 | 3 | 'Incorrect message type. Must be string, array or object with __toString method.' |
|
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | 19 | return $this; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $message |
||
| 95 | * @param string $indent |
||
| 96 | */ |
||
| 97 | 6 | protected function buildArrayMessage(array $message, string $indent): void |
|
| 98 | { |
||
| 99 | 6 | foreach ($message as $key => $value) { |
|
| 100 | 6 | $this->processMessage($key, $value, $indent); |
|
| 101 | } |
||
| 102 | 6 | } |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param string|int $key |
||
| 106 | * @param mixed $value |
||
| 107 | * @param string $indent |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | 4 | protected function processMessage($key, $value, string $indent): MessageInterface |
|
| 111 | { |
||
| 112 | 4 | $row = '- '; |
|
| 113 | |||
| 114 | 4 | if (!\is_int($key)) { |
|
| 115 | 4 | $row .= $key . ':'; |
|
| 116 | } |
||
| 117 | |||
| 118 | 4 | if (\is_array($value)) { |
|
| 119 | 2 | $indent .= ' '; |
|
| 120 | 2 | $this->message .= $row . PHP_EOL; |
|
| 121 | 2 | $this->buildMessage($value, $indent); |
|
| 122 | } else { |
||
| 123 | 4 | $this->message .= $indent |
|
| 124 | 4 | . $row |
|
| 125 | 4 | . (!$key ? '' : ' ') |
|
| 126 | 4 | . $value |
|
| 127 | 4 | . PHP_EOL; |
|
| 128 | } |
||
| 129 | |||
| 130 | 4 | return $this; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | 4 | protected function dateTimeSplit(): array |
|
| 137 | { |
||
| 138 | 4 | $dateTime = new \DateTime(); |
|
| 139 | 4 | $date = $dateTime->format(self::DATE_FORMAT); |
|
| 140 | 4 | $time = $dateTime->format(self::TIME_FORMAT); |
|
| 141 | |||
| 142 | 4 | return [$date, $time]; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | 19 | protected function dateTime(): string |
|
| 149 | { |
||
| 150 | 19 | $dateTime = new \DateTime(); |
|
| 151 | |||
| 152 | 19 | return $dateTime->format(self::DATE_TIME_FORMAT); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 19 | public function getMessage(): string |
|
| 159 | { |
||
| 160 | 19 | return $this->message; |
|
| 161 | } |
||
| 162 | } |
||
| 163 |