Skip to content

Commit

Permalink
update logic, replace static with filling current instance, added abi…
Browse files Browse the repository at this point in the history
…lity to add custom data
  • Loading branch information
MasterRO94 committed May 17, 2021
1 parent 4eef5f2 commit 61fc3e8
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 18 deletions.
85 changes: 67 additions & 18 deletions src/Flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,88 @@ class Flash

public string $type;

public bool $session;
public bool $storeInMainSession;

protected array $data;

/**
* Flash constructor.
*
* @param string $message
* @param string $type
* @param bool $session
* @param bool $storeInMainSession
*/
public function __construct(string $message = '', string $type = 'success', bool $session = true)
{
public function __construct(
string $message = '',
string $type = 'success',
bool $storeInMainSession = true,
array $data = []
) {
$this->message = $message;
$this->type = $type;
$this->session = $session;
$this->storeInMainSession = $storeInMainSession;
$this->data = $data;
}

public static function make(string $message = '', string $type = 'success', bool $session = true): Flash
public static function make(
string $message = '',
string $type = 'success',
bool $storeInMainSession = true,
array $data = []
): Flash {
return new static($message, $type, $storeInMainSession, $data);
}

public function with(array $data): Flash
{
return new static($message, $type, $session);
$this->data = $data;

return $this;
}

public function fill(
?string $message = null,
?string $type = null,
?bool $storeInMainSession = null,
?array $data = null
): Flash {
if (null !== $message) {
$this->message = $message;
}

if (null !== $type) {
$this->type = $type;
}

if (null !== $storeInMainSession) {
$this->storeInMainSession = $storeInMainSession;
}

if (null !== $data) {
$this->data = $data;
}

return $this;
}

public function success(string $message, bool $session = true)
public function success(string $message, ?bool $storeInMainSession = null, ?array $data = null)
{
return static::make($message, 'success', $session)->push();
return $this->fill($message, 'success', $storeInMainSession, $data)->push();
}

public function warning(string $message, bool $session = true)
public function warning(string $message, ?bool $storeInMainSession = null, ?array $data = null)
{
return static::make($message, 'warning', $session)->push();
return $this->fill($message, 'warning', $storeInMainSession, $data)->push();
}

public function info(string $message, bool $session = true)
public function info(string $message, ?bool $storeInMainSession = null, ?array $data = null)
{
return static::make($message, 'info', $session)->push();
return $this->fill($message, 'info', $storeInMainSession, $data)->push();
}

public function error(string $message, bool $session = true)
public function error(string $message, ?bool $storeInMainSession = null, ?array $data = null)
{
return static::make($message, 'error', $session)->push();
return $this->fill($message, 'error', $storeInMainSession, $data)->push();
}

/**
Expand All @@ -60,11 +103,17 @@ public function push()
{
$type = $this->type;
$message = $this->message;
$data = $this->data;

if ($this->session) {
return Session::push('flash_messages', compact('type', 'message'));
if ($this->storeInMainSession) {
return Session::push('flash_messages', compact('type', 'message', 'data'));
}

return Session::flash('flash_messages', [compact('type', 'message')]);
return Session::flash('flash_messages', [compact('type', 'message', 'data')]);
}

public function __call($method, $arguments = [])
{
$this->fill($arguments[0], $method)->push();
}
}
44 changes: 44 additions & 0 deletions tests/FlashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,48 @@ public function it_puts_multiple_flash_messages()
$this->assertEquals($test[1], $sessionMessage['type']);
}
}

/** @test */
public function it_can_store_additional_data()
{
flash()->with(['test1' => 'Test 1'])->success('Success message');

[$sessionMessage] = session('flash_messages', [null]);

$this->assertEquals('Success message', $sessionMessage['message']);
$this->assertEquals('success', $sessionMessage['type']);
$this->assertEquals(['test1' => 'Test 1'], $sessionMessage['data']);

session()->flush();

Flash::make('Warning message', 'warning', true, ['test2' => 'Test 2'])->push();

[$sessionMessage] = session('flash_messages', [null]);

$this->assertEquals('Warning message', $sessionMessage['message']);
$this->assertEquals('warning', $sessionMessage['type']);
$this->assertEquals(['test2' => 'Test 2'], $sessionMessage['data']);
}

/** @test */
public function it_can_handle_custom_types()
{
flash()->emergency('Alert!');

[$sessionMessage] = session('flash_messages', [null]);

$this->assertEquals('Alert!', $sessionMessage['message']);
$this->assertEquals('emergency', $sessionMessage['type']);

session()->flush();

Flash::secondary('Why not?');

[$sessionMessage] = session('flash_messages', [null]);

$this->assertEquals('Why not?', $sessionMessage['message']);
$this->assertEquals('secondary', $sessionMessage['type']);

session()->flush();
}
}

0 comments on commit 61fc3e8

Please sign in to comment.