Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#6903-allow-replacin…
Browse files Browse the repository at this point in the history
…g-keys-in-array-utils' into develop

Close zendframework/zendframework#6903
  • Loading branch information
Ocramius committed Dec 6, 2014
2 parents 1756cda + 309e16b commit 12840f8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Traversable;
use Zend\Stdlib\ArrayUtils\MergeRemoveKey;
use Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface;

/**
* Utility class for testing and manipulation of PHP arrays.
Expand Down Expand Up @@ -258,7 +259,9 @@ public static function iteratorToArray($iterator, $recursive = true)
public static function merge(array $a, array $b, $preserveNumericKeys = false)
{
foreach ($b as $key => $value) {
if (isset($a[$key]) || array_key_exists($key, $a)) {
if ($value instanceof MergeReplaceKeyInterface) {
$a[$key] = $value->getData();
} elseif (isset($a[$key]) || array_key_exists($key, $a)) {
if ($value instanceof MergeRemoveKey) {
unset($a[$key]);
} elseif (!$preserveNumericKeys && is_int($key)) {
Expand Down
34 changes: 34 additions & 0 deletions src/ArrayUtils/MergeReplaceKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\ArrayUtils;

final class MergeReplaceKey implements MergeReplaceKeyInterface
{
/**
* @var mixed
*/
protected $data;

/**
* @param mixed $data
*/
public function __construct($data)
{
$this->data = $data;
}

/**
* {@inheritDoc}
*/
public function getData()
{
return $this->data;
}
}
21 changes: 21 additions & 0 deletions src/ArrayUtils/MergeReplaceKeyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib\ArrayUtils;

/**
* Marker interface: can be used to replace keys completely in {@see ArrayUtils::merge()} operations
*/
interface MergeReplaceKeyInterface
{
/**
* @return mixed
*/
public function getData();
}
31 changes: 31 additions & 0 deletions test/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,37 @@ public static function mergeArrays()
);
}

/**
* @group 6903
*/
public function testMergeReplaceKey()
{
$expected = array(
'car' => array(
'met' => 'bet',
),
'new' => array(
'foo' => 'get',
),
);
$a = array(
'car' => array(
'boo' => 'foo',
'doo' => 'moo',
),
);
$b = array(
'car' => new \Zend\Stdlib\ArrayUtils\MergeReplaceKey(array(
'met' => 'bet',
)),
'new' => new \Zend\Stdlib\ArrayUtils\MergeReplaceKey(array(
'foo' => 'get',
)),
);
$this->assertInstanceOf('Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface', $b['car']);
$this->assertEquals($expected, ArrayUtils::merge($a, $b));
}

/**
* @group 6899
*/
Expand Down

0 comments on commit 12840f8

Please sign in to comment.