At some point in every developer's life, you build a feature that feels completely permanent—until someone asks for a slightly different version of it next Tuesday. Then another one on Thursday. Before you know it, you're maintaining five nearly identical PHP classes that only differ by a few column names.
Instead of duplicating code every time the requirements shift, you can hand the steering wheel to a JSON recipe file and let data dictate what your code does.
What is a JSON recipe?
Think of it like an actual cooking recipe, except instead of "preheat oven to 350°F" and "fold in the cheese," it's a declarative config file your application reads at runtime and executes against.
Your code owns how each check works. The JSON file owns which headers and rules apply.
The hardcoded headache
I ran into this exact problem on a form that needed to validate different Excel file uploads. The catch? Every Excel file had completely different headers.
One upload expected Student ID, First Name, and Email. Another expected Account Code, Department, and Amount. A third had its own set of columns entirely. Hardcoding that in PHP quickly turns into a match or switch block you dread opening:
class ExcelValidator
{
public function validate(string $type, array $headers): array
{
$required = match ($type) {
'roster' => ['Student ID', 'First Name', 'Last Name', 'Email'],
'budget' => ['Account Code', 'Department', 'Amount'],
default => throw new InvalidArgumentException("Unknown upload type: {$type}"),
};
$missing = array_diff($required, $headers);
return $missing !== []
? ['Missing required headers: ' . implode(', ', $missing)]
: [];
}
}
Every time a new spreadsheet template shows up, you have to open ExcelValidator, add another branch, and re-test the whole thing.
Handing the job to a JSON recipe
Instead of wiring every spreadsheet format into PHP, describe each Excel template in its own JSON recipe:
{
"upload_type": "roster_import",
"required_headers": ["Student ID", "First Name", "Last Name", "Email"],
"rules": {
"Student ID": "numeric",
"Email": "email"
}
}
Then write one small validator that reads whichever recipe matches the form selection and validates the uploaded file against it:
class ExcelRecipeValidator
{
private const RULES = [
'numeric' => NumericRule::class,
'email' => EmailRule::class,
];
public function validate(array $headers, array $rows, string $recipePath): array
{
$json = file_get_contents($recipePath);
$recipe = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$missing = array_diff($recipe['required_headers'], $headers);
if ($missing !== []) {
return ['Missing required headers: ' . implode(', ', $missing)];
}
$errors = [];
foreach ($rows as $index => $row) {
foreach ($recipe['rules'] as $column => $ruleName) {
$rule = self::RULES[$ruleName]
?? throw new InvalidArgumentException("Unknown rule: {$ruleName}");
if (!$rule::passes($row[$column] ?? null)) {
$errors[] = "Row {$index}: {$column} failed {$ruleName} validation";
}
}
}
return $errors;
}
}
Notice how clean that boundary is:
- The JSON file never contains arbitrary code—it only picks from an explicit allowlist (
RULES). - Every rule is a small, testable class that checks a single value.
- Whenever a new Excel template shows up, you don't touch
ExcelRecipeValidatorat all—you just drop in a new JSON recipe.
Why it works so well
- Zero code duplication for new formats. New spreadsheet headers? New JSON file. Done.
- Readable by non-authors. Even someone who has never opened your PHP classes can glance at the recipe and immediately see which headers and rules a spreadsheet requires.
- Pairs naturally with the Strategy Pattern. Each rule in
RULESis just a strategy, and the JSON recipe is what wires it to each column.