6af5bacd6b76dbbda891317a57a08eec5b584f4f
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Definition / Pattern / PatternTransformer.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\Definition\Pattern;
12
13 use Behat\Behat\Definition\Exception\UnknownPatternException;
14 use Behat\Behat\Definition\Exception\UnsupportedPatternTypeException;
15 use Behat\Behat\Definition\Pattern\Policy\PatternPolicy;
16
17 /**
18  * Transforms patterns using registered policies.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class PatternTransformer
23 {
24     /**
25      * @var PatternPolicy[]
26      */
27     private $policies = array();
28
29     /**
30      * Registers pattern policy.
31      *
32      * @param PatternPolicy $policy
33      */
34     public function registerPatternPolicy(PatternPolicy $policy)
35     {
36         $this->policies[] = $policy;
37     }
38
39     /**
40      * Generates pattern.
41      *
42      * @param string $type
43      * @param string $stepText
44      *
45      * @return Pattern
46      *
47      * @throws UnsupportedPatternTypeException
48      */
49     public function generatePattern($type, $stepText)
50     {
51         foreach ($this->policies as $policy) {
52             if ($policy->supportsPatternType($type)) {
53                 return $policy->generatePattern($stepText);
54             }
55         }
56
57         throw new UnsupportedPatternTypeException(sprintf('Can not find policy for a pattern type `%s`.', $type), $type);
58     }
59
60     /**
61      * Transforms pattern string to regex.
62      *
63      * @param string $pattern
64      *
65      * @return string
66      *
67      * @throws UnknownPatternException
68      */
69     public function transformPatternToRegex($pattern)
70     {
71         foreach ($this->policies as $policy) {
72             if ($policy->supportsPattern($pattern)) {
73                 return $policy->transformPatternToRegex($pattern);
74             }
75         }
76
77         throw new UnknownPatternException(sprintf('Can not find policy for a pattern `%s`.', $pattern), $pattern);
78     }
79 }