Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpFoundation] Add peekMultiple method to FlashBag #50380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function peekAll(): array
return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : [];
}

public function peekMultiple(array $types): array
{
return \array_key_exists('display', $this->flashes)
? array_intersect_key($this->flashes['display'], array_flip($types))
: [];
}

public function get(string $type, array $default = []): array
{
$return = $default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function peekAll(): array
return $this->flashes;
}

public function peekMultiple(array $types): array
{
return array_intersect_key($this->flashes, array_flip($types));
}

public function get(string $type, array $default = []): array
{
if (!$this->has($type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function peek(string $type, array $default = []): array;
*/
public function peekAll(): array;

/**
* Gets specific types of flash messages.
*/
public function peekMultiple(array $types): array;

/**
* Gets and clears flash from the stack.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ public function testPeekAll()
);
}

public function testPeekMultiple()
{
$array = [
'new' => [
'notice' => 'Foo',
'error' => 'Bar',
'warning' => 'Baz',
],
];

$this->bag->initialize($array);
$this->assertEquals([
'notice' => 'Foo',
'warning' => 'Baz',
], $this->bag->peekMultiple(['notice', 'warning'])
);

$this->assertEquals([
'notice' => 'Foo',
'error' => 'Bar',
'warning' => 'Baz',
], $this->bag->peekAll()
);
}

public function testGet()
{
$this->assertEquals([], $this->bag->get('non_existing'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,25 @@ public function testPeekAll()
], $this->bag->peekAll()
);
}

public function testPeekMultiple()
{
$this->bag->set('notice', 'Foo');
$this->bag->set('error', 'Bar');
$this->bag->set('warning', 'Baz');
$this->assertEquals([
'notice' => ['Foo'],
'warning' => ['Baz'],
], $this->bag->peekMultiple(['notice', 'warning'])
);
$this->assertTrue($this->bag->has('notice'));
$this->assertTrue($this->bag->has('error'));
$this->assertTrue($this->bag->has('warning'));
$this->assertEquals([
'notice' => ['Foo'],
'error' => ['Bar'],
'warning' => ['Baz'],
], $this->bag->peekAll()
);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.