Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1556 from pasamio/jarrayhelper_invert

Add JArrayHelper::invert
  • Loading branch information...
commit e6b3e96110b7952d9853634a6252bb8fa1a34090 2 parents 350b18a + 37e430f
@LouisLandry LouisLandry authored
View
49 libraries/joomla/utilities/arrayhelper.php
@@ -331,6 +331,55 @@ public static function getValue(&$array, $name, $default = null, $type = '')
}
/**
+ * 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.
*
* @param array $array An array to test.
View
73 tests/suites/unit/joomla/utilities/JArrayHelperTest.php
@@ -373,6 +373,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
*
* @return array
@@ -1329,6 +1382,7 @@ public function testArrayUnique($input, $expected)
$this->equalTo($expected)
);
}
+
/**
* Tests conversion of object to string.
*
@@ -1410,6 +1464,25 @@ public function testGetValue($input, $index, $default, $type, $expect, $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.
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.