Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @since 2.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Utility;
- use ArrayAccess;
- use InvalidArgumentException;
- use RuntimeException;
- /**
- * Library of array functions for manipulating and extracting data
- * from arrays or 'sets' of data.
- *
- * `Hash` provides an improved interface, more consistent and
- * predictable set of features over `Set`. While it lacks the spotty
- * support for pseudo Xpath, its more fully featured dot notation provides
- * similar features in a more consistent implementation.
- *
- * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html
- */
- class Hash
- {
- /**
- * This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
- *
- * The difference between this method and the built-in ones, is that if an array key contains another array, then
- * Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
- * keys that contain scalar values (unlike `array_merge_recursive`).
- *
- * Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
- *
- * @param array $data Array to be merged
- * @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged
- * @return array Merged array
- * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::merge
- */
- public static function merge(array $data, $merge)
- {
- $args = array_slice(func_get_args(), 1);
- $return = $data;
- $stack = [];
- foreach ($args as &$curArg) {
- $stack[] = [(array)$curArg, &$return];
- }
- unset($curArg);
- static::_merge($stack, $return);
- return $return;
- }
- /**
- * Merge helper function to reduce duplicated code between merge() and expand().
- *
- * @param array $stack The stack of operations to work with.
- * @param array $return The return value to operate on.
- * @return void
- */
- protected static function _merge($stack, &$return)
- {
- while (!empty($stack)) {
- foreach ($stack as $curKey => &$curMerge) {
- foreach ($curMerge[0] as $key => &$val) {
- $isArray = is_array($curMerge[1]);
- if ($isArray && !empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
- // Recurse into the current merge data as it is an array.
- $stack[] = [&$val, &$curMerge[1][$key]];
- } elseif ((int)$key === $key && $isArray && isset($curMerge[1][$key])) {
- $curMerge[1][] = $val;
- } else {
- $curMerge[1][$key] = $val;
- }
- }
- unset($stack[$curKey]);
- }
- unset($curMerge);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement