LdapTools\Utilities\LdapUtilities::explodeDn PHP Method

explodeDn() public static method

Converts a string distinguished name into its separate pieces.
public static explodeDn ( string $dn, integer $withAttributes = 1 ) : array
$dn string
$withAttributes integer Set to 0 to get the attribute names along with the value.
return array
    public static function explodeDn($dn, $withAttributes = 1)
    {
        $pieces = ldap_explode_dn($dn, $withAttributes);
        if ($pieces === false || !isset($pieces['count']) || $pieces['count'] == 0) {
            throw new InvalidArgumentException(sprintf('Unable to parse DN "%s".', $dn));
        }
        for ($i = 0; $i < $pieces['count']; $i++) {
            $pieces[$i] = self::unescapeValue($pieces[$i]);
        }
        unset($pieces['count']);
        return $pieces;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fromLdap($dn)
 {
     $options = $this->getOptionsArray();
     if (!(isset($options['display_dn']) && $options['display_dn'])) {
         $dn = LdapUtilities::explodeDn($dn);
         $dn = reset($dn);
     }
     return $dn;
 }