| Total Complexity | 13 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | trait AttachTrait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * 属性数组 |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $attribute=[]; |
||
| 15 | |||
| 16 | public function addAttribute(string $name , $value) |
||
| 17 | { |
||
| 18 | $this->attribute[$name] = $value; |
||
| 19 | } |
||
| 20 | |||
| 21 | protected function analyse(array $context) |
||
| 22 | { |
||
| 23 | $replace = []; |
||
| 24 | $attach =[] ; |
||
| 25 | foreach ($context as $key => $val) { |
||
| 26 | if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString')) && ! $val instanceof \Exception) { |
||
| 27 | $replace['{' . $key . '}'] = $val; |
||
| 28 | } else { |
||
| 29 | $attach[$key] = $val; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | return [$attach, $replace]; |
||
| 33 | } |
||
| 34 | |||
| 35 | public function interpolate(string $message, array $context, array $attribute) |
||
| 36 | { |
||
| 37 | list($attach, $replace) = $this->analyse($context); |
||
| 38 | $attribute = array_merge($this->attribute, $attribute); |
||
| 39 | foreach ($attribute as $key => $val) { |
||
| 40 | $replace['%' . $key . '%'] = $val; |
||
| 41 | } |
||
| 42 | $message = strtr($message, $replace); |
||
| 43 | $attachInfo = ''; |
||
| 44 | foreach ($attach as $name => $value) { |
||
| 45 | $attachInfo = $name.' = '; |
||
| 46 | if ($value instanceof AttachValueInterface) { |
||
| 47 | $attachInfo.= $value->getLogAttach().PHP_EOL; |
||
| 48 | } else { |
||
| 49 | $attachInfo.= DumpTrait::parameterToString($value).PHP_EOL; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | if (strlen($attachInfo) > 0) { |
||
| 53 | return $message.PHP_EOL.$attachInfo; |
||
| 54 | } |
||
| 55 | return $message; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get 属性数组 |
||
| 60 | * |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function getAttribute() |
||
| 66 | } |
||
| 67 | } |
||
| 68 |