diff --git a/src/Identity.php b/src/Identity.php index 18d5281b..1f005160 100644 --- a/src/Identity.php +++ b/src/Identity.php @@ -109,6 +109,12 @@ public function get(?string $field = null): mixed $map = $this->_config['fieldMap']; if (isset($map[$field])) { $field = $map[$field]; + } else { + // Check if the first segment of a dot-separated path has a mapping + $parts = explode('.', $field, 2); + if (isset($parts[1]) && isset($map[$parts[0]])) { + $field = $map[$parts[0]] . '.' . $parts[1]; + } } return Hash::get($this->data, $field); diff --git a/tests/TestCase/IdentityTest.php b/tests/TestCase/IdentityTest.php index 4f8ba363..9e007782 100644 --- a/tests/TestCase/IdentityTest.php +++ b/tests/TestCase/IdentityTest.php @@ -61,6 +61,43 @@ public function testGet(): void $this->assertSame($data, $identity->get()); } + /** + * Test field mapping with dot notation + * + * @return void + */ + public function testFieldMappingWithDotNotation(): void + { + $data = [ + 'id' => 1, + 'user_account' => new Entity(['id' => 2, 'role' => 'admin']), + 'profile_data' => ['email' => 'test@example.com', 'preferences' => ['theme' => 'dark']], + ]; + + $identity = new Identity($data, [ + 'fieldMap' => [ + 'account' => 'user_account', + 'profile' => 'profile_data', + ], + ]); + + // Test that mapped fields can be accessed directly + $this->assertInstanceOf(Entity::class, $identity->get('account')); + $this->assertSame(['email' => 'test@example.com', 'preferences' => ['theme' => 'dark']], $identity->get('profile')); + + // Test that fieldMap works with dot notation for nested access + $this->assertSame('admin', $identity->get('account.role')); + $this->assertSame('test@example.com', $identity->get('profile.email')); + $this->assertSame('dark', $identity->get('profile.preferences.theme')); + + // Test that original field names still work with dot notation + $this->assertSame('admin', $identity->get('user_account.role')); + $this->assertSame('test@example.com', $identity->get('profile_data.email')); + + // Test that non-mapped fields with dot notation still work + $this->assertNull($identity->get('missing.field')); + } + /** * Test mapping fields *