@ -17,9 +28,9 @@ class HttpRequest implements ArrayAccess {
/**
/**
* Creates a new HttpRequest instance, and ensures that the given request data validates the given schema.
* 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.)
* 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<string,mixed> $request the request's data
* @param array $fails a reference to a failure array, that will contain the reported validation failures.
* @param array<string,ValidationFail> $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
* @param array<string,Validator[]> $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
* @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 {
public static function from(array $request, array &$fails, array $schema): ?HttpRequest {
@ -43,14 +54,27 @@ class HttpRequest implements ArrayAccess {
return isset($this->data[$offset]);
return isset($this->data[$offset]);
}
}
/**
* @param $offset
* @return mixed
*/
public function offsetGet($offset) {
public function offsetGet($offset) {
return $this->data[$offset];
return $this->data[$offset];
}
}
/**
* @param $offset
* @param $value
* @throws Exception
*/
public function offsetSet($offset, $value) {
public function offsetSet($offset, $value) {
throw new Exception("requests are immutable objects.");
throw new Exception("requests are immutable objects.");
}
}
/**
* @param $offset
* @throws Exception
*/
public function offsetUnset($offset) {
public function offsetUnset($offset) {
throw new Exception("requests are immutable objects.");
throw new Exception("requests are immutable objects.");
* @param callable $validate_fn the validate function. Must have the same signature as the {@link Validator::validate()} method.
* @param callable(string, mixed): ValidationFail[] $validate_fn the validate function. Must have the same signature as the {@link Validator::validate()} method.
*/
*/
public function __construct(callable $validate_fn) {
public function __construct(callable $validate_fn) {
* A simple validator that takes a predicate and an error factory
* A simple validator that takes a predicate and an error factory
*/
*/
class SimpleFunctionValidator extends Validator {
class SimpleFunctionValidator extends Validator {
/**
* @var callable(mixed): bool
*/
private $predicate;
private $predicate;
/**
* @var callable(string): ValidationFail[]
*/
private $errorFactory;
private $errorFactory;
/**
/**
* @param callable $predicate a function predicate with signature: `(string) => bool`, to validate the given string
* @param callable(mixed): bool $predicate a function predicate with signature: `(string) => bool`, to validate the given string
* @param callable $errorsFactory a factory function with signature `(string) => array` to emit failures when the predicate fails
* @param callable(string): ValidationFail[] $errorsFactory a factory function with signature `(string) => array` to emit failures when the predicate fails
*/
*/
public function __construct(callable $predicate, callable $errorsFactory) {
public function __construct(callable $predicate, callable $errorsFactory) {