LDAP-DN is a PHP library for Distinguished name parsing and manipulation. Pawel Decowski, the author of the library, wrote it while working on a legacy PHP project that synchronizes LDAP teams and users. He found out many solution available but very limited and without a clean API. The reason why this library exists.

It breaks down DNs into RDNs and attributes, supports escaping, allows basic manipulation and has 100% test coverage.

Features of LDAP-DN

  • access individual RDNs by index
  • iterate RDNs
  • filter by attribute name
  • support for escaping special characters
  • access attribute values
  • remove fragments of a DN
  • construct DNs
  • case-insensitive but case-preserving (lookups are case-insensitive but attribute names’ and values’ case is preserved)
  • 100% test coverage

Getting started with LDAP-DN

The library require PHP version >7.1, then you can add it to your project with composer :

composer require paweldecowski/ldap-dn

Now you can create a DN object from a string using :

$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn; // 'cn=john.doe,ou=it,ou=leadership,dc=example,dc=org'
echo $dn[0]; // 'dc=org'
echo $dn->filter('ou'); // 'ou=it,ou=leadership'

Dn class as well as classes representing its components (RdnAttribute) implement the __toString method. It means that in string context, they automatically become strings. You can also get the parent DN :

$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn->getParent(); // 'ou=it,ou=leadership,dc=example,dc=org'
echo $dn->getParent()->getParent(); // 'ou=leadership,dc=example,dc=org'

Sometimes you may want to remove a fragment of a DN, for example its base DN.

$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');
$fragmentToRemove = LdapDn\Dn::fromString('dc=example,dc=org');

echo $dn->withRemoved($fragmentToRemove); // 'cn=john.doe,ou=it,ou=leadership'

The main purpose of this library is parsing and manipulating DNs but you can also constructing them even with a shorthand syntax.

LDAP-DN is an open source software released under an MIT license. More information and download at https://github.com/paweldecowski/ldap-dn


Please enter your comment!
Please enter your name here