1: <?php
2: namespace Peridot\Leo\Responder;
3:
4: use Exception;
5: use Peridot\Leo\Formatter\FormatterInterface;
6: use Peridot\Leo\Matcher\Match;
7: use Peridot\Leo\Matcher\Template\TemplateInterface;
8:
9: /**
10: * The ExceptionResponder responds to a match by throwing an exception
11: * on a failed match.
12: *
13: * @package Peridot\Leo\Responder
14: */
15: class ExceptionResponder implements ResponderInterface
16: {
17: /**
18: * @var FormatterInterface
19: */
20: protected $formatter;
21:
22: /**
23: * @param FormatterInterface $formatter
24: */
25: public function __construct(FormatterInterface $formatter)
26: {
27: $this->formatter = $formatter;
28: }
29:
30: /**
31: * {@inheritdoc}
32: *
33: * Throws an exception containing the formatted message.
34: *
35: * @param Match $match
36: * @param TemplateInterface $template
37: * @param string $message
38: * @return void
39: * @throws Exception
40: */
41: public function respond(Match $match, TemplateInterface $template, $message = "")
42: {
43: if ($match->isMatch()) {
44: return;
45: }
46:
47: $this->formatter->setMatch($match);
48: $message = ($message) ? $message : $this->formatter->getMessage($template);
49: throw new Exception($message);
50: }
51: }
52: