From e2a57e4b885f61dd13ba3069c0b5a852f0c360ba Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Thu, 9 May 2013 12:42:50 +0200 Subject: [PATCH 1/4] Enhance ArrayHelper with popValue method --- framework/helpers/base/ArrayHelper.php | 33 ++++++++++++++++++++++-- tests/unit/framework/helpers/ArrayHelperTest.php | 10 +++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/framework/helpers/base/ArrayHelper.php b/framework/helpers/base/ArrayHelper.php index 672c7b9..7a0b606 100644 --- a/framework/helpers/base/ArrayHelper.php +++ b/framework/helpers/base/ArrayHelper.php @@ -87,6 +87,35 @@ class ArrayHelper } /** + * Removes an item from an array and returns the value. If the key does not exist in the array, the default value + * will be returned instead. + * + * Usage examples, + * + * ~~~ + * // $array = array('type'=>'A', 'options'=>array(1,2)); + * // working with array + * $type = \yii\helpers\ArrayHelper::getValue($array, 'type'); + * // $array content + * // $array = array('options'=>array(1,2)); + * ~~~ + * + * @param array $array the array to extract value from + * @param string $key key name of the array element + * @param mixed $default the default value to be returned if the specified key does not exist + * @return mixed|null the value of the element if found, default value otherwise + */ + public static function popValue(&$array, $key, $default = null) + { + if (is_array($array)) { + $value = static::getValue($array, $key, $default); + unset($array[$key]); + return $value; + } + return $default; + } + + /** * Indexes an array according to a specified key. * The input array should be multidimensional or an array of objects. * @@ -284,12 +313,12 @@ class ArrayHelper $args[] = $column; } } else { - $args[] = static::getColumn($array, $key); + $args[] = static::getColumn($array, $key); } $args[] = $ascending[$i] ? SORT_ASC : SORT_DESC; $args[] = $flag; } - $args[] = &$array; + $args[] = & $array; call_user_func_array('array_multisort', $args); } diff --git a/tests/unit/framework/helpers/ArrayHelperTest.php b/tests/unit/framework/helpers/ArrayHelperTest.php index b3ffabf..897fcf7 100644 --- a/tests/unit/framework/helpers/ArrayHelperTest.php +++ b/tests/unit/framework/helpers/ArrayHelperTest.php @@ -12,6 +12,16 @@ class ArrayHelperTest extends \yii\test\TestCase } + public function testPopvalue() + { + $array = array('name' => 'b', 'age' => 3); + $name = ArrayHelper::popValue($array, 'name'); + + $this->assertEquals($name, 'b'); + $this->assertEquals($array, array('age' => 3)); + } + + public function testMultisort() { // single key From ad25cdc906e768ed1fe3650483f316e05ab2d3c1 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Thu, 9 May 2013 12:51:08 +0200 Subject: [PATCH 2/4] Fix 'automated' (?) space --- framework/helpers/base/ArrayHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/helpers/base/ArrayHelper.php b/framework/helpers/base/ArrayHelper.php index 7a0b606..5df483a 100644 --- a/framework/helpers/base/ArrayHelper.php +++ b/framework/helpers/base/ArrayHelper.php @@ -318,7 +318,7 @@ class ArrayHelper $args[] = $ascending[$i] ? SORT_ASC : SORT_DESC; $args[] = $flag; } - $args[] = & $array; + $args[] = &$array; call_user_func_array('array_multisort', $args); } From 673d207253aa6b9b8f7bba49861398fe6e9a521b Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Thu, 9 May 2013 13:10:18 +0200 Subject: [PATCH 3/4] Fix silly typo --- framework/helpers/base/ArrayHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/helpers/base/ArrayHelper.php b/framework/helpers/base/ArrayHelper.php index 5df483a..4802651 100644 --- a/framework/helpers/base/ArrayHelper.php +++ b/framework/helpers/base/ArrayHelper.php @@ -95,7 +95,7 @@ class ArrayHelper * ~~~ * // $array = array('type'=>'A', 'options'=>array(1,2)); * // working with array - * $type = \yii\helpers\ArrayHelper::getValue($array, 'type'); + * $type = \yii\helpers\ArrayHelper::popValue($array, 'type'); * // $array content * // $array = array('options'=>array(1,2)); * ~~~ From 5da62fab3acdb11f857067a9b6b9b1ddd1a56385 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Thu, 9 May 2013 18:23:10 +0200 Subject: [PATCH 4/4] #196 refactored method name --- framework/helpers/base/ArrayHelper.php | 8 ++++---- tests/unit/framework/helpers/ArrayHelperTest.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/framework/helpers/base/ArrayHelper.php b/framework/helpers/base/ArrayHelper.php index 4802651..e482883 100644 --- a/framework/helpers/base/ArrayHelper.php +++ b/framework/helpers/base/ArrayHelper.php @@ -95,7 +95,7 @@ class ArrayHelper * ~~~ * // $array = array('type'=>'A', 'options'=>array(1,2)); * // working with array - * $type = \yii\helpers\ArrayHelper::popValue($array, 'type'); + * $type = \yii\helpers\ArrayHelper::remove($array, 'type'); * // $array content * // $array = array('options'=>array(1,2)); * ~~~ @@ -105,10 +105,10 @@ class ArrayHelper * @param mixed $default the default value to be returned if the specified key does not exist * @return mixed|null the value of the element if found, default value otherwise */ - public static function popValue(&$array, $key, $default = null) + public static function remove(&$array, $key, $default = null) { - if (is_array($array)) { - $value = static::getValue($array, $key, $default); + if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) { + $value = $array[$key]; unset($array[$key]); return $value; } diff --git a/tests/unit/framework/helpers/ArrayHelperTest.php b/tests/unit/framework/helpers/ArrayHelperTest.php index 897fcf7..8c83278 100644 --- a/tests/unit/framework/helpers/ArrayHelperTest.php +++ b/tests/unit/framework/helpers/ArrayHelperTest.php @@ -12,10 +12,10 @@ class ArrayHelperTest extends \yii\test\TestCase } - public function testPopvalue() + public function testRemove() { $array = array('name' => 'b', 'age' => 3); - $name = ArrayHelper::popValue($array, 'name'); + $name = ArrayHelper::remove($array, 'name'); $this->assertEquals($name, 'b'); $this->assertEquals($array, array('age' => 3));