diff --git a/extension/qfq/qfq/helper/OnArray.php b/extension/qfq/qfq/helper/OnArray.php index 4beff8d89714623047da915e5a2c18eacb79bd08..0d7020128d230f62c539a55ed855ef06352fd18c 100644 --- a/extension/qfq/qfq/helper/OnArray.php +++ b/extension/qfq/qfq/helper/OnArray.php @@ -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 diff --git a/extension/qfq/tests/phpunit/OnArrayTest.php b/extension/qfq/tests/phpunit/OnArrayTest.php index c60e3c7e6767b2923cc03887c47988f636b73daa..f2bc6b44e93d2718bec9dff7a4e216fa16c3bd1e 100644 --- a/extension/qfq/tests/phpunit/OnArrayTest.php +++ b/extension/qfq/tests/phpunit/OnArrayTest.php @@ -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)); + } }