Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Add JArrayHelper::invert #1556

Merged
merged 1 commit into from Oct 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions libraries/joomla/utilities/arrayhelper.php
Expand Up @@ -330,6 +330,55 @@ public static function getValue(&$array, $name, $default = null, $type = '')
return $result;
}

/**
* Takes an associative array of arrays and inverts the array keys to values using the array values as keys.
*
* Example:
* $input = array(
* 'New' => array('1000', '1500', '1750'),
* 'Used' => array('3000', '4000', '5000', '6000')
* );
* $output = JArrayHelper::invert($input);
*
* Output would be equal to:
* $output = array(
* '1000' => 'New',
* '1500' => 'New',
* '1750' => 'New',
* '3000' => 'Used',
* '4000' => 'Used',
* '5000' => 'Used',
* '6000' => 'Used'
* );
*
* @param array $array The source array.
*
* @return array The inverted array.
*
* @since 12.3
*/
public static function invert($array)
{
$return = array();
foreach ($array as $base => $values)
{
if (!is_array($values))
{
continue;
}

foreach ($values as $key)
{
// If the key isn't scalar then ignore it.
if (is_scalar($key))
{
$return[$key] = $base;
}
}
}
return $return;
}

/**
* Method to determine if an array is an associative array.
*
Expand Down
73 changes: 73 additions & 0 deletions tests/suites/unit/joomla/utilities/JArrayHelperTest.php
Expand Up @@ -372,6 +372,59 @@ function getTestGetValueData()
);
}

/**
* Data provider for invert
*
* @return array
*
* @since 12.3
*/
function getTestInvertData()
{

return array(
'Case 1' => array(
// Input
array(
'New' => array('1000', '1500', '1750'),
'Used' => array('3000', '4000', '5000', '6000')
),
// Expected
array(
'1000' => 'New',
'1500' => 'New',
'1750' => 'New',
'3000' => 'Used',
'4000' => 'Used',
'5000' => 'Used',
'6000' => 'Used'
)
),
'Case 2' => array(
// Input
array(
'New' => array(1000, 1500, 1750),
'Used' => array(2750, 3000, 4000, 5000, 6000),
'Refurbished' => array(2000, 2500),
'Unspecified' => array()
),
// Expected
array(
'1000' => 'New',
'1500' => 'New',
'1750' => 'New',
'2750' => 'Used',
'3000' => 'Used',
'4000' => 'Used',
'5000' => 'Used',
'6000' => 'Used',
'2000' => 'Refurbished',
'2500' => 'Refurbished'
)
)
);
}

/**
* Data provider for testPivot
*
Expand Down Expand Up @@ -1329,6 +1382,7 @@ public function testArrayUnique($input, $expected)
$this->equalTo($expected)
);
}

/**
* Tests conversion of object to string.
*
Expand Down Expand Up @@ -1409,6 +1463,25 @@ public function testGetValue($input, $index, $default, $type, $expect, $message,
$this->assertEquals($expect, $output, $message);
}

/**
* Tests the JArrayHelper::invert method.
*
* @param array $input The array being input.
* @param string $expected The expected return value.
*
* @return void
*
* @dataProvider getTestInvertData
* @since 12.3
*/
public function testInvert($input, $expected)
{
$this->assertThat(
JArrayHelper::invert($input),
$this->equalTo($expected)
);
}

/**
* Test the JArrayHelper::isAssociate method.
*
Expand Down