Skip to content

Core\Ldap\Dn::of

Returns a DN with one part, attribute=value, such as DC=test.

The value can come from user input. Each special character in it, such as ,, + or =, is escaped. So the value stays one value, and it cannot add a part to the DN. Use child to add more parts below this one.

Good to know: the attribute name cannot be tainted. An empty value throws a LogicError, and so does an attribute that is not a name.

Parameters

  • $attributestringThe attribute's name, such as CN or OU. It cannot be tainted.
  • $valuestringThe value, such as Ann Lee. It can be tainted. A ,, +, = or other special character in it is escaped, so it stays one value.

Return value

Returns Core\Ldap\Dn. A Core\Ldap\Dn.

Errors

  • LogicErrorThe attribute is not an attribute name, or the value is empty.

Examples

One part

<?nvs
// Shows how to make a DN with one part.
var $top = Core\Ldap\Dn::of('DC', 'test');
// This prints DC=test.
echo $top->toString(), "\n";
Output
DC=test

A value from a form

<?nvs
// Shows how a value from user input is escaped.
// This text came from a form, so it is `tainted`.
tainted string $name = 'Lee, Ann';
var $dn = Core\Ldap\Dn::of('CN', $name);
// The comma is escaped. This prints CN=Lee\, Ann.
echo $dn->toString(), "\n";
Output
CN=Lee\, Ann

An empty value

<?nvs
// Shows the error for an empty value.
try {
var $dn = Core\Ldap\Dn::of('CN', '');
echo $dn->toString(), "\n";
} catch (LogicError $error) {
// This prints Core\Ldap\Dn::of: the value is empty.
echo $error->message, "\n";
}
Output
Core\Ldap\Dn::of: the value is empty