Commit da63eafb authored by alexpott's avatar alexpott

Issue #1973312 by RobLoach, ParisLiakos, damiankloip, dawehner: Move...

Issue #1973312 by RobLoach, ParisLiakos, damiankloip, dawehner: Move drupal_map_assoc() to a MapArray Utility Component.
parent 1eb4d700
......@@ -11,6 +11,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
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;
......@@ -1594,28 +1595,10 @@ function drupal_exit() {
/**
* 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, $callable = NULL) {
return MapArray::copyValuesToKeys($array, $callable);
}
/**
......
<?php
/**
* @file
* Contains \Drupal\Component\Utility\MapArray.
*/
namespace Drupal\Component\Utility;
/**
* Provides generic array mapping helper methods.
*/
class MapArray {
/**
* 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 $array
* A linear array.
* @param callable $callback
* A name of a function to apply to all values before output.
*
* @return array
* An associative array.
*/
public static function copyValuesToKeys(array $array, $callable = NULL) {
// array_combine() fails with empty arrays:
// http://bugs.php.net/bug.php?id=34857.
if (!empty($array)) {
$array = array_combine($array, $array);
}
if (is_callable($callable)) {
$array = array_map($callable, $array);
}
return $array;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Utility\MapArrayTest.
*/
namespace Drupal\Tests\Component\Utility;
use Drupal\Tests\UnitTestCase;
use Drupal\Component\Utility\MapArray;
/**
* Tests the MapArray system.
*
* @see \Drupal\Component\Utility\MapArray
*/
class MapArrayTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'MapArray test',
'description' => 'Test that the MapArray functions work properly.',
'group' => 'Bootstrap',
);
}
/**
* Tests MapArray::copyValuesToKey() input against expected output.
*
* @dataProvider providerCopyValuesToKey
*
* @param array $input
* The input array for the MapArray::copyValuesToKey() method.
* @param array $expected
* The expected output from calling the method.
* @param callable $callable
* The optional callable.
*
* @see Drupal\Component\Utility\MapArray::copyValuesToKey()
* @see Drupal\Tests\Component\Utility\MapArrayTest::providerCopyValuesToKey()
*/
public function testCopyValuesToKey(array $input, array $expected, $callable = NULL) {
$output = MapArray::copyValuesToKeys($input, $callable);
$this->assertEquals($expected, $output);
}
/**
* Data provider for MapArray::copyValuesToKey().
*
* @return array
* 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 int $n
* The value passed in from array_map().
*
* @return int
* The doubled integer value.
*/
public static function providerCopyValuesToKeyCallback($n) {
return $n * 2;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment