Commit 65f5fe1f authored by Carsten  Rose's avatar Carsten Rose
Browse files

OnArray.php: new function copyArrayItemsIfNotAlreadyExists()

parent bcb0aedb
......@@ -182,4 +182,50 @@ class OnArray {
}
return $new;
}
/**
* Search in array $dest for all $keyNames if they exist. If not, check if they exist in $src. If yes, copy.
*
* @param array $src
* @param array $dest
* @param array $keyNames
* @return array $dest filled with new values
*/
public static function copyArrayItemsIfNotAlreadyExist(array $src, array $dest, array $keyNames) {
foreach ($keyNames as $key) {
if (!isset($dest[$key])) {
if (isset($src[$key])) {
$dest[$key] = $src[$key];
}
}
}
return $dest;
}
/**
* Copies all items whose $keyNames are listed to $new and return $new
*
* @param array $src
* @param array $keyNames
* @param bool $createMissing
* @return array
*/
public static function getArrayItems(array $src, array $keyNames, $createMissing = false) {
$new = array();
// Extract necessary elements
foreach ($keyNames as $key) {
if (isset($src[$key])) {
$new[$key] = $src[$key];
} elseif ($createMissing == true) {
$new[$key] = '';
}
}
return $new;
}
}
\ No newline at end of file
......@@ -89,4 +89,35 @@ class OnArrayTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($expected, OnArray::removeEmptyElementsFromArray($raw2));
}
public function testCopyValuesIfNotAlreadyExist() {
$this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(array(), array(), array()));
$this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], array(), array()));
$this->assertEquals(array(), OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], array(), ['b']));
$this->assertEquals(['a' => 'base'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['a' => 'base'], ['unknown']));
$this->assertEquals(['b' => 'base', 'a' => 'something'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['b' => 'base'], ['a']));
$this->assertEquals(['a' => 'base'], OnArray::copyArrayItemsIfNotAlreadyExist(['a' => 'something'], ['a' => 'base'], ['a']));
}
public function testGetArrayItems() {
$this->assertEquals(array(), OnArray::getArrayItems(array(), array()));
$this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello'], array()));
$this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], array()));
$this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c']));
$this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a']));
$this->assertEquals(['a' => 'hello', 'b' => 'world'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'b']));
$this->assertEquals(array(), OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c'], false));
$this->assertEquals(['c' => ''], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['c'], true));
$this->assertEquals(['a' => 'hello'], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'c'], false));
$this->assertEquals(['a' => 'hello', 'c' => ''], OnArray::getArrayItems(['a' => 'hello', 'b' => 'world'], ['a', 'c'], true));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment