Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Helper / StepTextPainter.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\Node\Printer\Helper;
12
13 use Behat\Behat\Definition\Definition;
14 use Behat\Behat\Definition\Pattern\PatternTransformer;
15 use Behat\Testwork\Tester\Result\TestResult;
16
17 /**
18  * Paints step text (with tokens) according to found definition.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class StepTextPainter
23 {
24     /**
25      * @var PatternTransformer
26      */
27     private $patternTransformer;
28     /**
29      * @var ResultToStringConverter
30      */
31     private $resultConverter;
32
33     /**
34      * Initializes painter.
35      *
36      * @param PatternTransformer      $patternTransformer
37      * @param ResultToStringConverter $resultConverter
38      */
39     public function __construct(PatternTransformer $patternTransformer, ResultToStringConverter $resultConverter)
40     {
41         $this->patternTransformer = $patternTransformer;
42         $this->resultConverter = $resultConverter;
43     }
44
45     /**
46      * Colorizes step text arguments according to definition.
47      *
48      * @param string     $text
49      * @param Definition $definition
50      * @param TestResult $result
51      *
52      * @return string
53      */
54     public function paintText($text, Definition $definition, TestResult $result)
55     {
56         $regex = $this->patternTransformer->transformPatternToRegex($definition->getPattern());
57         $style = $this->resultConverter->convertResultToString($result);
58         $paramStyle = $style . '_param';
59
60         // If it's just a string - skip
61         if ('/' !== substr($regex, 0, 1)) {
62             return $text;
63         }
64
65         // Find arguments with offsets
66         $matches = array();
67         preg_match($regex, $text, $matches, PREG_OFFSET_CAPTURE);
68         array_shift($matches);
69
70         // Replace arguments with colorized ones
71         $shift = 0;
72         $lastReplacementPosition = 0;
73         foreach ($matches as $key => $match) {
74             if (!is_numeric($key) || -1 === $match[1] || false !== strpos($match[0], '<')) {
75                 continue;
76             }
77
78             $offset = $match[1] + $shift;
79             $value = $match[0];
80
81             // Skip inner matches
82             if ($lastReplacementPosition > $offset) {
83                 continue;
84             }
85             $lastReplacementPosition = $offset + strlen($value);
86
87             $begin = substr($text, 0, $offset);
88             $end = substr($text, $lastReplacementPosition);
89             $format = "{-$style}{+$paramStyle}%s{-$paramStyle}{+$style}";
90             $text = sprintf("%s{$format}%s", $begin, $value, $end);
91
92             // Keep track of how many extra characters are added
93             $shift += strlen($format) - 2;
94             $lastReplacementPosition += strlen($format) - 2;
95         }
96
97         // Replace "<", ">" with colorized ones
98         $text = preg_replace(
99             '/(<[^>]+>)/',
100             "{-$style}{+$paramStyle}\$1{-$paramStyle}{+$style}",
101             $text
102         );
103
104         return $text;
105     }
106 }