X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FTestwork%2FArgument%2FPregMatchArgumentOrganiser.php;fp=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FTestwork%2FArgument%2FPregMatchArgumentOrganiser.php;h=2fb05358522ce563449eaa4a80bdd6475b4ee0e2;hp=0000000000000000000000000000000000000000;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68 diff --git a/vendor/behat/behat/src/Behat/Testwork/Argument/PregMatchArgumentOrganiser.php b/vendor/behat/behat/src/Behat/Testwork/Argument/PregMatchArgumentOrganiser.php new file mode 100644 index 000000000..2fb053585 --- /dev/null +++ b/vendor/behat/behat/src/Behat/Testwork/Argument/PregMatchArgumentOrganiser.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Behat\Testwork\Argument; + +use ReflectionFunctionAbstract; + +/** + * Organises arguments coming from preg_match results. + * + * @author Konstantin Kudryashov + */ +final class PregMatchArgumentOrganiser implements ArgumentOrganiser +{ + /** + * @var ArgumentOrganiser + */ + private $baseOrganiser; + + /** + * Initialises organiser. + * + * @param ArgumentOrganiser $organiser + */ + public function __construct(ArgumentOrganiser $organiser) + { + $this->baseOrganiser = $organiser; + } + + /** + * {@inheritdoc} + */ + public function organiseArguments(ReflectionFunctionAbstract $function, array $match) + { + $arguments = $this->cleanupMatchDuplicates($match); + + return $this->baseOrganiser->organiseArguments($function, $arguments); + } + + /** + * Cleans up provided preg_match match into a list of arguments. + * + * `preg_match` matches named arguments with named indexes and also + * represents all arguments with numbered indexes. This method removes + * duplication and also drops the first full match element from the + * array. + * + * @param array $match + * + * @return mixed[] + */ + private function cleanupMatchDuplicates(array $match) + { + $cleanMatch = array_slice($match, 1); + $arguments = array(); + + $keys = array_keys($cleanMatch); + for ($keyIndex = 0; $keyIndex < count($keys); $keyIndex++) { + $key = $keys[$keyIndex]; + + $arguments[$key] = $cleanMatch[$key]; + + if ($this->isKeyAStringAndNexOneIsAnInteger($keyIndex, $keys)) { + $keyIndex += 1; + } + } + + return $arguments; + } + + /** + * Checks if key at provided index is a string and next key in the array is an integer. + * + * @param integer $keyIndex + * @param mixed[] $keys + * + * @return Boolean + */ + private function isKeyAStringAndNexOneIsAnInteger($keyIndex, array $keys) + { + $keyIsAString = is_string($keys[$keyIndex]); + $nextKeyIsAnInteger = isset($keys[$keyIndex + 1]) && is_integer($keys[$keyIndex + 1]); + + return $keyIsAString && $nextKeyIsAnInteger; + } +}