/
Transformation.php
89 lines (77 loc) · 2.47 KB
/
Transformation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Behat\Behat\Definition\Annotation;
use Behat\Gherkin\Node\TableNode;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Behat\Definition\TransformationInterface,
Behat\Behat\Context\ContextInterface,
Behat\Behat\Annotation\Annotation;
/*
* This file is part of the Behat.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Step arguments transformation.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class Transformation extends Annotation implements TransformationInterface
{
private $regex;
/**
* Initializes transformation.
*
* @param callback $callback definition callback
* @param string $regex definition regular expression
*
* @throws \InvalidArgumentException
*/
public function __construct($callback, $regex)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException(sprintf(
'Transformation callback should be a valid callable, but %s given',
gettype($callback)
));
}
parent::__construct($callback);
$this->regex = $regex;
}
/**
* Returns transformation regex.
*
* @return string
*/
public function getRegex()
{
return $this->regex;
}
/**
* Transforms provided argument.
*
* @param string $translatedRegex
* @param ContextInterface $context
* @param mixed $argument
*
* @return mixed
*/
public function transform($translatedRegex, ContextInterface $context, $argument)
{
$callback = $this->getCallbackForContext($context);
if ($argument instanceof TableNode) {
$tableMatching = 'table:' . implode(',', $argument->getRow(0));
if (preg_match($translatedRegex, $tableMatching)
|| preg_match($this->regex, $tableMatching)) {
return call_user_func($callback, $argument);
}
} elseif (is_string($argument) || $argument instanceof PyStringNode) {
if (preg_match($translatedRegex, (string) $argument, $transformArguments)
|| preg_match($this->regex, (string) $argument, $transformArguments)) {
array_shift($transformArguments);
return call_user_func_array($callback, $transformArguments);
}
}
}
}