ArrayUtils.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. class ArrayUtils
  5. {
  6. /**
  7. * Vérifie que tous les éléments d'une array valident le callback
  8. *
  9. * @param array $array
  10. * @param callable $callback
  11. * @return bool
  12. */
  13. public static function all(array $array, callable $callback) {
  14. foreach ($arr as $item){
  15. if (call_user_func($callback, $item)) {
  16. return true;
  17. }
  18. }
  19. return false;
  20. }
  21. /**
  22. * Vérifie qu'au moins un élément d'une array valide le callback
  23. *
  24. * @param array $array
  25. * @param callable $callback
  26. * @return bool
  27. */
  28. public static function any(array $array, callable $callback) {
  29. foreach ($arr as $item){
  30. if (!call_user_func($callback, $item)){
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. /**
  37. * Returns an array containing changes between the first array the keys/values from an array
  38. * which are absent or different from $initialData
  39. *
  40. * If recursive is true, when values are arrays, they'll get processed too to only keep changes. Else,
  41. * the whole value will be added to the changes if there is at least one difference between the former and the latter.
  42. *
  43. * @param mixed[] $initialArray
  44. * @param mixed[] $newArray
  45. * @param bool $recursive
  46. * @param callable|null $callback An optional callback method to test the equality between to values. The callback shall
  47. * accept two parameters (the values) and return true if the values are equals.
  48. * @return mixed[]
  49. */
  50. public function getChanges(array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null): array
  51. {
  52. $changes = [];
  53. foreach ($newArray as $field => $value) {
  54. if (!array_key_exists($field, $initialArray)) {
  55. $changes[$field] = $value;
  56. }
  57. elseif ($recursive && is_array($initialArray[$field]) && is_array($value)) {
  58. $newVal = $this->getChanges($initialArray[$field], $value, $recursive, $callback);
  59. if (!empty($newVal)) {
  60. $changes[$field] = $newVal;
  61. }
  62. }
  63. elseif ($callback === null && $value !== $initialArray[$field]) {
  64. $changes[$field] = $value;
  65. }
  66. elseif ($callback !== null && !$callback($value, $initialArray[$field])) {
  67. $changes[$field] = $value;
  68. }
  69. }
  70. return $changes;
  71. }
  72. /**
  73. * If the given $key is set in the array and if the corresponding value is not null,
  74. * cast this value and returns it. Else, returns null.
  75. *
  76. * Ex:
  77. *
  78. * $array = ['a' => '123']
  79. *
  80. * ArrayUtils::getAndCast($array, 'a', 'int') // => returns `123` as an integer
  81. * ArrayUtils::getAndCast($array, 'b', 'int') // => returns `null`
  82. *
  83. * @param mixed[] $array
  84. * @param string $key
  85. * @param string $type
  86. * @return mixed
  87. */
  88. public static function getAndCast(array $array, mixed $key, string $type): mixed
  89. {
  90. $value = $array[$key] ?? null;
  91. if ($value !== null) {
  92. settype($value, $type);
  93. }
  94. return $value;
  95. }
  96. }