4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Argument;
13 use ReflectionFunctionAbstract;
16 * Organises arguments coming from preg_match results.
18 * @author Konstantin Kudryashov <ever.zet@gmail.com>
20 final class PregMatchArgumentOrganiser implements ArgumentOrganiser
23 * @var ArgumentOrganiser
25 private $baseOrganiser;
28 * Initialises organiser.
30 * @param ArgumentOrganiser $organiser
32 public function __construct(ArgumentOrganiser $organiser)
34 $this->baseOrganiser = $organiser;
40 public function organiseArguments(ReflectionFunctionAbstract $function, array $match)
42 $arguments = $this->cleanupMatchDuplicates($match);
44 return $this->baseOrganiser->organiseArguments($function, $arguments);
48 * Cleans up provided preg_match match into a list of arguments.
50 * `preg_match` matches named arguments with named indexes and also
51 * represents all arguments with numbered indexes. This method removes
52 * duplication and also drops the first full match element from the
59 private function cleanupMatchDuplicates(array $match)
61 $cleanMatch = array_slice($match, 1);
64 $keys = array_keys($cleanMatch);
65 for ($keyIndex = 0; $keyIndex < count($keys); $keyIndex++) {
66 $key = $keys[$keyIndex];
68 $arguments[$key] = $cleanMatch[$key];
70 if ($this->isKeyAStringAndNexOneIsAnInteger($keyIndex, $keys)) {
79 * Checks if key at provided index is a string and next key in the array is an integer.
81 * @param integer $keyIndex
82 * @param mixed[] $keys
86 private function isKeyAStringAndNexOneIsAnInteger($keyIndex, array $keys)
88 $keyIsAString = is_string($keys[$keyIndex]);
89 $nextKeyIsAnInteger = isset($keys[$keyIndex + 1]) && is_integer($keys[$keyIndex + 1]);
91 return $keyIsAString && $nextKeyIsAnInteger;