示例#1
0
 public function testResolve()
 {
     $styles = ['token.first' => 'first', 'token' => 'token'];
     $this->assertEquals('first', ArrayHelper::resolve($styles, 'token.first'));
     $this->assertEquals('token', ArrayHelper::resolve($styles, 'token.second'));
     $this->assertEquals('fallback', ArrayHelper::resolve($styles, 'smth', 'fallback'));
 }
示例#2
0
 public function format(Tokens $tokens)
 {
     $source = $tokens->getSource();
     $result = '';
     $last = 0;
     /** @var Token $token */
     foreach ($tokens as $token) {
         $result .= substr($source, $last, $token->pos - $last);
         if (($style = ArrayHelper::resolve($this->_styles, $token->name)) !== null) {
             $result .= $token->isStart() ? Console::open(is_callable($style) ? $style($token) : $style) : Console::close();
         }
         $last = $token->pos;
     }
     $result .= substr($source, $last) . Console::reset();
     return $result;
 }