Skip to content

Commit

Permalink
Add support for flashMessage method + fix Paginator type.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored May 15, 2020
1 parent c55b0e6 commit 7c3d74c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Endpoint/BaseEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nette\Security\IIdentity;
use Nette\Security\User;
use Nette\SmartObject;
use Nette\Utils\Paginator;

abstract class BaseEndpoint implements Endpoint
{
Expand All @@ -31,6 +32,9 @@ abstract class BaseEndpoint implements Endpoint
/** @var mixed[] */
protected $data = [];

/** @var string[][] */
private $messages = [];

/** @var bool */
private $startupCheck = false;

Expand Down Expand Up @@ -89,11 +93,21 @@ final public function setData(array $data): void


/**
* Send raw data to output.
*
* @param mixed[] $haystack
* @param int $httpCode
*/
final public function sendJson(array $haystack, int $httpCode = 200): void
{
if ($this->messages !== []) {
if (isset($haystack['flashMessages']) === true) {
throw new \RuntimeException('Flash message was already defined in your data. Did you want to use the flashMessage() function?');
}
$haystack = array_merge($haystack, ['flashMessages' => $this->messages]);
$this->messages = []; // Reset for next response
}

throw new ThrowResponse(new JsonResponse($haystack, $httpCode));
}

Expand Down Expand Up @@ -145,6 +159,23 @@ final public function sendItems(array $items, ?Paginator $paginator = null, arra
}


/**
* Add new flash message to internal storage.
* All Flash messages will be returned in `flashMessages` key in all responses.
* Warning: FlashMessage can change the structure of your response data.
*
* @param string $message
* @param string $type
*/
final public function flashMessage(string $message, string $type = 'info'): void
{
$this->messages[] = [
'message' => $message,
'type' => $type,
];
}


/**
* @param string $key
* @param string|null $message
Expand Down

0 comments on commit 7c3d74c

Please sign in to comment.