52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace com\fwdekker\deathnotifier;
|
|
|
|
|
|
/**
|
|
* A response from the server to a request.
|
|
*/
|
|
class Response
|
|
{
|
|
/**
|
|
* @var bool `true` if and only if the request was fully completed
|
|
*/
|
|
public readonly bool $satisfied;
|
|
/**
|
|
* @var mixed the payload corresponding to the client's query
|
|
*/
|
|
public readonly mixed $payload;
|
|
|
|
|
|
/**
|
|
* Constructs a new response.
|
|
*
|
|
* If {@see $satisfied} is `false`, then {@see $payload} must be an array containing at least the strings `target`
|
|
* and `message`. Otherwise, {@see payload} may be anything.
|
|
*
|
|
* @param bool $satisfied `true` if and only if the request was fully completed
|
|
* @param mixed $payload the payload corresponding to the client's query
|
|
*/
|
|
public function __construct(bool $satisfied, mixed $payload)
|
|
{
|
|
$this->satisfied = $satisfied;
|
|
$this->payload = $payload;
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns a response indicating something went wrong, with {@see payload} describing what went wrong in what place
|
|
* and {@see $satisfied} set to `false`.
|
|
*
|
|
* @param ?string $message the message to show to the user, or `null` if no message should be shown
|
|
* @param ?string $target the name of the input field that caused an error, or `null` if no single input is to be
|
|
* blamed
|
|
* @return Response a response indicating something went wrong, with {@see payload} describing what went wrong in
|
|
* what place and {@see $satisfied} set to `false`
|
|
*/
|
|
public static function unsatisfied(?string $message, ?string $target = null): Response
|
|
{
|
|
return new Response(satisfied: false, payload: ["message" => $message, "target" => $target]);
|
|
}
|
|
}
|