<?php
/**
* Checks collection constraints for backward compatibility in dry-run mode.
*
* @param array $collection The collection to check.
* @param array $constraints An array of constraint definitions. Each constraint should be an associative array
* with keys: 'name' (string), 'type' (string, e.g., 'int', 'string', 'array'),
* 'min' (int, optional), 'max' (int, optional), 'allowed_values' (array, optional).
* @param bool $dry_run Whether to perform a dry run (no actual changes). Defaults to true.
* @return array An array of constraint violations. Each element is an associative array with
* keys: 'name' (string), 'message' (string). Returns an empty array if no violations.
*/
function checkCollectionConstraints(array $collection, array $constraints, bool $dry_run = true): array
{
$violations = [];
foreach ($constraints as $constraint) {
$name = $constraint['name'];
$type = $constraint['type'];
switch ($type) {
case 'int':
if (!is_int($collection[$name])) {
$violations[] = ['name' => $name, 'message' => "Expected integer, got " . gettype($collection[$name])];
}
if (isset($constraint['min']) && $collection[$name] < $constraint['min']) {
$violations[] = ['name' => $name, 'message' => "Must be at least " . $constraint['min']];
}
if (isset($constraint['max']) && $collection[$name] > $constraint['max']) {
$violations[] = ['name' => $name, 'message' => "Must be at most " . $constraint['max']];
}
break;
case 'string':
if (!is_string($collection[$name])) {
$violations[] = ['name' => $name, 'message' => "Expected string, got " . gettype($collection[$name])];
}
if (isset($constraint['min_length']) && strlen($collection[$name]) < $constraint['min_length']) {
$violations[] = ['name' => $name, 'message' => "Must be at least " . $constraint['min_length'] . " characters long"];
}
if (isset($constraint['max_length']) && strlen($collection[$name]) > $constraint['max_length']) {
$violations[] = ['name' => $name, 'message' => "Must be at most " . $constraint['max_length'] . " characters long"];
}
break;
case 'array':
if (!is_array($collection[$name])) {
$violations[] = ['name' => $name, 'message' => "Expected array, got " . gettype($collection[$name])];
}
if (isset($constraint['allowed_values'])) {
$allowedValues = $constraint['allowed_values'];
$valid = true;
foreach ($collection[$name] as $item) {
if (!in_array($item, $allowedValues)) {
$valid = false;
break;
}
}
if (!$valid) {
$violations[] = ['name' => $name, 'message' => "Array must contain only values from: " . implode(', ', $allowedValues)];
}
}
break;
default:
// Unknown type - log a warning or handle as needed
error_log("Unknown constraint type: " . $type);
break;
}
}
return $violations;
}
//Example usage (dry run)
$collection = [
'age' => 30,
'name' => 'John Doe',
'tags' => ['tag1', 'tag2', 'invalid_tag'],
'score' => 150
];
$constraints = [
['name' => 'age', 'type' => 'int', 'min' => 18, 'max' => 100],
['name' => 'name', 'type' => 'string', 'min_length' => 3, 'max_length' => 20],
['name' => 'tags', 'type'
Add your comment