death-notifier/src/main/php/com/fwdekker/deathnotifier/Response.php

64 lines
2.1 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier;
/**
* A response from the server to a request.
*/
class Response
{
/**
* @var mixed The payload corresponding to the client's query.
*/
public mixed $payload;
/**
* @var bool `true` if and only if the request was fully completed.
*/
public bool $satisfied;
/**
* Constructs a new response.
*
* If `satisfied` is `false`, then `payload` must be an array containing at least the strings `target` and
* `message`. Otherwise, `payload` may be anything.
*
* @param mixed $payload the payload corresponding to the client's query
* @param bool $satisfied `true` if and only if the request was fully completed
*/
public function __construct(mixed $payload, bool $satisfied)
{
$this->payload = $payload;
$this->satisfied = $satisfied;
}
/**
* Returns a response indicating that the request was completed successfully, with the given `payload` and
* `satisfied` set to `true`.
*
* @param ?mixed $payload the payload to attach to the `Response`
* @return Response a response with the given `payload` and `satisfied` set to `true`.
*/
public static function satisfied(mixed $payload = null): Response
{
return new Response(payload: $payload, satisfied: true);
}
/**
* Returns a response indicating something went wrong, with `payload` describing what went wrong in what place and
* `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 with `payload` describing what went wrong in what place and `satisfied` set to
* `false`
*/
public static function unsatisfied(?string $message, ?string $target = null): Response
{
return new Response(payload: ["message" => $message, "target" => $target], satisfied: false);
}
}