diff --git a/src/View/Helper/IdentityHelper.php b/src/View/Helper/IdentityHelper.php index 6c2dc947..88da0cd7 100644 --- a/src/View/Helper/IdentityHelper.php +++ b/src/View/Helper/IdentityHelper.php @@ -103,7 +103,11 @@ public function is(int|string $id, string $field = 'id'): bool } /** - * Gets user data + * Get data from the identity. + * + * You can use dot notation to fetch nested data. + * Calling the method without any argument will return + * the entire data array/object (same as `IdentityInterface::getOriginalData()`). * * @param string|null $key Key of something you want to get from the identity data * @return mixed @@ -120,4 +124,14 @@ public function get(?string $key = null): mixed return Hash::get($this->_identity, $key); } + + /** + * Returns the identity instance. + * + * @return \Authentication\IdentityInterface|null + */ + public function getIdentity(): ?IdentityInterface + { + return $this->_identity; + } } diff --git a/tests/TestCase/View/Helper/IdentityHelperTest.php b/tests/TestCase/View/Helper/IdentityHelperTest.php index b731f769..b41f0162 100644 --- a/tests/TestCase/View/Helper/IdentityHelperTest.php +++ b/tests/TestCase/View/Helper/IdentityHelperTest.php @@ -90,5 +90,19 @@ public function testWithOutIdentity() $this->assertNull($helper->getId()); $this->assertFalse($helper->is(1)); + + $this->assertNull($helper->getIdentity()); + } + + public function testGetIdentity() + { + $identity = new Identity([ + 'id' => 1, + ]); + $request = (new ServerRequest())->withAttribute('identity', $identity); + $view = new View($request); + + $helper = new IdentityHelper($view); + $this->assertSame($identity, $helper->getIdentity()); } }