|
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': return var_export($obj, true); |
|
|
|
|
|
|
78
|
|
|
case 'NULL': return 'null'; |
|
|
|
|
|
|
79
|
|
|
case 'string': return '"' . $obj . '"'; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return rtrim(print_r($obj, true)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Applies match results to other template variables. |
|
87
|
|
|
* |
|
88
|
|
|
* @param TemplateInterface $template |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getTemplateVars(TemplateInterface $template) |
|
92
|
|
|
{ |
|
93
|
|
|
$vars = [ |
|
94
|
|
|
'expected' => $this->match->getExpected(), |
|
95
|
|
|
'actual' => $this->match->getActual(), |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
if ($tplVars = $template->getTemplateVars()) { |
|
99
|
|
|
$vars = array_merge($vars, $tplVars); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $vars; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.