Advertisement
valdeir2000

StackOverflow 266968 - Hash.php

Jan 4th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4.  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5.  *
  6.  * Licensed under The MIT License
  7.  * For full copyright and license information, please see the LICENSE.txt
  8.  * Redistributions of files must retain the above copyright notice.
  9.  *
  10.  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11.  * @since         2.2.0
  12.  * @license       https://opensource.org/licenses/mit-license.php MIT License
  13.  */
  14. namespace Cake\Utility;
  15.  
  16. use ArrayAccess;
  17. use InvalidArgumentException;
  18. use RuntimeException;
  19.  
  20. /**
  21.  * Library of array functions for manipulating and extracting data
  22.  * from arrays or 'sets' of data.
  23.  *
  24.  * `Hash` provides an improved interface, more consistent and
  25.  * predictable set of features over `Set`. While it lacks the spotty
  26.  * support for pseudo Xpath, its more fully featured dot notation provides
  27.  * similar features in a more consistent implementation.
  28.  *
  29.  * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html
  30.  */
  31. class Hash
  32. {
  33.  
  34.     /**
  35.      * This function can be thought of as a hybrid between PHP's `array_merge` and `array_merge_recursive`.
  36.      *
  37.      * The difference between this method and the built-in ones, is that if an array key contains another array, then
  38.      * Hash::merge() will behave in a recursive fashion (unlike `array_merge`). But it will not act recursively for
  39.      * keys that contain scalar values (unlike `array_merge_recursive`).
  40.      *
  41.      * Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
  42.      *
  43.      * @param array $data Array to be merged
  44.      * @param mixed $merge Array to merge with. The argument and all trailing arguments will be array cast when merged
  45.      * @return array Merged array
  46.      * @link https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::merge
  47.      */
  48.     public static function merge(array $data, $merge)
  49.     {
  50.         $args = array_slice(func_get_args(), 1);
  51.         $return = $data;
  52.         $stack = [];
  53.  
  54.         foreach ($args as &$curArg) {
  55.             $stack[] = [(array)$curArg, &$return];
  56.         }
  57.         unset($curArg);
  58.         static::_merge($stack, $return);
  59.  
  60.         return $return;
  61.     }
  62.  
  63.     /**
  64.      * Merge helper function to reduce duplicated code between merge() and expand().
  65.      *
  66.      * @param array $stack The stack of operations to work with.
  67.      * @param array $return The return value to operate on.
  68.      * @return void
  69.      */
  70.     protected static function _merge($stack, &$return)
  71.     {
  72.         while (!empty($stack)) {
  73.             foreach ($stack as $curKey => &$curMerge) {
  74.                 foreach ($curMerge[0] as $key => &$val) {
  75.                     $isArray = is_array($curMerge[1]);
  76.                     if ($isArray && !empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) {
  77.                         // Recurse into the current merge data as it is an array.
  78.                         $stack[] = [&$val, &$curMerge[1][$key]];
  79.                     } elseif ((int)$key === $key && $isArray && isset($curMerge[1][$key])) {
  80.                         $curMerge[1][] = $val;
  81.                     } else {
  82.                         $curMerge[1][$key] = $val;
  83.                     }
  84.                 }
  85.                 unset($stack[$curKey]);
  86.             }
  87.             unset($curMerge);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement