1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Consolidation\OutputFormatters\Formatters;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Symfony\Component\Console\Output\OutputInterface;
use Consolidation\OutputFormatters\StructuredData\MetadataInterface;
trait MetadataFormatterTrait
{
/**
* @inheritdoc
*/
public function writeMetadata(OutputInterface $output, $structuredOutput, FormatterOptions $options)
{
$template = $options->get(FormatterOptions::METADATA_TEMPLATE);
if (!$template) {
return;
}
if (!$structuredOutput instanceof MetadataInterface) {
return;
}
$metadata = $structuredOutput->getMetadata();
if (empty($metadata)) {
return;
}
$message = $this->interpolate($template, $metadata);
return $output->writeln($message);
}
/**
* Interpolates context values into the message placeholders.
*
* @author PHP Framework Interoperability Group
*
* @param string $message
* @param array $context
*
* @return string
*/
private function interpolate($message, array $context)
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace[sprintf('{%s}', $key)] = $val;
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
}