|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace espend\Behat\PlaceholderExtension\Transformer; |
|
5
|
|
|
|
|
6
|
|
|
use Behat\Behat\Definition\Call\DefinitionCall; |
|
7
|
|
|
use Behat\Behat\Transformation\Transformer\ArgumentTransformer; |
|
8
|
|
|
use espend\Behat\PlaceholderExtension\PlaceholderBagInterface; |
|
9
|
|
|
use espend\Behat\PlaceholderExtension\Utils\PlaceholderUtil; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Daniel Espendiller <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class PlaceholderArgumentTransformer implements ArgumentTransformer |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var PlaceholderBagInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $placeholderBag; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param PlaceholderBagInterface $placeholderBag |
|
23
|
|
|
*/ |
|
24
|
8 |
|
public function __construct(PlaceholderBagInterface $placeholderBag) |
|
25
|
|
|
{ |
|
26
|
8 |
|
$this->placeholderBag = $placeholderBag; |
|
27
|
8 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) |
|
33
|
|
|
{ |
|
34
|
4 |
|
if (!is_string($argumentValue)) { |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// '%FOO%', '%foo%' |
|
39
|
4 |
|
if (PlaceholderUtil::isValidPlaceholder($argumentValue)) { |
|
40
|
1 |
|
return ($placeholders = $this->placeholderBag->all()) |
|
41
|
1 |
|
&& isset($placeholders[$argumentValue]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// 'foobar%FOO%' |
|
45
|
3 |
|
foreach ($this->placeholderBag->all() as $key => $value) { |
|
46
|
3 |
|
if (false !== strpos($argumentValue, $key)) { |
|
47
|
3 |
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) |
|
58
|
|
|
{ |
|
59
|
|
|
// 'foobar%FOO%' |
|
60
|
4 |
|
foreach ($this->placeholderBag->all() as $key => $value) { |
|
61
|
4 |
|
$argumentValue = str_replace($key, $value, $argumentValue); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// '%FOO%', '%foo%' |
|
65
|
4 |
|
$placeholder = $this->placeholderBag->all(); |
|
66
|
4 |
|
if (isset($placeholder[$argumentValue])) { |
|
67
|
|
|
return $placeholder[$argumentValue]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
return $argumentValue; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|