1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Peridot\Leo\Formatter; |
4
|
|
|
|
5
|
|
|
use Peridot\Leo\Matcher\Match; |
6
|
|
|
use Peridot\Leo\Matcher\Template\TemplateInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Formatter is the core FormatterInterface for Leo. |
10
|
|
|
* |
11
|
|
|
* @package Peridot\Leo\Formatter |
12
|
|
|
*/ |
13
|
|
|
class Formatter implements FormatterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Match |
17
|
|
|
*/ |
18
|
|
|
protected $match; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* |
27
|
|
|
* @return Match |
28
|
|
|
*/ |
29
|
|
|
public function getMatch() |
30
|
|
|
{ |
31
|
|
|
return $this->match; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @param Match $match |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function setMatch(Match $match) |
41
|
|
|
{ |
42
|
|
|
$this->match = $match; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* |
50
|
|
|
* @param TemplateInterface $template |
51
|
|
|
* @return mixed|string |
52
|
|
|
*/ |
53
|
|
|
public function getMessage(TemplateInterface $template) |
54
|
|
|
{ |
55
|
|
|
$vars = $this->getTemplateVars($template); |
56
|
|
|
|
57
|
|
|
$tpl = $this->match->isNegated() |
58
|
|
|
? $template->getNegatedTemplate() |
59
|
|
|
: $template->getDefaultTemplate(); |
60
|
|
|
|
61
|
|
|
foreach ($vars as $name => $value) { |
62
|
|
|
$tpl = str_replace('{{' . $name . '}}', $this->objectToString($value), $tpl); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $tpl; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
* |
71
|
|
|
* @param mixed $obj |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function objectToString($obj) |
75
|
|
|
{ |
76
|
|
|
switch (gettype($obj)) { |
77
|
|
|
case 'boolean': |
78
|
|
|
return var_export($obj, true); |
79
|
|
|
|
80
|
|
|
case 'NULL': |
81
|
|
|
return 'null'; |
82
|
|
|
|
83
|
|
|
case 'string': |
84
|
|
|
return '"' . $obj . '"'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return rtrim(print_r($obj, true)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Applies match results to other template variables. |
92
|
|
|
* |
93
|
|
|
* @param TemplateInterface $template |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getTemplateVars(TemplateInterface $template) |
97
|
|
|
{ |
98
|
|
|
$vars = [ |
99
|
|
|
'expected' => $this->match->getExpected(), |
100
|
|
|
'actual' => $this->match->getActual(), |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
if ($tplVars = $template->getTemplateVars()) { |
104
|
|
|
$vars = array_merge($vars, $tplVars); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $vars; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|