diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php index 80bbeda0f8828..9408f1097a543 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/AutoExpireFlashBag.php @@ -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; diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index 659d59d18699f..8c06b913fde64 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -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)) { diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index bbcf7f8b7d877..4b3bcfeae30f0 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -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. * diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php index 6a6510a576b9c..03daa1fa87e7e 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php @@ -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')); diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php index 59e3f1f0e69a7..3a9ca9494095e 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php @@ -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() + ); + } }