Skip to content
Closed
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
6 changes: 6 additions & 0 deletions src/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions tests/TestCase/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down