diff --git a/core/includes/common.inc b/core/includes/common.inc index e2a28c3..c4ed1f5 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -7,6 +7,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Yaml\Parser; use Drupal\Component\PhpStorage\PhpStorageFactory; +use Drupal\Component\Utility\MapArray; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Datetime\DrupalDateTime; @@ -2014,28 +2015,10 @@ function drupal_exit($destination = NULL) { /** * Forms an associative array from a linear array. * - * This function walks through the provided array and constructs an associative - * array out of it. The keys of the resulting array will be the values of the - * input array. The values will be the same as the keys unless a function is - * specified, in which case the output of the function is used for the values - * instead. - * - * @param $array - * A linear array. - * @param $function - * A name of a function to apply to all values before output. - * - * @return - * An associative array. + * @see \Drupal\Component\Utility\MapArray::copyValuesToKeys() */ -function drupal_map_assoc($array, $function = NULL) { - // array_combine() fails with empty arrays: - // http://bugs.php.net/bug.php?id=34857. - $array = !empty($array) ? array_combine($array, $array) : array(); - if (is_callable($function)) { - $array = array_map($function, $array); - } - return $array; +function drupal_map_assoc($array, $callback = NULL) { + return MapArray::copyValuesToKeys($array, $callback); } /** diff --git a/core/lib/Drupal/Component/Utility/MapArray.php b/core/lib/Drupal/Component/Utility/MapArray.php new file mode 100644 index 0000000..f671223 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/MapArray.php @@ -0,0 +1,42 @@ + 'MapArray test', + 'description' => 'Test that the MapArray functions work properly.', + 'group' => 'Bootstrap', + ); + } + + /** + * Tests MapArray::copyValuesToKey() input against expected output. + * + * @dataProvider providerCopyValuesToKey + * + * @param $input + * The input variable for the MapArray::copyValuesToKey() function. + * @param $expected + * The expected output from when calling the function. + * @param callable $function + * The optional function callback. + * + * @see Drupal\Component\Utility\MapArray::copyValuesToKey() + * @see Drupal\Tests\Component\Utility\MapArrayTest::providerCopyValuesToKey() + */ + public function testCopyValuesToKey($input, $expected, $function = NULL) { + $output = MapArray::copyValuesToKeys($input, $function); + $this->assertEquals($expected, $output); + } + + /** + * Data provider for MapArray::copyValuesToKey(). + * + * @return + * An array of tests, matching the parameter inputs for testCopyValuesToKey. + * + * @see Drupal\Component\Utility\MapArray::copyValuesToKey() + * @see Drupal\Tests\Component\Utility\MapArrayTest::testCopyValuesToKey() + */ + public function providerCopyValuesToKey() { + // Test an empty array. + $tests[] = array( + array(), + array() + ); + + // Tests the creation of an associative array. + $tests[] = array( + array('foobar'), + array('foobar' => 'foobar') + ); + + // Tests overwriting indexes with their value. + $tests[] = array( + array('foo' => 'bar'), + array('bar' => 'bar') + ); + + // Tests using the callback function. + $tests[] = array( + array(1, 2, 3, 4, 5), + array( + 1 => 2, + 2 => 4, + 3 => 6, + 4 => 8, + 5 => 10, + ), + 'Drupal\Tests\Component\Utility\MapArrayTest::providerCopyValuesToKeyCallback', + ); + + return $tests; + } + + /** + * Callback for a test in providerCopyValuesToKey(), which doubles the value. + * + * @param $n + * The value passed in from array_map(). + */ + public function providerCopyValuesToKeyCallback($n) { + return $n * 2; + } + +}