ArrayTrait.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * Copyright 2018 Google LLC
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Google\ApiCore;
  33. /**
  34. * Provides basic array helper methods.
  35. *
  36. * @internal
  37. */
  38. trait ArrayTrait
  39. {
  40. /**
  41. * Pluck a value out of an array.
  42. *
  43. * @param string $key
  44. * @param array $arr
  45. * @param bool $isRequired
  46. * @return mixed|null
  47. * @throws \InvalidArgumentException
  48. */
  49. private function pluck(string $key, array &$arr, bool $isRequired = true)
  50. {
  51. if (!array_key_exists($key, $arr)) {
  52. if ($isRequired) {
  53. throw new \InvalidArgumentException(
  54. "Key $key does not exist in the provided array."
  55. );
  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(
  115. $arr,
  116. array_flip($keys)
  117. );
  118. }
  119. /**
  120. * A method, similar to PHP's `array_merge_recursive`, with two differences.
  121. *
  122. * 1. Keys in $array2 take precedence over keys in $array1.
  123. * 2. Non-array keys found in both inputs are not transformed into an array
  124. * and appended. Rather, the value in $array2 is used.
  125. *
  126. * @param array $array1
  127. * @param array $array2
  128. * @return array
  129. */
  130. private function arrayMergeRecursive(array $array1, array $array2)
  131. {
  132. foreach ($array2 as $key => $value) {
  133. if (array_key_exists($key, $array1) && is_array($array1[$key]) && is_array($value)) {
  134. $array1[$key] = ($this->isAssoc($array1[$key]) && $this->isAssoc($value))
  135. ? $this->arrayMergeRecursive($array1[$key], $value)
  136. : array_merge($array1[$key], $value);
  137. } else {
  138. $array1[$key] = $value;
  139. }
  140. }
  141. return $array1;
  142. }
  143. }