Skip to content

Core\Ldap\Dn::rdnAttribute

$dn->rdnAttribute(): string

Returns the attribute name of the first part of a DN. For CN=Ann,OU=Staff,DC=example,DC=test the result is CN.

Use it to see what kind of entry a DN names, such as CN for a person or OU for a unit.

Good to know: the name is returned as it is written in the DN, so cn stays cn. When the first part has several values joined by +, the result is the name of the first one.

Return value

Returns string. The name, written as in the DN. When the first part has several values joined by +, it is the name of the first one.

Examples

A person

<?nvs
// Shows how to read the attribute name of the first part of a DN.
var $person = Core\Ldap\Dn::parse('CN=Ann,OU=Staff,DC=example,DC=test');
// This prints CN.
echo $person->rdnAttribute(), "\n";
Output
CN

Person or unit

<?nvs
// Shows how to tell a person's entry from a unit by the first attribute name.
array<string> $texts = ['CN=Ann,OU=Staff,DC=example,DC=test', 'ou=Staff,DC=example,DC=test'];
// The name is compared without case, because a DN may write `ou` or `OU`.
// This prints a person, then a unit.
foreach ($texts as string $text) {
var $name = Core\Str::upper(Core\Ldap\Dn::parse($text)->rdnAttribute());
echo $name == 'OU' ? 'a unit' : 'a person', "\n";
}
Output
a person
a unit

Several values

<?nvs
// Shows the result when the first part has two values joined by `+`.
var $person = Core\Ldap\Dn::parse('CN=Ann+UID=ann7,OU=Staff,DC=example,DC=test');
// The result is the name of the first value. This prints CN.
echo $person->rdnAttribute(), "\n";
Output
CN