1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ciandt\Behat\PlaceholdersExtension\Utils; |
4
|
|
|
|
5
|
|
|
use Ciandt\Behat\PlaceholdersExtension\Exception\RedundantConfigTagException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Filters an array of tags, excluding or including only the variant tags |
9
|
|
|
* |
10
|
|
|
* @author bwowk |
11
|
|
|
*/ |
12
|
|
|
class PlaceholderUtils |
13
|
|
|
{ |
14
|
|
|
private static $variantTags; |
15
|
|
|
|
16
|
|
|
private static $configKeys; |
17
|
|
|
|
18
|
|
|
public static function setVariantTags($variantTags) |
19
|
|
|
{ |
20
|
|
|
self::$variantTags = $variantTags; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public static function setConfigKeys($configKeys) |
24
|
|
|
{ |
25
|
|
|
self::$configKeys = $configKeys; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function getSectionKey($tag){ |
29
|
|
|
$tagParts = explode(':', $tag); |
30
|
|
|
if (count($tagParts) == 2){ |
31
|
|
|
return $tagParts[1]; |
32
|
|
|
} else { |
33
|
|
|
return 'default'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function getConfigTag($tags){ |
39
|
|
|
$configTags = self::filterConfigTags($tags,false); |
40
|
|
|
if (empty($configTags)) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
if (count($configTags) > 1) { |
44
|
|
|
throw new RedundantConfigTagException($configTags); |
45
|
|
|
} |
46
|
|
|
return $configTags[0]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public static function getConfigKey($tag){ |
51
|
|
|
return explode(':', $tag)[0]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public static function filterVariantTags($tags, $exclude) |
56
|
|
|
{ |
57
|
|
|
return array_filter($tags, function ($tag) use ($exclude) { |
58
|
|
|
return (in_array($tag, self::$variantTags) xor $exclude); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getVariant($tags){ |
63
|
|
|
$variantTags = self::filterVariantTags($tags, false); |
64
|
|
|
if (count($variantTags) > 1) { |
65
|
|
|
throw new \RuntimeException("Scenario should only have one variant tag." |
66
|
|
|
. " Multiple found: " . implode(', ', $variantTags)); |
67
|
|
|
} |
68
|
|
|
if (empty($variantTags)){ |
69
|
|
|
return 'default'; |
70
|
|
|
} |
71
|
|
|
return end($variantTags); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public static function filterConfigTags($tags, $exclude) |
76
|
|
|
{ |
77
|
|
|
return array_filter($tags, function ($tag) use ($exclude){ |
78
|
|
|
return (in_array(self::getConfigKey($tag), self::$configKeys) xor $exclude); |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|