1. <?php
  2. /**
  3. * Loads dataset resources with compatibility for older PHP versions.
  4. *
  5. * @param array $dataset_paths An array of paths to dataset files. Each path can be a local file or a URL.
  6. * @param string $dataset_type A string identifying the dataset type (e.g., 'csv', 'json', 'parquet'). Used for specific parsing.
  7. * @return array An associative array where keys are dataset names and values are the loaded dataset data. Returns an empty array on failure.
  8. */
  9. function loadDatasets(array $dataset_paths, string $dataset_type): array
  10. {
  11. $datasets = [];
  12. foreach ($dataset_paths as $path) {
  13. try {
  14. if (is_url($path)) {
  15. // Load from URL (using file_get_contents for compatibility)
  16. $data = file_get_contents($path);
  17. } else {
  18. // Load from local file
  19. $data = file_get_contents($path);
  20. }
  21. if ($data === false) {
  22. error_log("Error: Could not read dataset from path: " . $path);
  23. continue; // Skip to the next dataset
  24. }
  25. // Basic type handling. Expand as needed.
  26. if ($dataset_type === 'csv') {
  27. $datasets[$path] = parse_csv($data); // Assuming parse_csv function exists
  28. } elseif ($dataset_type === 'json') {
  29. $datasets[$path] = json_decode($data, true); // Decode to associative array
  30. } elseif ($dataset_type === 'parquet') {
  31. // Parquet parsing requires external libraries (e.g., apache-parquet-tools).
  32. // This is a placeholder - replace with actual parquet loading.
  33. error_log("Warning: Parquet parsing not implemented. Dataset: " . $path);
  34. $datasets[$path] = []; //Placeholder
  35. } else {
  36. error_log("Error: Unsupported dataset type: " . $dataset_type . " for path: " . $path);
  37. continue;
  38. }
  39. } catch (Exception $e) {
  40. error_log("Error loading dataset from " . $path . ": " . $e->getMessage());
  41. }
  42. }
  43. return $datasets;
  44. }
  45. /**
  46. * Placeholder for CSV parsing function. Replace with a proper CSV parser.
  47. * @param string $csv_data
  48. * @return array
  49. */
  50. function parse_csv(string $csv_data): array
  51. {
  52. //Simple CSV parser (very basic, doesn't handle quoted values, etc.)
  53. $lines = explode("\n", $csv_data);
  54. $data = [];
  55. foreach ($lines as $line) {
  56. $values = str_split(str_replace(',', ' ', $line)); //Splits by comma
  57. $data[] = $values;
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Checks if a given string is a valid URL.
  63. *
  64. * @param string $string The string to check.
  65. * @return bool True if the string is a valid URL, false otherwise.
  66. */
  67. function is_url(string $string): bool
  68. {
  69. return filter_var($string, FILTER_VALIDATE_URL) !== false;
  70. }
  71. ?>

Add your comment