Fixes #11275: Added possibility of unset or force replace former value in ArrayHelper::merge()

This commit is contained in:
Robert Korulczyk
2016-08-03 21:49:49 +02:00
committed by Alexander Makarov
parent 0ac161b69d
commit 993f2aef28
6 changed files with 268 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #11912: Fixed PostgreSQL Schema to support negative default values for integer/float/decimal columns (nsknewbie)
- Bug #11947: Fixed `gridData` initialization in `yii.gridView.js` (pavlm)
- Bug #11949: Fixed `ActiveField::end` generates close tag when it's `option['tag']` is null (egorio)
- Enh #11275: Added possibility of unset or force replace former value in `ArrayHelper::merge()` (mdmunir, rob006)
- Enh #11950: Improve BaseArrayHelper::keyExists speed (egorio)
- Bug #11726: `DbSession` was echoing database errors in production mode (samdark, pastuhov, deadkrolik)
- Bug #12030: Fixed `yii\base\Model::offsetExists()` throws an exception on un-existing field (klimov-paul)

View File

@@ -107,6 +107,8 @@ class BaseArrayHelper
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* You can use [[UnsetArrayValue]] object to unset value from previous array or
* [[ReplaceArrayValue]] to force replace former value instead of recursive merging.
* @param array $a array to be merged to
* @param array $b array to be merged from. You can specify additional
* arrays via third argument, fourth argument etc.
@@ -119,7 +121,11 @@ class BaseArrayHelper
while (!empty($args)) {
$next = array_shift($args);
foreach ($next as $k => $v) {
if (is_int($k)) {
if ($v instanceof UnsetArrayValue) {
unset($res[$k]);
} elseif ($v instanceof ReplaceArrayValue) {
$res[$k] = $v->value;
} elseif (is_int($k)) {
if (isset($res[$k])) {
$res[] = $v;
} else {

View File

@@ -0,0 +1,73 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Object that represents the replacement of array value while performing [[ArrayHelper::merge()]].
*
* Usage example:
*
* ```php
* $array1 = [
* 'ids' => [
* 1,
* ],
* 'validDomains' => [
* 'example.com',
* 'www.example.com',
* ],
* ];
*
* $array2 = [
* 'ids' => [
* 2,
* ],
* 'validDomains' => new \yii\helpers\ReplaceArrayValue([
* 'yiiframework.com',
* 'www.yiiframework.com',
* ]),
* ];
*
* $result = \yii\helpers\ArrayHelper::merge($array1, $array2);
* ```
*
* The result will be
*
* ```php
* [
* 'ids' => [
* 1,
* 2,
* ],
* 'validDomains' => [
* 'yiiframework.com',
* 'www.yiiframework.com',
* ],
* ]
* ```
*
* @author Robert Korulczyk <robert@korulczyk.pl>
* @since 2.0.10
*/
class ReplaceArrayValue
{
/**
* @var mixed value used as replacement.
*/
public $value;
/**
* Constructor.
* @param mixed $value value used as replacement.
*/
public function __construct($value)
{
$this->value = $value;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Object that represents the removal of array value while performing [[ArrayHelper::merge()]].
*
* Usage example:
*
* ```php
* $array1 = [
* 'ids' => [
* 1,
* ],
* 'validDomains' => [
* 'example.com',
* 'www.example.com',
* ],
* ];
*
* $array2 = [
* 'ids' => [
* 2,
* ],
* 'validDomains' => new \yii\helpers\UnsetArrayValue(),
* ];
*
* $result = \yii\helpers\ArrayHelper::merge($array1, $array2);
* ```
*
* The result will be
*
* ```php
* [
* 'ids' => [
* 1,
* 2,
* ],
* ]
* ```
*
* @author Robert Korulczyk <robert@korulczyk.pl>
* @since 2.0.10
*/
class UnsetArrayValue
{
}