From 9797e9135e579dbc5b18c75341a901bbdc5ac821 Mon Sep 17 00:00:00 2001 From: alexmerlin Date: Sun, 21 May 2023 10:49:50 +0300 Subject: [PATCH] [FlashBag] Add peekMultiple method Signed-off-by: alexmerlin --- .../Session/Flash/AutoExpireFlashBag.php | 7 ++++++ .../HttpFoundation/Session/Flash/FlashBag.php | 5 ++++ .../Session/Flash/FlashBagInterface.php | 5 ++++ .../Session/Flash/AutoExpireFlashBagTest.php | 25 +++++++++++++++++++ .../Tests/Session/Flash/FlashBagTest.php | 21 ++++++++++++++++ 5 files changed, 63 insertions(+) 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 ba2687199d7b5..4c055a70d562f 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/AutoExpireFlashBagTest.php @@ -119,6 +119,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 24dbbfe98f05f..9cbd7346dd611 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Flash/FlashBagTest.php @@ -154,4 +154,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() + ); + } }