fix: use CSV for GET array

This commit is contained in:
Victor Westerlund 2024-01-06 14:15:23 +01:00
parent a2154faef0
commit d748d5205a

View file

@ -20,8 +20,15 @@
}
class Rules {
private const CSV_DELIMITER = ",";
private string $property;
/*
# Rule properties
These properties store rules for an instance of a property
*/
public bool $required = false;
// Matched Type against $types array
@ -137,7 +144,7 @@
return !is_bool($value) && in_array($value, $this->enum);
}
private function eval_json(mixed $value, Scope $scope): bool {
private function eval_object(mixed $value, Scope $scope): bool {
// Arrays in POST parameters should already be decoded
if ($scope === Scope::POST) {
return is_array($value);
@ -157,6 +164,20 @@
return true;
}
private function eval_array(string $value, Scope $scope): bool {
// Arrays in POST parameters should already be decoded
if ($scope === Scope::POST) {
return is_array($value);
}
// Mutate property on superglobal with decoded CSV if not already an array
if (!is_array($_GET[$this->property])) {
$GLOBALS[Scope::GET->value][$this->property] = explode(self::CSV_DELIMITER, $_GET[$this->property]);
}
return true;
}
/*
## Public eval methods
These are the entry-point eval methods that in turn can call other
@ -186,8 +207,8 @@
Type::NUMBER => $match = is_numeric($value),
Type::STRING => $match = is_string($value),
Type::BOOLEAN => $match = $this->eval_type_boolean($value, $scope),
Type::ARRAY,
Type::OBJECT => $match = $this->eval_json($value, $scope),
Type::ARRAY => $match = $this->eval_array($value, $scope),
Type::OBJECT => $match = $this->eval_object($value, $scope),
Type::ENUM => $match = $this->eval_type_enum($value),
Type::NULL => $match = is_null($value)
};