* */ class HttpRequest implements ArrayAccess { /** * @var array */ private array $data; /** * @param array $data */ private function __construct(array $data) { $this->data = $data; } /** * Creates a new HttpRequest instance, and ensures that the given request data validates the given schema. * This is a simple function that only supports flat schemas (non-composed, the data must only be a k/v array pair.) * @param array $request the request's data * @param array $fails a reference to a failure array, that will contain the reported validation failures. * @param array $schema the schema to satisfy. a schema is a simple array with a string key (which is the top-level field name), and a set of validators * @return HttpRequest|null the built HttpRequest instance, or null if a field is missing, or if any of the schema validator failed */ public static function from(array $request, array &$fails, array $schema): ?HttpRequest { $failure = false; foreach ($schema as $fieldName => $fieldValidators) { if (!isset($request[$fieldName])) { $fails[] = FieldValidationFail::missing($fieldName); $failure = true; continue; } $failure |= Validation::validate($request[$fieldName], $fieldName, $fails, ...$fieldValidators); } if ($failure) { return null; } return new HttpRequest($request); } public function offsetExists($offset): bool { return isset($this->data[$offset]); } /** * @param $offset * @return mixed */ public function offsetGet($offset) { return $this->data[$offset]; } /** * @param $offset * @param $value * @throws Exception */ public function offsetSet($offset, $value) { throw new Exception("requests are immutable objects."); } /** * @param $offset * @throws Exception */ public function offsetUnset($offset) { throw new Exception("requests are immutable objects."); } }