Overview

Namespaces

  • None
  • Peridot
    • Leo
      • Formatter
      • Interfaces
        • Assert
      • Matcher
        • Template
      • ObjectPath
      • Responder
  • PHP

Classes

  • Formatter

Interfaces

  • FormatterInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace Peridot\Leo\Formatter;
  3: 
  4: use Peridot\Leo\Matcher\Match;
  5: use Peridot\Leo\Matcher\Template\TemplateInterface;
  6: 
  7: /**
  8:  * Class Formatter is the core FormatterInterface for Leo.
  9:  *
 10:  * @package Peridot\Leo\Formatter
 11:  */
 12: class Formatter implements FormatterInterface
 13: {
 14:     /**
 15:      * @var Match
 16:      */
 17:     protected $match;
 18: 
 19:     public function __construct()
 20:     {
 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:         return $this;
 44:     }
 45: 
 46:     /**
 47:      * {@inheritdoc}
 48:      *
 49:      * @param TemplateInterface $template
 50:      * @return mixed|string
 51:      */
 52:     public function getMessage(TemplateInterface $template)
 53:     {
 54:         $vars = $this->getTemplateVars($template);
 55: 
 56:         $tpl = $this->match->isNegated()
 57:             ? $template->getNegatedTemplate()
 58:             : $template->getDefaultTemplate();
 59: 
 60:         foreach ($vars as $name => $value) {
 61:             $tpl = str_replace('{{' . $name . '}}', $this->objectToString($value), $tpl);
 62:         }
 63: 
 64:         return $tpl;
 65:     }
 66: 
 67:     /**
 68:      * {@inheritdoc}
 69:      *
 70:      * @param mixed $obj
 71:      * @return string
 72:      */
 73:     public function objectToString($obj)
 74:     {
 75:         if ($obj === false) {
 76:             return 'false';
 77:         }
 78: 
 79:         if ($obj === true) {
 80:             return 'true';
 81:         }
 82: 
 83:         if (is_null($obj)) {
 84:             return 'null';
 85:         }
 86: 
 87:         if (is_string($obj)) {
 88:             return '"' . $obj . '"';
 89:         }
 90: 
 91:         return rtrim(print_r($obj, true));
 92:     }
 93: 
 94:     /**
 95:      * Applies match results to other template variables.
 96:      *
 97:      * @param TemplateInterface $template
 98:      * @return array
 99:      */
100:     protected function getTemplateVars(TemplateInterface $template)
101:     {
102:         $vars = [
103:             'expected' => $this->match->getExpected(),
104:             'actual' => $this->match->getActual()
105:         ];
106: 
107:         if ($tplVars = $template->getTemplateVars()) {
108:             $vars = array_merge($vars, $tplVars);
109:         }
110: 
111:         return $vars;
112:     }
113: }
114: 
Leo API documentation generated by ApiGen