diff --git a/src/Rules.php b/src/Rules.php index e2183a1..fa30a99 100644 --- a/src/Rules.php +++ b/src/Rules.php @@ -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) };