1. <?php
  2. /**
  3. * Diffs request headers datasets for maintenance tasks with fixed retry intervals.
  4. *
  5. * @param array $dataset1 First dataset of request headers (array of associative arrays).
  6. * @param array $dataset2 Second dataset of request headers (array of associative arrays).
  7. * @param int $retryInterval Seconds to wait before retrying a request.
  8. * @return array Array of differences between the datasets.
  9. */
  10. function diffRequestHeaders(array $dataset1, array $dataset2, int $retryInterval): array
  11. {
  12. $differences = [];
  13. if (empty($dataset1) && empty($dataset2)) {
  14. return $differences; // Both datasets are empty, no differences.
  15. }
  16. if (empty($dataset1)) {
  17. return $differences; // dataset1 is empty, return all entries from dataset2 as differences
  18. }
  19. if (empty($dataset2)) {
  20. return $differences; // dataset2 is empty, return all entries from dataset1 as differences
  21. }
  22. // Iterate through dataset1 and check for differences in dataset2.
  23. foreach ($dataset1 as $item1) {
  24. $foundMatch = false;
  25. foreach ($dataset2 as $item2) {
  26. if (isSameDatasetEntry($item1, $item2)) {
  27. $foundMatch = true;
  28. break;
  29. }
  30. }
  31. if (!$foundMatch) {
  32. $differences[] = [
  33. 'dataset' => 'dataset1',
  34. 'item' => $item1,
  35. 'message' => 'Item not found in dataset2.',
  36. ];
  37. }
  38. }
  39. // Iterate through dataset2 and check for differences in dataset1.
  40. foreach ($dataset2 as $item2) {
  41. $foundMatch = false;
  42. foreach ($dataset1 as $item1) {
  43. if (isSameDatasetEntry($item1, $item2)) {
  44. $foundMatch = true;
  45. break;
  46. }
  47. }
  48. if (!$foundMatch) {
  49. $differences[] = [
  50. 'dataset' => 'dataset2',
  51. 'item' => $item2,
  52. 'message' => 'Item not found in dataset1.',
  53. ];
  54. }
  55. }
  56. return $differences;
  57. }
  58. /**
  59. * Checks if two dataset entries are the same.
  60. *
  61. * @param array $item1 First dataset entry.
  62. * @param array $item2 Second dataset entry.
  63. * @return bool True if the entries are the same, false otherwise.
  64. */
  65. function isSameDatasetEntry(array $item1, array $item2): bool
  66. {
  67. return array_key_exists('request_url', $item1) && array_key_exists('request_url', $item2) &&
  68. $item1['request_url'] == $item2['request_url'] &&
  69. array_key_exists('user_agent', $item1) && array_key_exists('user_agent', $item2) &&
  70. $item1['user_agent'] == $item2['user_agent'] &&
  71. array_key_exists('headers', $item1) && array_key_exists('headers', $item2) &&
  72. isSameHeaderDataset($item1['headers'], $item2['headers']);
  73. }
  74. /**
  75. * Checks if two header datasets are the same.
  76. *
  77. * @param array $headers1 First header dataset.
  78. * @param array $headers2 Second header dataset.
  79. * @return bool True if the header datasets are the same, false otherwise.
  80. */
  81. function isSameHeaderDataset(array $headers1, array $headers2): bool
  82. {
  83. if (count($headers1) !== count($headers2)) {
  84. return false;
  85. }
  86. foreach ($headers1 as $key => $value) {
  87. if (!isset($headers2[$key]) || $headers2[$key] !== $value) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. // Example Usage (replace with your actual data)
  94. /*
  95. $dataset1 = [
  96. ['request_url' => 'https://example.com/api/data', 'user_agent' => 'Mozilla/5.0', 'headers' => ['X-Custom-Header' => 'value1']],
  97. ['request_url' => 'https://example.com/api/data', 'user_agent' => '

Add your comment