| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2018 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\Google\ApiCore; |
| 34 |
|
| 35 |
/** |
| 36 |
* Provides basic array helper methods. |
| 37 |
* |
| 38 |
* @internal |
| 39 |
*/ |
| 40 |
trait ArrayTrait |
| 41 |
{ |
| 42 |
/** |
| 43 |
* Pluck a value out of an array. |
| 44 |
* |
| 45 |
* @param string $key |
| 46 |
* @param array $arr |
| 47 |
* @param bool $isRequired |
| 48 |
* @return mixed|null |
| 49 |
* @throws \InvalidArgumentException |
| 50 |
*/ |
| 51 |
private function pluck(string $key, array &$arr, bool $isRequired = \true) |
| 52 |
{ |
| 53 |
if (!array_key_exists($key, $arr)) { |
| 54 |
if ($isRequired) { |
| 55 |
throw new \InvalidArgumentException("Key {$key} does not exist in the provided array."); |
| 56 |
} |
| 57 |
return null; |
| 58 |
} |
| 59 |
$value = $arr[$key]; |
| 60 |
unset($arr[$key]); |
| 61 |
return $value; |
| 62 |
} |
| 63 |
/** |
| 64 |
* Pluck a subset of an array. |
| 65 |
* |
| 66 |
* @param array $keys |
| 67 |
* @param array $arr |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
private function pluckArray(array $keys, array &$arr) |
| 71 |
{ |
| 72 |
$values = []; |
| 73 |
foreach ($keys as $key) { |
| 74 |
if (array_key_exists($key, $arr)) { |
| 75 |
$values[$key] = $this->pluck($key, $arr, \false); |
| 76 |
} |
| 77 |
} |
| 78 |
return $values; |
| 79 |
} |
| 80 |
/** |
| 81 |
* Determine whether given array is associative. |
| 82 |
* |
| 83 |
* @param array $arr |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
private function isAssoc(array $arr) |
| 87 |
{ |
| 88 |
return array_keys($arr) !== range(0, count($arr) - 1); |
| 89 |
} |
| 90 |
/** |
| 91 |
* Just like array_filter(), but preserves falsey values except null. |
| 92 |
* |
| 93 |
* @param array $arr |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
private function arrayFilterRemoveNull(array $arr) |
| 97 |
{ |
| 98 |
return array_filter($arr, function ($element) { |
| 99 |
if (!is_null($element)) { |
| 100 |
return \true; |
| 101 |
} |
| 102 |
return \false; |
| 103 |
}); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Return a subset of an array, like pluckArray, without modifying the original array. |
| 107 |
* |
| 108 |
* @param array $keys |
| 109 |
* @param array $arr |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
private function subsetArray(array $keys, array $arr) |
| 113 |
{ |
| 114 |
return array_intersect_key($arr, array_flip($keys)); |
| 115 |
} |
| 116 |
/** |
| 117 |
* A method, similar to PHP's `array_merge_recursive`, with two differences. |
| 118 |
* |
| 119 |
* 1. Keys in $array2 take precedence over keys in $array1. |
| 120 |
* 2. Non-array keys found in both inputs are not transformed into an array |
| 121 |
* and appended. Rather, the value in $array2 is used. |
| 122 |
* |
| 123 |
* @param array $array1 |
| 124 |
* @param array $array2 |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
private function arrayMergeRecursive(array $array1, array $array2) |
| 128 |
{ |
| 129 |
foreach ($array2 as $key => $value) { |
| 130 |
if (array_key_exists($key, $array1) && is_array($array1[$key]) && is_array($value)) { |
| 131 |
$array1[$key] = $this->isAssoc($array1[$key]) && $this->isAssoc($value) ? $this->arrayMergeRecursive($array1[$key], $value) : array_merge($array1[$key], $value); |
| 132 |
} else { |
| 133 |
$array1[$key] = $value; |
| 134 |
} |
| 135 |
} |
| 136 |
return $array1; |
| 137 |
} |
| 138 |
} |
| 139 |
|