Completed
Push — master ( 96adce...435a1a )
by Rudie
02:06
created

BehatVariablesArgumentTransformer::getContext()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace rdx\behatvars;
4
5
use Behat\Behat\Definition\Call\DefinitionCall;
6
use Behat\Behat\Transformation\Transformer\ArgumentTransformer;
7
use rdx\behatvars\BehatVariablesContext;
8
use rdx\behatvars\BehatVariablesDatabase;
9
10
class BehatVariablesArgumentTransformer implements ArgumentTransformer {
11
12
	const SLOT_NAME_OPEN = '<<';
13
	const SLOT_NAME_CLOSE = '>>';
14
	const SLOT_NAME_REGEX = '#<<(\w+)>>#i';
15
16
	protected $context;
17
	protected $matches;
18
19
	/**
20
	 *
21
	 */
22
	static public function validSlotName($slot) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
23
		$slot = self::SLOT_NAME_OPEN . $slot . self::SLOT_NAME_CLOSE;
24
		return preg_match(self::SLOT_NAME_REGEX, $slot);
25
	}
26
27
	/**
28
	 *
29
	 */
30
	public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) {
31
		return
32
			is_scalar($argumentValue) &&
33
			preg_match_all(self::SLOT_NAME_REGEX, $argumentValue, $this->matches, PREG_SET_ORDER);
34
	}
35
36
	/**
37
	 *
38
	 */
39
	public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue) {
40
		$replacements = array();
41
		foreach ($this->matches as $match) {
42
			$replacements[ $match[0] ] = BehatVariablesDatabase::get($match[1]);
43
		}
44
45
		return strtr($argumentValue, $replacements);
46
	}
47
48
}
49