Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / config / src / Util / ArrayUtil.php
diff --git a/vendor/consolidation/config/src/Util/ArrayUtil.php b/vendor/consolidation/config/src/Util/ArrayUtil.php
new file mode 100644 (file)
index 0000000..a23f854
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+namespace Consolidation\Config\Util;
+
+/**
+ * Useful array utilities.
+ */
+class ArrayUtil
+{
+    /**
+     * Merges arrays recursively while preserving.
+     *
+     * @param array $array1
+     * @param array $array2
+     *
+     * @return array
+     *
+     * @see http://php.net/manual/en/function.array-merge-recursive.php#92195
+     * @see https://github.com/grasmash/bolt/blob/robo-rebase/src/Robo/Common/ArrayManipulator.php#L22
+     */
+    public static function mergeRecursiveDistinct(
+        array &$array1,
+        array &$array2
+    ) {
+        $merged = $array1;
+        foreach ($array2 as $key => &$value) {
+            $merged[$key] = self::mergeRecursiveValue($merged, $key, $value);
+        }
+        return $merged;
+    }
+
+    /**
+     * Process the value in an mergeRecursiveDistinct - make a recursive
+     * call if needed.
+     */
+    protected static function mergeRecursiveValue(&$merged, $key, $value)
+    {
+        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
+            return self::mergeRecursiveDistinct($merged[$key], $value);
+        }
+        return $value;
+    }
+
+    /**
+     * Fills all of the leaf-node values of a nested array with the
+     * provided replacement value.
+     */
+    public static function fillRecursive(array $data, $fill)
+    {
+        $result = [];
+        foreach ($data as $key => $value) {
+            $result[$key] = $fill;
+            if (self::isAssociative($value)) {
+                $result[$key] = self::fillRecursive($value, $fill);
+            }
+        }
+        return $result;
+    }
+
+    /**
+     * Return true if the provided parameter is an array, and at least
+     * one key is non-numeric.
+     */
+    public static function isAssociative($testArray)
+    {
+        if (!is_array($testArray)) {
+            return false;
+        }
+        foreach (array_keys($testArray) as $key) {
+            if (!is_numeric($key)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}