<?php
/**
* Normalizes header metadata for batch processing.
*
* This function takes an array of header metadata and standardizes
* it to a consistent format. It focuses on simple normalization,
* handling common variations in header names and values.
*
* @param array $header_metadata An array of header metadata. Each element
* should be an associative array representing
* a single header.
* @return array Normalized header metadata. Returns an empty array on error.
*/
function normalizeHeaderMetadata(array $header_metadata): array
{
$normalized_metadata = [];
foreach ($header_metadata as $header) {
$normalized_header = [];
// Normalize 'header_name' key
$normalized_header['name'] = trim($header['header_name']);
// Normalize 'header_value' key - handle different data types
$normalized_header['value'] = trim($header['header_value']);
if (is_numeric($normalized_header['value'])) {
$normalized_header['value'] = (float)$normalized_header['value']; // Convert to float if numeric
} else {
$normalized_header['value'] = $normalized_header['value']; // Keep as string otherwise
}
//Add other potential normalized keys, like 'description'
if(isset($header['description'])){
$normalized_header['description'] = trim($header['description']);
}
$normalized_metadata[] = $normalized_header;
}
return $normalized_metadata;
}
//Example Usage (for testing)
/*
$header_data = [
['header_name' => ' Content-Type ', 'header_value' => 'text/html'],
['header_name' => ' Cache-Control ', 'header_value' => 'no-cache'],
['header_name' => ' Max-Age ', 'header_value' => '3600'],
['header_name' => 'description', 'header_value' => 'This is a test description']
];
$normalized_data = normalizeHeaderMetadata($header_data);
print_r($normalized_data);
*/
?>
Add your comment