Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gatling Gun
<?php
/**
* Gatling gun takes an array of valid params and returns every
* combination of invalid data caused by blanking out individual params.
*
* Example:
*
* $validData = array('firstName' => 'Gatling', 'lastName' => 'Gun', 'age' => 12);
* $ggData = gatlingGun($validData);
*
* $ggData = array(
* array('firstName' => '', 'lastName' => '', 'age' => ''),
* array('firstName' => '', 'lastName' => '', 'age' => 12),
* array('firstName' => '', 'lastName' => 'Gun', 'age' => ''),
* array('firstName' => '', 'lastName' => 'Gun', 'age' => 12),
* array('firstName' => 'Gatling', 'lastName' => '', 'age' => ''),
* array('firstName' => 'Gatling', 'lastName' => '', 'age' => 12),
* array('firstName' => 'Gatling', 'lastName' => 'Gun', 'age' => '')
* );
*
*
* @param array $validData
* @return array
*
*/
function gatlingGun($validData)
{
$original = $validData;
$invalidCombinations = array();
$max = count($original);
$keys = array_keys($original);
$combinations = pow(2, $max) - 1;
for ($i = 0; $i < $combinations; $i++) {
$tmp = array();
$j = count($keys) - 1;
foreach ($keys as $k => $key) {
$tmp[$key] = (($i & pow(2,$j)) == 0) ? '' : $original[$key];
$j--;
}
$invalidCombinations[] = $tmp;
}
return $invalidCombinations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.